无法计算决策树回归器的模型性能

时间:2020-05-30 13:22:09

标签: python-3.x machine-learning scikit-learn decision-tree

尽管我的代码在repl上运行良好并且确实给了我结果,但是在Katacoda测试环境中却不幸失败了。

我还将在此处附加repl文件以供您查看,其中还包含在我编写的代码上方被注释的问题。

请仔细检查并让我知道我在这里犯了什么错误。

回复链接 https://repl.it/repls/WarmRobustOolanguage

也在下面共享代码

评论问题说明

#Import two modules sklearn.datasets, and #sklearn.model_selection.
#Import numpy and set random seed to 100.

#Load popular Boston dataset from sklearn.datasets module #and assign it to variable boston.

#Split boston.data into two sets names X_train and X_test. #Also, split boston.target into two sets Y_train and Y_test.

#Hint: Use train_test_split method from #sklearn.model_selection; set random_state to 30.
#Print the shape of X_train dataset.

#Print the shape of X_test dataset.

import sklearn.datasets as datasets
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import RandomizedSearchCV
from sklearn.model_selection import cross_val_score
import numpy as np
np.random.seed(100)
max_depth = range(2, 6)

boston = datasets.load_boston()

X_train, X_test, Y_train, Y_test = train_test_split(boston.data, boston.target, random_state=30)

print(X_train.shape)
print(X_test.shape)

#Import required module from sklearn.tree.

#Build a Decision tree Regressor model from X_train set and #Y_train labels, with default parameters. Name the model as #dt_reg.

#Evaluate the model accuracy on training data set and print #it's score.

#Evaluate the model accuracy on testing data set and print it's score.

#Predict the housing price for first two samples of X_test #set and print them.(Hint : Use predict() function)

dt_reg = DecisionTreeRegressor(random_state=1)
dt_reg = dt_reg.fit(X_train, Y_train)

print('Accuracy of Train Data :', cross_val_score(dt_reg, X_train,Y_train, cv=10 ))
print('Accuracy of Test Data :', cross_val_score(dt_reg, X_test,Y_test, cv=10 ))
predicted = dt_reg.predict(X_test[:2])
print(predicted)

#Fit multiple Decision tree regressors on X_train data and #Y_train labels with max_depth parameter value changing from #2 to 5.

#Evaluate each model accuracy on testing data set.

#Hint: Make use of for loop
#Print the max_depth value of the model with highest accuracy.

dt_reg = DecisionTreeRegressor()
random_grid = {'max_depth': max_depth}
dt_random = RandomizedSearchCV(estimator = dt_reg, param_distributions = random_grid, 
n_iter = 90, cv = 3, verbose=2, random_state=42, n_jobs = -1)

dt_random.fit(X_train, Y_train)
dt_random.best_params_

def evaluate(model, test_features, test_labels):
    predictions = model.predict(test_features)
    errors = abs(predictions - test_labels)
    mape = 100 * np.mean(errors / test_labels)
    accuracy = 100 - mape
    print('Model Performance')
    print('Average Error: {:0.4f} degrees.'.format(np.mean(errors)))
    print('Accuracy = {:0.2f}%.'.format(accuracy))

    return accuracy

best_random = dt_random.best_estimator_
random_accuracy = evaluate(best_random, X_test,Y_test)

print("Accuracy Scores of the Model ",random_accuracy)
best_parameters = (dt_random.best_params_['max_depth']);
print(best_parameters)

2 个答案:

答案 0 :(得分:0)

问题是要求提供默认值。尝试删除random_state = 1

当前行:

dt_reg = DecisionTreeRegressor(random_state=1)

更新行:

dt_reg = DecisionTreeRegressor()

我认为它应该工作!!!

答案 1 :(得分:0)

# ================================================================================
# Machine Learning Using Scikit-Learn | 3 | Decision Trees ================================================================================

import sklearn.datasets as datasets
import sklearn.model_selection as model_selection
import numpy as np
from sklearn.tree import DecisionTreeRegressor

np.random.seed(100)
# Load popular Boston dataset from sklearn.datasets module and assign it to variable boston.

boston = datasets.load_boston()

# print(boston)


# Split boston.data into two sets names X_train and X_test. Also, split boston.target into two sets Y_train and Y_test

X_train, X_test, Y_train, Y_test = model_selection.train_test_split(boston.data, boston.target,  random_state=30)
# Print the shape of X_train dataset
print(X_train.shape)

# Print the shape of X_test dataset.
print(X_test.shape)

# Build a Decision tree Regressor model from X_train set and Y_train labels, with default parameters. Name the model as dt_reg

dt_Regressor = DecisionTreeRegressor()

dt_reg = dt_Regressor.fit(X_train, Y_train)

print(dt_reg.score(X_train,Y_train))

print(dt_reg.score(X_test,Y_test))

predicted = dt_reg.predict(X_test[:2])
print(predicted)

# Get the max depth

maxdepth = 2
maxscore = 0
for x in range(2, 6):
     dt_Regressor = DecisionTreeRegressor(max_depth=x)
     dt_reg = dt_Regressor.fit(X_train, Y_train)
     score = dt_reg.score(X_test, Y_test)
     if(maxscore < score):
         maxdepth = x
         maxscore = score

打印(最大深度)