Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python LIME功能解释产生无效密钥错误_Python_Python 3.x_Scikit Learn_Mlp - Fatal编程技术网

Python LIME功能解释产生无效密钥错误

Python LIME功能解释产生无效密钥错误,python,python-3.x,scikit-learn,mlp,Python,Python 3.x,Scikit Learn,Mlp,我有一个MLPrepressor,可以很好地处理我的数据集。以下是我的代码的精简版本,删除了一些不必要的内容: from sklearn.neural_network import MLPRegressor from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler, StandardScaler, RobustScaler from sklearn imp

我有一个MLPrepressor,可以很好地处理我的数据集。以下是我的代码的精简版本,删除了一些不必要的内容:

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.24685160714535

但是,我想看看这个模型中的功能重要性。我知道这并不总是神经网络的重点,但这是一个商业理由,所以这是必要的。我通过发现,我想使用它。因为这是回归,所以我试着遵循这个例子

因此,我添加了以下行:

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

category_features=np.argwhere(np.array([len(set)(data[:,x]))表示范围内的x(data.shape[1]))我需要首先将所有内容转换为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)

我按照要求将代码格式化为@Roganjoshi你颠倒了引号,但现在回溯的多行在一行上。这就是为什么你不应该首先将其作为引号:)这些真的是分类功能吗?由于您培训了MLP,这些功能可能不是分类的,但可能是单色编码的。Try now@roganjosh。它们不是,没有什么是分类的,只是尝试按照示例@RicHard
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)