ELI5 explain_weights 和 explain_predictions 作为 pandas DataFrame

Posted

技术标签:

【中文标题】ELI5 explain_weights 和 explain_predictions 作为 pandas DataFrame【英文标题】:ELI5 explain_weights and explain_predictions as pandas DataFrame 【发布时间】:2019-06-21 16:52:06 【问题描述】:

如何将 ELI5 explain_weightsexplain_prediction 保存为 pandas DataFrame?

下面的示例玩具代码。

# Load the dataset as DataFrame
import pandas as pd
from sklearn.datasets import load_iris
df = pd.DataFrame(load_iris().data, columns=load_iris().feature_names)
df['label'] = load_iris().target

# Divide to X and y and split to train and test sets
X = df.iloc[:,0:4].values
y = df.iloc[:,4].values
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

# Define the neural network model
from keras.models import Sequential
from keras.layers import Dense
def baseline_model():
    model = Sequential()
    model.add(Dense(units=8, activation='relu', input_dim=4))
    model.add(Dense(units=3, activation='sigmoid'))
    model.compile(optimizer='adam', loss='categorical_crossentropy', metrics='accuracy'])
    return model

# Wrap the model
from keras.wrappers.scikit_learn import KerasClassifier
my_model = KerasClassifier(build_fn=baseline_model)
my_model.fit(X_train, y_train, batch_size=4, epochs=100, verbose=0)

# Run eli5 explanations
import eli5
from eli5.sklearn import PermutationImportance
perm = PermutationImportance(my_model, random_state=1).fit(X_test, y_test)

# This actually works!
explanation = eli5.formatters.as_dataframe.explain_weights_df(perm, 
feature_names=df.columns[:-1].tolist())

# But this line does not "work", i.e. it produces a NoneType object
explanation_pred = 
eli5.formatters.as_dataframe.explain_prediction_df(estimator=my_model, 
doc=X_test[0])

【问题讨论】:

你有没有找到解决这个问题的方法??我遇到了同样的问题 【参考方案1】:

试试这个。它对我有用。

explanation_pred = eli5.explain_prediction_df(estimator=my_model, doc=X_test.iloc[0])

【讨论】:

这对我不起作用它仍然返回 nonetype 对象【参考方案2】:

如果您仍在寻找答案。 您只需要替换以下代码行:

explanation_pred = eli5.formatters.as_dataframe.explain_prediction_df(estimator=my_model, 
doc=X_test[0])

到:

explanation_pred = eli5.explain_prediction_df(estimator=my_model, doc=X_test[0])

【讨论】:

这对我不起作用它仍然返回 nonetype 对象

以上是关于ELI5 explain_weights 和 explain_predictions 作为 pandas DataFrame的主要内容,如果未能解决你的问题,请参考以下文章

eli5: show_weights() 有两个标签

eli5 解释预测 XGBoost 模型

eli5:show_weights()有两个标签

如何在 python 中将 eli5.show_weights 转换为数组/列表

ML之PFI(eli5):基于mpg汽车油耗数据集利用RF随机森林算法和PFI置换特征重要性算法实现模型特征可解释性排序

EX2010与EX2013共存迁移02-EX2010环境搭建