python特征重要性_在Python中使用Keras的神经网络中的特征重要性图

I am using python(3.6) anaconda (64 bit) spyder (3.1.2). I already set a neural network model using keras (2.0.6) for a regression problem(one response, 10 variables). I was wondering how can I generate feature importance chart like so:

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)

解决方案

I was recently looking for the answer to this question and found something that was useful for what I was doing and thought it would be helpful to share. I ended up using a permutation importance module from the eli5 package. It most easily works with a scikit-learn model. Luckily, Keras provides a wrapper for sequential models. As shown in the code below, using it is very straightforward.

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())

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值