2017-02-21 74 views
0

您好,我使用xgboost中的feature_importance繪製了一個圖。但是,圖表返回「f值」。我不知道圖中代表哪個特徵。我聽說如何解決這個問題的一種方法是將我的數據框中的要素索引映射到feature_importance「f-值」的索引並手動選擇列。我如何去做這件事?此外,如果在這樣做的另一種方式,幫助將真正意識到:將專長重要性的索引映射到數據框中的列索引

這裏是我下面的代碼:

feature_importance = pd.Series(model.booster().get_fscore()).sort_values(ascending=False) 
feature_importance.plot(kind='bar', title='Feature Importances') 
plt.ylabel('Feature Importance Score') 

這裏是圖: enter image description here

print(feature_importance.head()) 

Output: 
f20  320 
f22  85 
f29  67 
f34  38 
f81  20 
+0

嘿@jezrael。想你也可以看看我的! –

+0

以上檢查iI添加了@jezrael –

+0

不錯,謝謝。現在'f20','f22'映射了一些'dict'?還是另一個數據框? – jezrael

回答

0

我試着一個簡單的例子來看看這裏是什麼,這裏是我寫的代碼:

import pandas as pd 
import xgboost as xgb 
import numpy as np 

%matplotlib inline 
import matplotlib.pyplot as plt 

model = xgb.XGBRegressor() 

size = 100 

data = pd.DataFrame([], columns=['a','b','c','target']) 
data['a'] = np.random.rand(size) 
data['b'] = np.random.rand(size) 
data['c'] = np.random.rand(size) 

data['target'] = np.random.rand(size)*data['a'] + data['b'] 

model.fit(data.drop('target',1), data.target) 

feature_importance = pd.Series(model.booster().get_fscore()).sort_values(ascending=False) 
feature_importance.plot(kind='bar', title='Feature Importances') 
plt.ylabel('Feature Importance Score') 

結果是:

enter image description here

正如你看到的標籤罰款。

現在,讓我們傳遞一個數組,而不是一個數據幀:

model.fit(np.array(data.drop('target',1)), data.target) 

feature_importance = pd.Series(model.booster().get_fscore()).sort_values(ascending=False) 
feature_importance.plot(kind='bar', title='Feature Importances') 
plt.ylabel('Feature Importance Score') 

enter image description here

因此您的問題,一個np.array在默認情況下沒有索引/列名,因此xgboost使默認的功能名稱(f0,f1,...,fn)