我设法用GridSearchCV
from sklearn.datasets import fetch_covtype
from sklearn.ensemble import RandomForestClassifier
from sklearn import cross_validation
from sklearn import grid_search
import numpy as np
covtype = fetch_covtype()
clf = RandomForestClassifier()
X_train, X_test, y_train, y_test = cross_validation.train_test_split(covtype.data,
covtype.target,
test_size=0.33,
random_state=42)
params = {'n_estimators':[30, 50, 100],
'max_features':['sqrt', 'log2', 10]}
gsv = grid_search.GridSearchCV(clf, params, cv=3,
n_jobs=-1, scoring='f1')
gsv.fit(X_train, y_train)
print metrics.classification_report(y_train, gsv.best_estimator_.predict(X_train))
print metrics.classification_report(y_test, gsv.best_estimator_.predict(X_test))
输出让你的模型很好的改善:
precision recall f1-score support
1 1.00 1.00 1.00 141862
2 1.00 1.00 1.00 189778
3 1.00 1.00 1.00 24058
4 1.00 1.00 1.00 1872
5 1.00 1.00 1.00 6268
6 1.00 1.00 1.00 11605
7 1.00 1.00 1.00 13835
avg/total 1.00 1.00 1.00 389278
precision recall f1-score support
1 0.97 0.95 0.96 69978
2 0.95 0.97 0.96 93523
3 0.95 0.96 0.95 11696
4 0.92 0.86 0.89 875
5 0.94 0.78 0.86 3225
6 0.94 0.90 0.92 5762
7 0.97 0.95 0.96 6675
avg/total 0.96 0.96 0.96 191734
这是不是太遥远的Kaggle leaderboard分数(请注意,Kaggle比赛采用的是更具挑战性的数据拆分,但!)
如果你想看到更多的改进,那么你将不得不考虑的不平课程以及如何最好地选择您的培训数据。
注意
我用估计的数量较少比我会通常以节省时间,但是在训练集中表现不错的机型,所以你可能没有考虑这一点。
我使用了一小部分max_features
,因为通常这会减少模型训练中的偏差。虽然这并非总是如此。
我用f1
得分,因为我不太了解数据集,并且f1
在分类问题上工作得很好。
我试过你的代码,并且还打印出了n_estimators = 100和max_features = 10的最佳参数(best_params_)。然后,我调整我的代码以使用这些参数,并且还添加了参数scoring ='f1_weighted'。不幸的是,我仍然得到同样糟糕的结果。 任何想法? clf = RandomForestClassifier(n_estimators = 100,max_features = 10) scores = cross_validation.cross_val_score(clf,covtype.data,covtype.target,scoring ='f1_weighted') –