【问题标题】:eli5: show_weights() with two labelseli5: show_weights() 有两个标签
【发布时间】:2019-01-10 13:46:15
【问题描述】:

我正在尝试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,还是我错过了一个统计点?我希望看到第一种情况的两个表格。 p>

【问题讨论】:

    标签: scikit-learn nlp regression


    【解决方案1】:

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

    看LogisticRegression的属性:

    coef_ : 数组,形状 (1, n_features) 或 (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_ : 数组,形状 (1,) 或 (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() 使用。

    希望这说明清楚。

    【讨论】:

    • 基本上没有办法输出两个类呢?因为我预计这两个类都有正负加载因子?
    • @Christopher 一个类的正权重成为另一类的负数。没有别的了。
    • 在这个逻辑中,为什么案例 #2 是三个表?为什么不是两张桌子?
    • @Christopher 因为多类案例被处理为 one-vs-rest,其中问题分为 3 个二元问题。每个班级一个。 See this
    • 我怎样才能将打印的列名从Weights更改为Scores
    猜你喜欢
    • 2020-06-14
    • 2020-06-12
    • 2021-01-06
    • 2016-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多