scikit-learn - eli5 : show_weights() with two labels

标签 scikit-learn nlp regression

我正在尝试 eli5为了理解术语对某些类别的预测的贡献。

你可以运行这个脚本:

import numpy as np
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.datasets import fetch_20newsgroups

#categories = ['alt.atheism', 'soc.religion.christian']
categories = ['alt.atheism', 'soc.religion.christian', 'comp.graphics']

np.random.seed(1)
train = fetch_20newsgroups(subset='train', categories=categories, shuffle=True, random_state=7)
test = fetch_20newsgroups(subset='test', categories=categories, shuffle=True, random_state=7)

bow_model = CountVectorizer(stop_words='english')
clf = LogisticRegression()
pipel = Pipeline([('bow', bow),
                 ('classifier', clf)])

pipel.fit(train.data, train.target)

import eli5
eli5.show_weights(clf, vec=bow, top=20)

问题:

使用两个标签时,不幸的是输出仅限于一张表:
categories = ['alt.atheism', 'soc.religion.christian']

Image 1

但是,当使用三个标签时,它也会输出三个表。
categories = ['alt.atheism', 'soc.religion.christian', 'comp.graphics']

enter image description here

它在第一个输出中错过了 y=0 是软件中的错误,还是我错过了统计点?对于第一种情况,我希望看到两个表。

最佳答案

这与 eli5 无关,而是与 scikit-learn(在本例中为 LogisticRegression() )处理两个类别的方式有关。对于只有两个类别,问题变成了一个二元类别,因此从学习的分类器中只返回一列属性。

查看LogisticRegression的属性:

coef_ : array, shape (1, n_features) or (n_classes, n_features)

Coefficient of the features in the decision function.
coef_ is of shape (1, n_features) when the given problem is binary.

intercept_ : array, shape (1,) or (n_classes,)

Intercept (a.k.a. bias) added to the decision function.

If fit_intercept is set to False, the intercept is set to zero.
intercept_ is of shape(1,) when the problem is binary.

coef_形状(1, n_features)当二进制。此 coef_eli5.show_weights() 使用.

希望这能说清楚。

关于scikit-learn - eli5 : show_weights() with two labels,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51659523/

相关文章:

python - pip 安装 sklearn openmp

python - 使用 GridSearchCV 时需要拆分数据吗?

nlp - 使用 NLTK 处理复合词(2-grams)

machine-learning - Keras 的 Tokenizer 与 sklearn 的 CountVectorizer

用于标记英文文本的正则表达式

r - 使用 lm()、nls()(和 glm()?)估计马尔萨斯增长模型中的人口增长率

带矩阵的 Python 回归

R:有理函数回归

python - 测试准确率较低但 AUC 分数较高的可能原因

python - 如何将架构组合传递给 MLPClassifier?