【发布时间】:2018-02-22 06:26:06
【问题描述】:
我试图在DecisionTreeClassifier 上运行GridSearchCV,唯一的超参数是max_depth。我运行它的两个版本是:
max_depth = range(1,20)
best_estimator_ attribute 显示 max_depth 为 15,而评分函数在测试集上显示为 0.8880
max_depth = range(1,15)
best_estimator_ attribute 的 max_depth 为 10,得分更高,为 0.8907。
我的问题是,如果 GridSearchCV 给出更好的分数,为什么不第一次选择 10 的 max_depth?
代码如下:
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import make_scorer
clf = tree.DecisionTreeClassifier(random_state=7)
parameters = {"max_depth": range(1,20), "random_state":[7]}
scorer = make_scorer(fbeta_score,beta=0.5)
grid_obj = GridSearchCV(estimator=clf,param_grid=parameters,scoring=scorer)
grid_fit =grid_obj.fit(X_train,y_train)
best_clf = grid_fit.best_estimator_
predictions = (clf.fit(X_train, y_train)).predict(X_test)
best_predictions = best_clf.predict(X_test)
# Report the before-and-afterscores
print best_clf
print "\nOptimized Model\n------"
print "Final accuracy score on the testing data:
{:.4f}".format(accuracy_score(y_test, best_predictions))
print "Final F-score on the testing data: {:.4f}".format(fbeta_score(y_test,
best_predictions, beta = 0.5))
【问题讨论】:
-
你是不是把其他问题删了再重新添加,把cmets都扔掉了?
-
我做到了。除了两个 cmets 要求我添加代码之外,没有什么可以“扔掉”的,我照做了。
-
所以你删除它是为了让我看起来你确实在你的问题中准备了一些东西?再一次,你呈现的代码没有可重现的
X_train, y_train。
标签: python scikit-learn grid-search