xgboost特徵重要性指標: weight, gain, cover

官方解釋

Python中的xgboost可以通過get_fscore獲取特徵重要性,先看看官方對於這個方法的說明:

get_score(fmap=’’, importance_type=‘weight’)

Get feature importance of each feature. Importance type can be defined as:

  • ‘weight’: the number of times a feature is used to split the data across all trees.
  • ‘gain’: the average gain across all splits the feature is used in.
  • ‘cover’: the average coverage across all splits the feature is used in.
  • ‘total_gain’: the total gain across all splits the feature is used in.
  • ‘total_cover’: the total coverage across all splits the feature is used in.

看釋義不直觀,下面通過訓練一個簡單的模型,輸出這些重要性指標,再結合釋義進行解釋。

代碼實踐

首先構造10個樣例的樣本,每個樣例有兩維特徵,標籤爲0或1,二分類問題:

import numpy as np

sample_num = 10
feature_num = 2

np.random.seed(0)
data = np.random.randn(sample_num, feature_num)
np.random.seed(0)
label = np.random.randint(0, 2, sample_num)

輸出data和label:

# data:
array([[ 1.76405235,  0.40015721],
       [ 0.97873798,  2.2408932 ],
       [ 1.86755799, -0.97727788],
       [ 0.95008842, -0.15135721],
       [-0.10321885,  0.4105985 ],
       [ 0.14404357,  1.45427351],
       [ 0.76103773,  0.12167502],
       [ 0.44386323,  0.33367433],
       [ 1.49407907, -0.20515826],
       [ 0.3130677 , -0.85409574]])
# label:
array([0, 1, 1, 0, 1, 1, 1, 1, 1, 1])

訓練,這裏爲了便於下面計算,將樹深度設爲3(‘max_depth’: 3),只用一棵樹(num_boost_round=1):

import xgboost as xgb

train_data = xgb.DMatrix(data, label=label)
params = {'max_depth': 3}
bst = xgb.train(params, train_data, num_boost_round=1)

輸出重要性指標:

for importance_type in ('weight', 'gain', 'cover', 'total_gain', 'total_cover'):
    print('%s: ' % importance_type, bst.get_score(importance_type=importance_type))

結果:

weight:  {'f0': 1, 'f1': 2}
gain:  {'f0': 0.265151441, 'f1': 0.375000015}
cover:  {'f0': 10.0, 'f1': 4.0}
total_gain:  {'f0': 0.265151441, 'f1': 0.75000003}
total_cover:  {'f0': 10.0, 'f1': 8.0}

畫出唯一的一棵樹圖:

xgb.to_graphviz(bst, num_trees=0)

在這裏插入圖片描述

下面就結合這張圖,解釋下各指標含義:

  1. weight: {‘f0’: 1, ‘f1’: 2}
    在所有樹中,某特徵被用來分裂節點的次數,在本例中,可見分裂第1個節點時用到f0,分裂第2,3個節點時用到f1,所以weight_f0 = 1, weight_f1 = 2。
  2. total_cover: {‘f0’: 10.0, ‘f1’: 8.0}
    第1個節點,f0被用來對所有10個樣例進行分裂,之後的節點中f0沒再被用到,所以f0的total_cover爲10.0,此時f0 >= 0.855563045的樣例有5個,落入右子樹;
    第2個節點,f1被用來對上面落入右子樹的5個樣例進行分裂,其中f1 >= -0.178257734的樣例有3個,落入右子樹;
    第3個節點,f1被用來對上面落入右子樹的3個樣例進行分裂。
    總結起來,f0在第1個節點分裂了10個樣例,所以total_cover_f0 = 10,f1在第2、3個節點分別用於分裂5、3個樣例,所以total_cover_f1 = 5 + 3 = 8。total_cover表示在所有樹中,某特徵在每次分裂節點時處理(覆蓋)的所有樣例的數量。
  3. cover: {‘f0’: 10.0, ‘f1’: 4.0}
    cover = total_cover / weight,在本例中,cover_f0 = 10 / 1,cover_f1 = 8 / 2 = 4.
  4. total_gain: {‘f0’: 0.265151441, ‘f1’: 0.75000003}
    在所有樹中,某特徵在每次分裂節點時帶來的總增益,如果用熵或基尼不純衡量分裂前後的信息量分別爲i0和i1,則增益爲(i0 - i1)。
  5. gain: {‘f0’: 0.265151441, ‘f1’: 0.375000015}
    gain = total_gain / weight,在本例中,gain_f0 = 0.265151441 / 1,gain_f1 = 75000003 / 2 = 375000015.

在平時的使用中,多用total_gain來對特徵重要性進行排序。

By The Way

構造xgboost分類器還有另外一種方式,這種方式類似於sklearn中的分類器,採用fit, transform形式訓練模型:

from xgboost import XGBClassifier

cls = XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
       colsample_bytree=1, gamma=0, learning_rate=0.07, max_delta_step=0,
       max_depth=3, min_child_weight=1, missing=None, n_estimators=300,
       n_jobs=1, nthread=None, objective='binary:logistic', random_state=0,
       reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
       silent=True, subsample=1)
# 訓練模型
# cls.fit(data, label)

採用下面的方式獲取特徵重要性指標:

for importance_type in ('weight', 'gain', 'cover', 'total_gain', 'total_cover'):
    print('%s: ' % importance_type, cls.get_booster().get_score(importance_type=importance_type))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章