Python機(jī)器學(xué)習(xí)利用隨機(jī)森林對(duì)特征重要性計(jì)算評(píng)估
1
隨機(jī)森林是以決策樹(shù)為基學(xué)習(xí)器的集成學(xué)習(xí)算法。隨機(jī)森林非常簡(jiǎn)單,易于實(shí)現(xiàn),計(jì)算開(kāi)銷也很小,更令人驚奇的是它在分類和回歸上表現(xiàn)出了十分驚人的性能,因此,隨機(jī)森林也被譽(yù)為“代表集成學(xué)習(xí)技術(shù)水平的方法”。
2 隨機(jī)森林(RF)簡(jiǎn)介
只要了解決策樹(shù)的算法,那么隨機(jī)森林是相當(dāng)容易理解的。隨機(jī)森林的算法可以用如下幾個(gè)步驟概括:
1.用有抽樣放回的方法(bootstrap)從樣本集中選取n個(gè)樣本作為一個(gè)訓(xùn)練集
2.用抽樣得到的樣本集生成一棵決策樹(shù)。在生成的每一個(gè)結(jié)點(diǎn):
- 隨機(jī)不重復(fù)地選擇d個(gè)特征
- 利用這d個(gè)特征分別對(duì)樣本集進(jìn)行劃分,找到最佳的劃分特征(可用基尼系數(shù)、增益率或者信息增益判別)
3.重復(fù)步驟1到步驟2共k次,k即為隨機(jī)森林中決策樹(shù)的個(gè)數(shù)。
4.用訓(xùn)練得到的隨機(jī)森林對(duì)測(cè)試樣本進(jìn)行預(yù)測(cè),并用票選法決定預(yù)測(cè)的結(jié)果。
下圖比較直觀地展示了隨機(jī)森林算法(圖片出自文獻(xiàn)2):

圖1:隨機(jī)森林算法示意圖
沒(méi)錯(cuò),就是這個(gè)到處都是隨機(jī)取值的算法,在分類和回歸上有著極佳的效果,是不是覺(jué)得強(qiáng)的沒(méi)法解釋~
然而本文的重點(diǎn)不是這個(gè),而是接下來(lái)的特征重要性評(píng)估。
3 特征重要性評(píng)估
現(xiàn)實(shí)情況下,一個(gè)數(shù)據(jù)集中往往有成百上前個(gè)特征,如何在其中選擇比結(jié)果影響最大的那幾個(gè)特征,以此來(lái)縮減建立模型時(shí)的特征數(shù)是我們比較關(guān)心的問(wèn)題。這樣的方法其實(shí)很多,比如主成分分析,lasso等等。不過(guò),這里我們要介紹的是用隨機(jī)森林來(lái)對(duì)進(jìn)行特征篩選。
用隨機(jī)森林進(jìn)行特征重要性評(píng)估的思想其實(shí)很簡(jiǎn)單,說(shuō)白了就是看看每個(gè)特征在隨機(jī)森林中的每顆樹(shù)上做了多大的貢獻(xiàn),然后取個(gè)平均值,最后比一比特征之間的貢獻(xiàn)大小。
好了,那么這個(gè)貢獻(xiàn)是怎么一個(gè)說(shuō)法呢?通??梢杂没嶂笖?shù)(Gini index)或者袋外數(shù)據(jù)(OOB)錯(cuò)誤率作為評(píng)價(jià)指標(biāo)來(lái)衡量。
我們這里只介紹用基尼指數(shù)來(lái)評(píng)價(jià)的方法,想了解另一種方法的可以參考文獻(xiàn)2。


4 舉個(gè)例子
值得慶幸的是, sklearn已經(jīng)幫我們封裝好了一切,我們只需要調(diào)用其中的函數(shù)即可。
我們以UCI上葡萄酒的例子為例,首先導(dǎo)入數(shù)據(jù)集。
import pandas as pd url = 'http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data' df = pd.read_csv(url, header = None) df.columns = ['Class label', 'Alcohol', 'Malic acid', 'Ash', 'Alcalinity of ash', 'Magnesium', 'Total phenols', 'Flavanoids', 'Nonflavanoid phenols', 'Proanthocyanins', 'Color intensity', 'Hue', 'OD280/OD315 of diluted wines', 'Proline']
然后,我們來(lái)大致看下這時(shí)一個(gè)怎么樣的數(shù)據(jù)集
import numpy as np np.unique(df['Class label'])
輸出為
array([1, 2, 3], dtype=int64)
可見(jiàn)共有3個(gè)類別。然后再來(lái)看下數(shù)據(jù)的信息:
df.info()
輸出為
<class 'pandas.core.frame.DataFrame'> RangeIndex: 178 entries, 0 to 177 Data columns (total 14 columns): Class label178 non-null int64 Alcohol 178 non-null float64 Malic acid 178 non-null float64 Ash 178 non-null float64 Alcalinity of ash178 non-null float64 Magnesium 178 non-null int64 Total phenols 178 non-null float64 Flavanoids 178 non-null float64 Nonflavanoid phenols178 non-null float64 Proanthocyanins 178 non-null float64 Color intensity 178 non-null float64 Hue 178 non-null float64 OD280/OD315 of diluted wines 178 non-null float64 Proline 178 non-null int64 dtypes: float64(11), int64(3) memory usage: 19.5 KB
可見(jiàn)除去class label之外共有13個(gè)特征,數(shù)據(jù)集的大小為178。
按照常規(guī)做法,將數(shù)據(jù)集分為訓(xùn)練集和測(cè)試集。
from sklearn.cross_validation import train_test_split from sklearn.ensemble import RandomForestClassifier x, y = df.iloc[:, 1:].values, df.iloc[:, 0].values x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.3, random_state = 0) feat_labels = df.columns[1:] forest = RandomForestClassifier(n_estimators=10000, random_state=0, n_jobs=-1) forest.fit(x_train, y_train)
好了,這樣一來(lái)隨機(jī)森林就訓(xùn)練好了,其中已經(jīng)把特征的重要性評(píng)估也做好了,我們拿出來(lái)看下。
importances = forest.feature_importances_
indices = np.argsort(importances)[::-1]
for f in range(x_train.shape[1]):
print("%2d) %-*s %f" % (f + 1, 30, feat_labels[indices[f]], importances[indices[f]]))
輸出的結(jié)果為
1) Color intensity 0.182483 2) Proline0.158610 3) Flavanoids0.150948 4) OD280/OD315 of diluted wines0.131987 5) Alcohol0.106589 6) Hue0.078243 7) Total phenols0.060718 8) Alcalinity of ash 0.032033 9) Malic acid0.025400 10) Proanthocyanins 0.022351 11) Magnesium 0.022078 12) Nonflavanoid phenols 0.014645 13) Ash0.013916
對(duì)的就是這么方便。
如果要篩選出重要性比較高的變量的話,這么做就可以
threshold = 0.15 x_selected = x_train[:, importances > threshold] x_selected.shape
輸出為
(124, 3)
瞧,這不,幫我們選好了3個(gè)重要性大于0.15的特征了嗎~
5 參考文獻(xiàn)
[1] Raschka S. Python Machine Learning[M]. Packt Publishing, 2015.
[2] 楊凱, 侯艷, 李康. 隨機(jī)森林變量重要性評(píng)分及其研究進(jìn)展[J]. 2015.
以上就是Python機(jī)器學(xué)習(xí)利用隨機(jī)森林對(duì)特征重要性計(jì)算評(píng)估的詳細(xì)內(nèi)容,更多關(guān)于Python隨機(jī)森林重要性計(jì)算的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信