利用隨機森林對特征重要性進行評估

前言

隨機森林是以決策樹為基學習器的集成學習算法。隨機森林非常簡單,易于實現(xiàn),計算開銷也很小,更令人驚奇的是它在分類和回歸上表現(xiàn)出了十分驚人的性能,因此,隨機森林也被譽為“代表集成學習技術(shù)水平的方法”。
本文是對隨機森林如何用在特征選擇上做一個簡單的介紹。

隨機森林(RF)簡介

只要了解決策樹的算法,那么隨機森林是相當容易理解的。隨機森林的算法可以用如下幾個步驟概括:

  1. 用有抽樣放回的方法(bootstrap)從樣本集中選取n個樣本作為一個訓練集
  2. 用抽樣得到的樣本集生成一棵決策樹。在生成的每一個結(jié)點:
    1. 隨機不重復地選擇d個特征
    2. 利用這d個特征分別對樣本集進行劃分,找到最佳的劃分特征(可用基尼系數(shù)、增益率或者信息增益判別)
  3. 重復步驟1到步驟2共k次,k即為隨機森林中決策樹的個數(shù)。
  4. 用訓練得到的隨機森林對測試樣本進行預測,并用票選法決定預測的結(jié)果。
    下圖比較直觀地展示了隨機森林算法:
圖1:隨機森林算法示意圖

沒錯,就是這個到處都是隨機取值的算法,在分類和回歸上有著極佳的效果,是不是覺得強的沒法解釋~
然而本文的重點不是這個,而是接下來的特征重要性評估。

特征重要性評估

sklearn 已經(jīng)幫我們封裝好了一切,我們只需要調(diào)用其中的函數(shù)即可。 我們以UCI上葡萄酒的例子為例,首先導入數(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']

然后,我們來大致看下這是一個怎么樣的數(shù)據(jù)集

import numpy as np
np.unique(df['Class label'])

輸出為

array([1, 2, 3], dtype=int64)

可見共有3個類別。然后再來看下數(shù)據(jù)的信息:

df.info()

輸出為:

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 178 entries, 0 to 177
Data columns (total 14 columns):
Class label                     178 non-null int64
Alcohol                         178 non-null float64
Malic acid                      178 non-null float64
Ash                             178 non-null float64
Alcalinity of ash               178 non-null float64
Magnesium                       178 non-null int64
Total phenols                   178 non-null float64
Flavanoids                      178 non-null float64
Nonflavanoid phenols            178 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

可見除去class label之外共有13個特征,數(shù)據(jù)集的大小為178。

按照常規(guī)做法,將數(shù)據(jù)集分為訓練集和測試集。此處注意:sklearn.cross_validation 模塊在0.18版本中被棄用,支持所有重構(gòu)的類和函數(shù)都被移動到了model_selection模塊。從sklearn.model_selection引入train_test_split

from sklearn.model_selection 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)

好了,這樣一來隨機森林就訓練好了,其中已經(jīng)把特征的重要性評估也做好了,我們拿出來看下。

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) Proline                        0.158610
 3) Flavanoids                     0.150948
 4) OD280/OD315 of diluted wines   0.131987
 5) Alcohol                        0.106589
 6) Hue                            0.078243
 7) Total phenols                  0.060718
 8) Alcalinity of ash              0.032033
 9) Malic acid                     0.025400
10) Proanthocyanins                0.022351
11) Magnesium                      0.022078
12) Nonflavanoid phenols           0.014645
13) Ash                            0.013916

對的就是這么方便。
如果要篩選出重要性比較高的變量的話,這么做就可以

threshold = 0.15
x_selected = x_train[:, importances > threshold]
x_selected.shape

輸出為

(124, 3)

這樣,幫我們選好了3個重要性大于0.15的特征。

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

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

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