- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 SHAP 对我的产品分类模型进行一些不良案例分析。我的数据看起来像这样:
corpus_train, corpus_test, y_train, y_test = train_test_split(data['Name_Description'],
data['Category_Target'],
test_size = 0.2,
random_state=8)
vectorizer = TfidfVectorizer(stop_words='english', ngram_range=(1, 3), min_df=3, analyzer='word')
X_train = vectorizer.fit_transform(corpus_train)
X_test = vectorizer.transform(corpus_test)
model = LogisticRegression(max_iter=200)
model.fit(X_train, y_train)
X_train_sample = shap.sample(X_train, 100)
X_test_sample = shap.sample(X_test, 20)
masker = shap.maskers.Independent(data=X_test_sample)
explainer = shap.LinearExplainer(model, masker=masker)
shap_values = explainer.shap_values(X_test_sample)
X_test_array = X_test_sample.toarray()
shap.summary_plot(shap_values, X_test_array, feature_names=vectorizer.get_feature_names(), class_names=data['Category'].unique())
现在为了节省空间,我没有包含实际的摘要图,但它看起来不错。我的问题是我希望能够分析单个预测并按照以下思路获得更多信息:
换句话说,我想知道哪些特定词对预测的贡献最大。但是当我运行上图中单元格 36 中的代码时,我得到了一个
AttributeError: 'numpy.ndarray' object has no attribute 'output_names'
我仍然对 shap_values 的索引感到困惑。我该如何解决这个问题?
最佳答案
我找不到 SHAP 的解决方案,但我找到了使用 LIME 的解决方案。下面的代码显示了一个非常相似的输出,从中可以很容易地看出模型是如何进行预测的,以及某些词的贡献有多大。
c = make_pipeline(vectorizer, classifier)
# saving a list of strings version of the X_test object
ls_X_test= list(corpus_test)
# saving the class names in a dictionary to increase interpretability
class_names = list(data.Category.unique())
# Create the LIME explainer
# add the class names for interpretability
LIME_explainer = LimeTextExplainer(class_names=class_names)
# explain the chosen prediction
# use the probability results of the logistic regression
# can also add num_features parameter to reduce the number of features explained
LIME_exp = LIME_explainer.explain_instance(ls_X_test[idx], c.predict_proba)
LIME_exp.show_in_notebook(text=True, predict_proba=True)
关于python - 使用 SHAP 值解释 LogisticRegression 分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66606563/
sklearn 中的逻辑回归类带有 L1 和 L2 正则化。如何关闭正则化以获得“原始”逻辑拟合,例如 Matlab 中的 glmfit?我想我可以设置 C = large number 但我不认为这
from sklearn.feature_extraction.text import CountVectorizer vectorizer = CountVectorizer() train_mat
我有一组生成的数据,以 CSV 格式描述 Web 连接,如下所示: conn_duration,conn_destination,response_size,response_code,is_mali
我正在尝试将逻辑回归模型拟合到 sklearn 的鸢尾花数据集。我得到一条看起来太平坦的概率曲线,也就是系数太小。我希望萼片长度 > 7 的概率超过 90%: 这条概率曲线真的错了吗?如果是这样,是什
我正在使用 sklearn 包中的 LogisticRegression,并且有一个关于分类的快速问题。我为我的分类器构建了一条 ROC 曲线,结果证明我的训练数据的最佳阈值约为 0.25。我假设创建
为什么不同的求解器会导致 sklearn 中简单问题的学习权重不同?这闻起来不像数值不稳定——看起来不同的求解器旨在收敛于不同的权重集。怎么回事? 这似乎与正则化有关,因为效果随着 C 的增加而消失
我正在研究 LogisticRegression 模型并尝试进行调试。 这是一件简单的事情,但似乎无法让它发挥作用:只需 time of day和一个state 0 or 1 ,并想要预测一天中给定时
有人告诉我,在处理多元逻辑回归时,您想在 X 矩阵中为模型截距添加一列 1。使用sklearn的模型时,模块会自动添加1的列吗? 最佳答案 LinearRegression 模块有一个 fit_int
scikit-learn 的 sklearn.linear_model.LogisticRegression 类如何处理回归和分类问题? 如 Wikipedia page 上所示以及许多来源,由于逻辑
我正在尝试使用 scikit-learn 0.12.1 来: 训练逻辑回归分类器 根据保留的验证数据评估分类器 向该分类器提供新数据并为每个观察检索 5 个最可能的标签 Sklearn 使所有这一切变
我正在尝试使用 SHAP 对我的产品分类模型进行一些不良案例分析。我的数据看起来像这样: corpus_train, corpus_test, y_train, y_test = train_test
我是新手,我必须根据 De Pauw 和 Wagacha (1998) 方法(基本上是 char n-grams 上的 maxent)对词典中的单词进行分类。数据非常大(500 000 个条目和数百万
我正在查看 Spark 1.5 dataframe/row api和 implementation对于逻辑回归。据我了解,其中的 train 方法首先将 dataframe 转换为 RDD[Label
Sklearn 的 LogisticRegression 模型拒绝并行运行。我设置了 n_jobs=-1,也尝试了 n_jobs=4。运气不好——只有一个核心参与。我并行运行了其他 sklearn 模
我正在尝试使用 python 中的 doc2vec 将用户输入文本分为两类。我有以下代码来训练模型,然后对输入文本进行分类。问题是,我找不到任何对字符串进行分类的方法。我是新手,所以请忽略错误。 这里
sklearn LogisticRegression 中带有 l1 惩罚的默认 alpha 是多少? lmMod = LogisticRegression(penalty='l1') 最佳答案 根据d
我正在尝试通过 sklearn 运行逻辑回归: from sklearn.model_selection import train_test_split from sklearn.linear_mod
似乎在 scikit-learn 中实现的 LogisticRegression 无法学习简单的 bool 函数 AND 或 OR。我会理解 XOR 给出不好的结果,但 AND 和 OR 应该没问题。
我正在尝试使用 LogisticRegression 预测 Twitter 的人口统计属性,但我在预测中收到“File "Anaconda\lib\site-packages\sklearn\line
在使用 sklearn.linear_model.LogisticRegression 拟合训练数据集后,我想获得训练数据集和交叉验证数据集的成本函数值。 是否可以让 sklearn 简单地给我它最小
我是一名优秀的程序员,十分优秀!