【问题标题】:ELI5 explain_weights and explain_predictions as pandas DataFrameELI5 explain_weights 和 explain_predictions 作为 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])

【问题讨论】:

  • 你有没有找到解决这个问题的方法??我遇到了同样的问题

标签: pandas keras scikit-learn


【解决方案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 对象
猜你喜欢
  • 2020-04-02
  • 1970-01-01
  • 2021-12-01
  • 2018-01-01
  • 2022-12-22
  • 2017-10-08
  • 1970-01-01
  • 2017-02-22
相关资源
最近更新 更多