我正在尝试 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']
但是,当使用三个标签时,它也会输出三个表。
categories = ['alt.atheism', 'soc.religion.christian', 'comp.graphics']
它在第一个输出中错过了 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/