gpt4 book ai didi

python - LIME 功能解释产生无效 key 错误

转载 作者:行者123 更新时间:2023-12-02 00:13:48 25 4
gpt4 key购买 nike

我有一个非常适合我的数据集的 MLPRegressor。这是我的代码的修剪版本,删除了一些不必要的东西:

from sklearn.neural_network import MLPRegressor
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler, StandardScaler, RobustScaler
from sklearn import preprocessing
import pandas as pd
import numpy as np
from sklearn import tree
from sklearn.tree import export_graphviz
from datetime import datetime

def str_to_num(arr):
le = preprocessing.LabelEncoder()
new_arr = le.fit_transform(arr)
return new_arr

def compare_values(arr1, arr2):
thediff = 0
thediffs = []
for thing1, thing2 in zip(arr1, arr2):
thediff = abs(thing1 - thing2)
thediffs.append(thediff)

return thediffs

def minmaxscale(data):
scaler = MinMaxScaler()
df_scaled = pd.DataFrame(scaler.fit_transform(data), columns=data.columns)
return df_scaled

data = pd.read_csv('reg.csv')
label = data['TOTAL']
data = data.drop('TOTAL', axis=1)
data = minmaxscale(data)

mlp = MLPRegressor(
activation = 'tanh',
alpha = 0.005,
learning_rate = 'invscaling',
learning_rate_init = 0.01,
max_iter = 200,
momentum = 0.9,
solver = 'lbfgs',
warm_start = True
)

X_train, X_test, y_train, y_test = train_test_split(data, label, test_size = 0.2)
mlp.fit(X_train, y_train)
preds = mlp.predict(X_test)
score = compare_values(y_test, preds)
print("Score: ", np.average(score))

而且效果很好!生产:分数:7.246851606714535

但是,我想查看此模型中的特征重要性。我明白这并不总是神经网络的重点,但这是商业理由,所以有必要。我发现 LIME通过 LIME Paper我想使用它。由于这是回归,我尝试遵循示例 here

所以我添加了以下几行:

categorical_features = np.argwhere(np.array([len(set(data[:,x])) for x in range(data.shape[1])]) <= 10).flatten()

explainer = lime.lime_tabular.LimeTabularExplainer(
X_train,
feature_names=X_train.columns,
class_names=['TOTAL'],
verbose=True,
categorical_features = categorical_features,
mode='regression')

但是现在得到错误:

Traceback (most recent call last):
File "c:\Users\jerry\Desktop\mlp2.py", line 65, in <module>
categorical_features = np.argwhere(np.array([len(set(data[:,x])) for x in range(data.shape[1])]) <= 10).flatten()
File "c:\Users\J39304\Desktop\mlp2.py", line 65, in <listcomp>
categorical_features = np.argwhere(np.array([len(set(data[:,x])) for x in range(data.shape[1])]) <= 10).flatten()
File "C:\Python35-32\lib\site-packages\pandas\core\frame.py", line 2927, in __getitem__
indexer = self.columns.get_loc(key)
File "C:\Python35-32\lib\site-packages\pandas\core\indexes\base.py", line 2657, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 108, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 110, in pandas._libs.index.IndexEngine.get_loc
TypeError: '(slice(None, None, None), 0)' is an invalid key

为什么会出现此错误,我该怎么办?我不明白如何正确集成 LIME。

I see others have had this issue, but I don't know how to fix

最佳答案

我需要先将所有内容转换为 numpy 数组:

class_names = X_train.columns
X_train = X_train.to_numpy()
X_test = X_test.to_numpy()
y_train = y_train.to_numpy()
y_test = y_test.to_numpy()

然后从那里,将其提供给解释器:

explainer = lime.lime_tabular.LimeTabularExplainer(
X_train,
feature_names=class_names,
class_names=['TOTAL'],
verbose=True,
mode='regression')

exp = explainer.explain_instance(X_test[5], mlp.predict)
exp = exp.as_list()
print(exp)

关于python - LIME 功能解释产生无效 key 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57717423/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com