代码之家  ›  专栏  ›  技术社区  ›  Christopher

eli5:使用两个标签显示“权重”()

  •  1
  • Christopher  · 技术社区  · 8 年前

    我在努力 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是软件中的一个bug,还是我漏掉了一个统计点? 我希望第一个案子有两张桌子。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Vivek Kumar    8 年前

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

    查看LogisticRecovery的属性:

    系数:数组、形状(1,n_特征)或(n_类,n_特征)

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

    截距:数组,形状(1,)或(n_类,)

    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) 当二进制时。这个 系数_ eli5.show_weights() .

    希望这能说明问题。