问题描述
from keras.wrappers.scikit_learn import KerasClassifier, KerasRegressor
import eli5
from eli5.sklearn import PermutationImportance
model = Sequential()
model.add(LSTM(units=30,return_sequences= True, input_shape=(X.shape[1],421)))
model.add(Dropout(rate=0.2))
model.add(LSTM(units=30, return_sequences=True))
model.add(LSTM(units=30))
model.add(Dense(units=1, activation='relu'))
perm = PermutationImportance(model, scoring='accuracy',random_state=1).fit(X, y, epochs=500, batch_size=8)
eli5.show_weights(perm, feature_names = X.columns.tolist())
因此,我运行LSTM只是为了查看包含400多个要素的数据集的要素重要性.我使用Keras scikitlearn包装器来使用eli5的PermutationImportance函数.但是代码返回此错误"ValueError:找到的数组为暗3.估计器预期< = 2.".
So I am running an LSTM just to see the feature importance of my dataset containing 400+ features. I used the Keras scikitlearn wrapper to use eli5's PermutationImportance function. But the code is returning this error "ValueError: Found array with dim 3. Estimator expected <= 2.".
如果我使用model.fit(),则代码可以平稳运行,但无法调试置换重要性的错误.有人知道怎么了吗?
The code runs smoothly if I use model.fit() but can't debug the error of the permutation importance. Anyone know what is wrong?
推荐答案
eli5
的scikitlearn
实现(用于确定置换重要性)只能处理2d数组,而keras
'LSTM
层则需要3d数组.此错误是已知问题,但似乎尚无解决方案.
eli5
's scikitlearn
implementation for determining permutation importance can only process 2d arrays while keras
' LSTM
layers require 3d arrays. This error is a known issue but there appears to be no solution yet.
我知道这并不能真正回答您使eli5
与LSTM配合使用的问题(因为当前无法使用),但是我遇到了相同的问题,并使用了另一个名为 SHAP
以了解我的LSTM模型的功能重要性.这是我的一些代码,可帮助您入门:
I understand this does not really answer your question of getting eli5
to work with LSTM (because it currently can't), but I encountered the same problem and used another library called SHAP
to get the feature importance of my LSTM model. Here is some of my code to help you get started:
import shap
DE = shap.DeepExplainer(model, X_train) # X_train is 3d numpy.ndarray
shap_values = DE.shap_values(X_validate_np, check_additivity=False) # X_validate is 3d numpy.ndarray
shap.initjs()
shap.summary_plot(
shap_values[0],
X_validate,
feature_names=list_of_your_columns_here,
max_display=50,
plot_type='bar')
以下是您可以获取的图形示例:
Here is an example of the graph which you can get:
希望这会有所帮助.
这篇关于有关LSTM Keras上置换重要性的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!