隨機森林參數(shù)調(diào)優(yōu)及特征重要性

特征重要性

#檢測重要特征rf = RandomForestClassifier()

rf.fit(X, y)

f, ax = plt.subplots(figsize=(7, 5))

ax.bar(range(len(rf.feature_importances_)),rf.feature_importances_)

ax.set_title("Feature Importances")

f.show()

#每個例子屬于哪個類的概率probs = rf.predict_proba(X)import pandas as pd

probs_df = pd.DataFrame(probs, columns=['0', '1'])

probs_df['was_correct'] = rf.predict(X) == yimport matplotlib.pyplot as plt

f, ax = plt.subplots(figsize=(7, 5))

probs_df.groupby('0').was_correct.mean().plot(kind='bar', ax=ax)

ax.set_title("Accuracy at 0 class probability")

ax.set_ylabel("% Correct")

ax.set_xlabel("% trees for 0")

f.show()

特征重要性

forest=RandomForestClassifier(n_estimators=10,n_jobs=-1,random_state=9)

forest.fit(x_train,y_train)

importances=forest.feature_importances_

print('每個維度對應(yīng)的重要性因子:\n',importances)

indices = np.argsort(importances)[::-1]# a[::-1]讓a逆序輸出print('得到按維度重要性因子排序的維度的序號:\n',indices)

most_import = indices[:3]#取最總要的3個print(x_train[:,most_import])

特征重要性

from sklearn.datasets import load_boston

from sklearn.ensemble import RandomForestRegressor

import numpy as np

#Load boston housing dataset as an example

boston = load_boston()

X = boston["data"]

print type(X),X.shape

Y = boston["target"]

names = boston["feature_names"]

print names

rf = RandomForestRegressor()

rf.fit(X, Y)

print "Features sorted by their score:"

print sorted(zip(map(lambda x: round(x, 4), rf.feature_importances_), names), reverse=True)

參數(shù)調(diào)優(yōu)

param_test1= {'n_estimators':list(range(10,71,10))}? ? #對參數(shù)'n_estimators'進行網(wǎng)格調(diào)參

gsearch1= GridSearchCV(estimator = RandomForestClassifier(min_samples_split=100,min_samples_leaf=20,max_depth=8,max_features='sqrt' ,random_state=10), param_grid =param_test1, scoring='roc_auc',cv=5)?

gsearch1.fit(X,y)?

gsearch1.grid_scores_,gsearch1.best_params_, gsearch1.best_score_? ? #輸出調(diào)參結(jié)果,并返回最優(yōu)下的參數(shù)

#輸出結(jié)果如下:

([mean:0.80681, std: 0.02236, params: {'n_estimators': 10},?

? mean: 0.81600, std: 0.03275, params:{'n_estimators': 20},?

? mean: 0.81818, std: 0.03136, params:{'n_estimators': 30},?

? mean: 0.81838, std: 0.03118, params:{'n_estimators': 40},?

? mean: 0.82034, std: 0.03001, params:{'n_estimators': 50},?

? mean: 0.82113, std: 0.02966, params:{'n_estimators': 60},?

? mean: 0.81992, std: 0.02836, params:{'n_estimators': 70}],?

{'n_estimators':60},? 0.8211334476626017)

#多個特征的網(wǎng)格搜索,如下所示

param_test2= {'max_depth':list(range(3,14,2)),'min_samples_split':list(range(50,201,20))}?

gsearch2= GridSearchCV(estimator = RandomForestClassifier(n_estimators= 60, min_samples_leaf=20,max_features='sqrt' ,oob_score=True,random_state=10),? param_grid = param_test2,scoring='roc_auc',iid=False, cv=5)?

gsearch2.fit(X,y)?

gsearch2.grid_scores_,gsearch2.best_params_, gsearch2.best_score_?

#通過查看袋外準(zhǔn)確率(oob)來判別參數(shù)調(diào)整前后準(zhǔn)確率的變化情況

rf1= RandomForestClassifier(n_estimators= 60, max_depth=13, min_samples_split=110,? min_samples_leaf=20,max_features='sqrt' ,oob_score=True,random_state=10)?

rf1.fit(X,y)?

print(rf1.oob_score_)? ?

#通過每次對1-3個特征進行網(wǎng)格搜索,重復(fù)此過程直到遍歷每個特征,并得到最終的調(diào)參結(jié)果。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容