#调整随机森林的参数(调整n_estimators随机森林中树的数量默认10个树,精度递增显著)

from sklearn import datasets
X, y = datasets.make_classification(n_samples=10000,n_features=20,n_informative=15,flip_y=.5, weights=[.2, .8])

import numpy as np
training = np.random.choice([True, False], p=[.8, .2],size=y.shape)


from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import confusion_matrix


n_estimator_params = range(1, 100,5)
confusion_matrixes = {}
for n_estimator in n_estimator_params:
    rf = RandomForestClassifier(n_estimators=n_estimator,n_jobs=-1, verbose=True)
    rf.fit(X[training], y[training])
    print ("Accuracy:\t", (rf.predict(X[~training]) == y[~training]).mean())

'''
======================== RESTART: E:/python/pp138.py ========================
[Parallel(n_jobs=-1)]: Done   1 out of   1 | elapsed:    0.0s finished
[Parallel(n_jobs=1)]: Done   1 out of   1 | elapsed:    0.0s finished
Accuracy:     0.590083456063
[Parallel(n_jobs=-1)]: Done   6 out of   6 | elapsed:    0.1s finished
[Parallel(n_jobs=2)]: Done   6 out of   6 | elapsed:    0.0s finished
Accuracy:     0.618065783014
[Parallel(n_jobs=-1)]: Done  11 out of  11 | elapsed:    0.3s finished
[Parallel(n_jobs=2)]: Done  11 out of  11 | elapsed:    0.0s finished
Accuracy:     0.682866961217
[Parallel(n_jobs=-1)]: Done  16 out of  16 | elapsed:    0.5s finished
[Parallel(n_jobs=2)]: Done  16 out of  16 | elapsed:    0.0s finished
Accuracy:     0.692194403535
[Parallel(n_jobs=-1)]: Done  21 out of  21 | elapsed:    0.6s finished
[Parallel(n_jobs=2)]: Done  21 out of  21 | elapsed:    0.0s finished
Accuracy:     0.702012763868
[Parallel(n_jobs=-1)]: Done  26 out of  26 | elapsed:    0.9s finished
[Parallel(n_jobs=2)]: Done  26 out of  26 | elapsed:    0.0s finished
Accuracy:     0.697594501718
[Parallel(n_jobs=-1)]: Done  31 out of  31 | elapsed:    1.0s finished
[Parallel(n_jobs=2)]: Done  31 out of  31 | elapsed:    0.0s finished
Accuracy:     0.710358370152
[Parallel(n_jobs=-1)]: Done  36 out of  36 | elapsed:    1.1s finished
[Parallel(n_jobs=2)]: Done  36 out of  36 | elapsed:    0.0s finished
Accuracy:     0.704958271969
[Parallel(n_jobs=-1)]: Done  41 out of  41 | elapsed:    1.3s finished
[Parallel(n_jobs=2)]: Done  41 out of  41 | elapsed:    0.0s finished
Accuracy:     0.707412862052
[Parallel(n_jobs=-1)]: Done  46 out of  46 | elapsed:    1.5s finished
[Parallel(n_jobs=2)]: Done  46 out of  46 | elapsed:    0.0s finished
Accuracy:     0.716740304369
[Parallel(n_jobs=-1)]: Done  46 tasks      | elapsed:    1.6s
[Parallel(n_jobs=-1)]: Done  51 out of  51 | elapsed:    1.8s finished
[Parallel(n_jobs=2)]: Done  46 tasks      | elapsed:    0.0s
[Parallel(n_jobs=2)]: Done  51 out of  51 | elapsed:    0.0s finished
Accuracy:     0.713303878252
[Parallel(n_jobs=-1)]: Done  46 tasks      | elapsed:    1.5s
[Parallel(n_jobs=-1)]: Done  56 out of  56 | elapsed:    1.8s finished
[Parallel(n_jobs=2)]: Done  46 tasks      | elapsed:    0.0s
[Parallel(n_jobs=2)]: Done  56 out of  56 | elapsed:    0.0s finished
Accuracy:     0.713303878252
[Parallel(n_jobs=-1)]: Done  46 tasks      | elapsed:    1.5s
[Parallel(n_jobs=-1)]: Done  61 out of  61 | elapsed:    2.0s finished
[Parallel(n_jobs=2)]: Done  46 tasks      | elapsed:    0.0s
[Parallel(n_jobs=2)]: Done  61 out of  61 | elapsed:    0.0s finished
Accuracy:     0.717231222386
[Parallel(n_jobs=-1)]: Done  46 tasks      | elapsed:    1.5s
[Parallel(n_jobs=-1)]: Done  66 out of  66 | elapsed:    2.3s finished
[Parallel(n_jobs=2)]: Done  46 tasks      | elapsed:    0.0s
[Parallel(n_jobs=2)]: Done  66 out of  66 | elapsed:    0.0s finished
Accuracy:     0.711340206186
[Parallel(n_jobs=-1)]: Done  46 tasks      | elapsed:    1.6s
[Parallel(n_jobs=-1)]: Done  71 out of  71 | elapsed:    2.5s finished
[Parallel(n_jobs=2)]: Done  46 tasks      | elapsed:    0.0s
[Parallel(n_jobs=2)]: Done  71 out of  71 | elapsed:    0.0s finished
Accuracy:     0.720667648503
[Parallel(n_jobs=-1)]: Done  46 tasks      | elapsed:    1.5s
[Parallel(n_jobs=-1)]: Done  76 out of  76 | elapsed:    2.4s finished
[Parallel(n_jobs=2)]: Done  46 tasks      | elapsed:    0.0s
[Parallel(n_jobs=2)]: Done  76 out of  76 | elapsed:    0.0s finished
Accuracy:     0.721649484536
[Parallel(n_jobs=-1)]: Done  46 tasks      | elapsed:    1.7s
[Parallel(n_jobs=-1)]: Done  81 out of  81 | elapsed:    3.0s finished
[Parallel(n_jobs=2)]: Done  46 tasks      | elapsed:    0.0s
[Parallel(n_jobs=2)]: Done  81 out of  81 | elapsed:    0.0s finished
Accuracy:     0.721649484536
[Parallel(n_jobs=-1)]: Done  46 tasks      | elapsed:    1.5s
[Parallel(n_jobs=-1)]: Done  86 out of  86 | elapsed:    2.8s finished
[Parallel(n_jobs=2)]: Done  46 tasks      | elapsed:    0.0s
[Parallel(n_jobs=2)]: Done  86 out of  86 | elapsed:    0.0s finished
Accuracy:     0.716740304369
[Parallel(n_jobs=-1)]: Done  46 tasks      | elapsed:    1.5s
[Parallel(n_jobs=-1)]: Done  91 out of  91 | elapsed:    3.1s finished
[Parallel(n_jobs=2)]: Done  46 tasks      | elapsed:    0.0s
[Parallel(n_jobs=2)]: Done  91 out of  91 | elapsed:    0.0s finished
Accuracy:     0.72410407462
[Parallel(n_jobs=-1)]: Done  46 tasks      | elapsed:    1.4s
[Parallel(n_jobs=-1)]: Done  96 out of  96 | elapsed:    3.1s finished
[Parallel(n_jobs=2)]: Done  46 tasks      | elapsed:    0.0s
[Parallel(n_jobs=2)]: Done  96 out of  96 | elapsed:    0.0s finished
Accuracy:     0.718213058419
'''

 

posted on 2016-03-31 18:36  qqhfeng16  阅读(15703)  评论(0编辑  收藏  举报