【问题标题】:Unable to calculate Model performance for Decision Tree Regressor无法计算决策树回归器的模型性能
【发布时间】:2020-09-18 00:46:44
【问题描述】:

虽然我的代码在 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)

【问题讨论】:

  • 请注意,术语“准确度”仅对分类有意义,在回归中无意义(编辑标题)。

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


【解决方案1】:

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

当前行:

dt_reg = DecisionTreeRegressor(random_state=1)

更新行:

dt_reg = DecisionTreeRegressor()

我认为它应该工作!!!

【讨论】:

    【解决方案2】:
    # ================================================================================
    # 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
    

    打印(最大深度)

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 2020-03-03
      • 2019-10-02
      • 2022-11-29
      • 1970-01-01
      • 2018-03-03
      • 2018-08-14
      • 2018-09-02
      • 2015-05-10
      相关资源
      最近更新 更多