在Python中使用Keras的神经网络中的特征重要性图 [英] Feature Importance Chart in neural network using Keras in Python

查看:1732
本文介绍了在Python中使用Keras的神经网络中的特征重要性图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python(3.6)anaconda(64位)spyder(3.1.2).我已经使用keras(2.0.6)设置了一个回归问题(一个响应,10个变量)的神经网络模型.我想知道如何生成像这样的功能重要性图表:

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)

推荐答案

我最近正在寻找该问题的答案,发现一些对我正在做的事情有用的东西,并认为分享会有所帮助.我最终使用了排列重要性 eli5程序包中的模块.它最容易与scikit学习模型一起使用.幸运的是,Keras为顺序模型提供了包装器.如下面的代码所示,使用起来非常简单.

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

这篇关于在Python中使用Keras的神经网络中的特征重要性图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆