0
votes

I am new to Machine Learning . When i am practicing Decision tree regression model with default parameters using boston data set from scikit-learn module.

After this link solution(How to Build a Decision tree Regressor model), i printed model accuracy on training data set with

print(dt_reg.score(X_train,Y_train)))
print(dt_reg.score(X_test,Y_test)))

Now, im facing issue with : print the Predicted housing price for first two samples of X_test set. For this , i wrote like below, but not getting correct output. Could you please help me to predict first 2 sample of X_test dataset.

predicted = dt_reg.predict(X_test)

for i in range(2):
    print("Predict housing price",predicted[i])
2

2 Answers

0
votes

For demonstration purposes, let's see the full code. Say you train your regression model like this:

from sklearn import datasets, model_selection, tree

boston = datasets.load_boston()

x_train, x_test, y_train, y_test = model_selection.train_test_split(boston.data,boston.target, random_state=30)

dt  = tree.DecisionTreeRegressor()

dt_reg = dt.fit(x_train, y_train)

Now print the train and test scores:

print(dt_reg.score(x_train, y_train))
print(dt_reg.score(x_test, y_test))
>> 1.0
>> 0.5826465689845075

Let's see how the prediction on test data looks like:

predicted = dt_reg.predict(x_test)
print(predicted)
>> array([18.2, 12.8, 20.1, 30.1, 14.5, .....])

Now select and print the output of prediction on the first two test samples:

for i in predicted[:2]:
    print(i)
>> 18.2
>> 12.8
0
votes

You can use like as follows:

I assume that you wrote this code firstly,

from sklearn import datasets, model_selection, tree

boston = datasets.load_boston()

x_train, x_test, y_train, y_test = model_selection.train_test_split(boston.data,boston.target, random_state=30)

dt  = tree.DecisionTreeRegressor()

dt_reg = dt.fit(x_train, y_train)

and then put the predicted y values in y_pred variable as follows:

y_pred = dt_reg.predict(X_test)

and finally, you can write it using:

y_pred[0:n]

n shows the number of results that you want to show. Put 2 instead of "n"