[英]Feature Importance Chart in neural network using Keras in Python
我正在使用 python(3.6) anaconda(64 位)spyder (3.1.2)。 我已經使用 keras (2.0.6) 為回歸問題(一個響應,10 個變量)設置了一個神經網絡模型。 我想知道如何生成像這樣的特征重要性圖表:
def base_model():
model = Sequential()
model.add(Dense(200, input_dim=10, kernel_initializer='normal', activation='relu'))
model.add(Dense(1, kernel_initializer='normal'))
model.compile(loss='mean_squared_error', optimizer = 'adam')
return model
clf = KerasRegressor(build_fn=base_model, epochs=100, batch_size=5,verbose=0)
clf.fit(X_train,Y_train)
我最近在尋找這個問題的答案,發現了一些對我正在做的事情有用的東西,並認為它會有所幫助。 我最終使用了eli5 包中的排列重要性模塊。 它最容易與 scikit-learn 模型一起使用。 幸運的是,Keras為順序模型提供了一個包裝器。 如下代碼所示,使用起來非常簡單。
from keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
import eli5
from eli5.sklearn import PermutationImportance
def base_model():
model = Sequential()
...
return model
X = ...
y = ...
my_model = KerasRegressor(build_fn=base_model, **sk_params)
my_model.fit(X,y)
perm = PermutationImportance(my_model, random_state=1).fit(X,y)
eli5.show_weights(perm, feature_names = X.columns.tolist())
這是一篇相對較舊的帖子,答案相對較舊,因此我想提供另一個建議,即使用SHAP
確定 Keras 模型的特征重要性。 SHAP
提供對 2d 和 3d 數組的支持,而eli5
目前僅支持 2d 數組(因此,如果您的模型使用需要 3d 輸入的層,如LSTM
或GRU
, eli5
將不起作用)。
以下是SHAP
如何繪制Keras
模型的特征重要性示例的鏈接,但以防它被破壞,下面還提供了一些示例代碼和圖表(取自上述鏈接):
import shap
# load your data here, e.g. X and y
# create and fit your model here
# load JS visualization code to notebook
shap.initjs()
# explain the model's predictions using SHAP
# (same syntax works for LightGBM, CatBoost, scikit-learn and spark models)
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X)
# visualize the first prediction's explanation (use matplotlib=True to avoid Javascript)
shap.force_plot(explainer.expected_value, shap_values[0,:], X.iloc[0,:])
shap.summary_plot(shap_values, X, plot_type="bar")
目前 Keras 不提供任何功能來提取特征重要性。
您可以查看之前的問題: Keras:有什么方法可以獲得可變的重要性?
或相關的 GoogleGroup:特征重要性
劇透:在 GoogleGroup 中,有人宣布了一個開源項目來解決這個問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.