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

管线中LogisticReturnal的_coef值太多

  •  4
  • Christopher  · 技术社区  · 7 年前

    我在利用这些小熊猫 DataFrameMapper 在一个学习管道中。为了评估特征联合管道中的特征贡献,我喜欢测量估计器的系数(逻辑回归)。对于下面的代码示例,有三个文本内容列 a, b c X_train :

    import pandas as pd
    import numpy as np
    import pickle
    from sklearn_pandas import DataFrameMapper
    from sklearn.feature_extraction.text import CountVectorizer
    from sklearn.linear_model import LogisticRegression
    from sklearn.model_selection import train_test_split
    from sklearn.pipeline import Pipeline
    np.random.seed(1)
    
    data = pd.read_csv('https://pastebin.com/raw/WZHwqLWr')
    #data.columns
    
    X = data.copy()
    y = data.result
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=1)
    
    mapper = DataFrameMapper([
            ('a', CountVectorizer()),
            ('b', CountVectorizer()),
            ('c', CountVectorizer())
    ])
    
    pipeline = Pipeline([
            ('featurize', mapper),
            ('clf', LogisticRegression(random_state=1))
            ])
    
    pipeline.fit(X_train, y_train)
    y_pred = pipeline.predict(X_test)
    
    print(abs(pipeline.named_steps['clf'].coef_))
    #array([[0.3567311 , 0.3567311 , 0.46215153, 0.10542043, 0.3567311 ,
    #        0.46215153, 0.46215153, 0.3567311 , 0.3567311 , 0.3567311 ,
    #        0.3567311 , 0.46215153, 0.46215153, 0.3567311 , 0.46215153,
    #        0.3567311 , 0.3567311 , 0.3567311 , 0.3567311 , 0.46215153,
    #        0.46215153, 0.46215153, 0.3567311 , 0.3567311 ]])
    
    print(len(pipeline.named_steps['clf'].coef_[0]))
    #24
    

    b) 访问数据库的最佳方法是什么 coef_ 每个特征的值(“a”、“b”、“c”)?

    期望输出:

    a: coef_score (float)
    b: coef_score (float)
    c: coef_score (float)
    

    2 回复  |  直到 7 年前
        1
  •  6
  •   James Dellinger    7 年前

    虽然您最初的数据帧确实只包含三个特性的列 a , b c ,熊猫 DataFrameMapper() 课堂应用学习 CountVectorizer() 到每个列a、b和c的相应单词体。这导致总共创建了24个特性,然后将它们传递给您的用户 LogisticRegression() 分类器。这就是为什么在尝试访问分类器的 .coef_ 属性

    coeff_ A. ,

    原始数据帧如下所示:

                 a                   b                c   result
    2   here we go   hello here we are   this is a test        0
    73  here we go   hello here we are   this is a test        0
    ...
    

    如果我们运行下面一行,我们可以看到由 DataFrameMapper / 计数向量器() mapper 对象:

    pipeline.named_steps['featurize'].transformed_names_
    
    ['a_another',
     'a_example',
     'a_go',
     'a_here',
     'a_is',
     'a_we',
     'b_are',
     'b_column',
     'b_content',
     'b_every',
     'b_has',
     'b_hello',
     'b_here',
     'b_text',
     'b_we',
     'c_can',
     'c_deal',
     'c_feature',
     'c_how',
     'c_is',
     'c_test',
     'c_this',
     'c_union',
     'c_with']
    
    len(pipeline.named_steps['featurize'].transformed_names_)
    
    24
    

    B /

    col_names = list(data.drop(['result'], axis=1).columns.values)
    vect_feats = pipeline.named_steps['featurize'].transformed_names_
    clf_coef_scores = abs(pipeline.named_steps['clf'].coef_)
    
    def get_avg_coef_scores(col_names, vect_feats, clf_coef_scores):
        scores = {}
        start_pos = 0
        for n in col_names:
            num_vect_feats = len([i for i in vect_feats if i[0] == n])
            end_pos = start_pos + num_vect_feats
            scores[n + '_avg_coef_score'] = np.mean(clf_coef_scores[0][start_pos:end_pos])
            start_pos = end_pos
        return scores
    

    如果我们调用刚刚编写的函数,我们将得到以下输出:

    get_avg_coef_scores(col_names, vect_feats, clf_coef_scores)
    
    {'a_avg_coef_score': 0.3499861323284858,
     'b_avg_coef_score': 0.40358462487685853,
     'c_avg_coef_score': 0.3918712435073411}
    

    {key:clf_coef_scores[0][i] for i, key in enumerate(vect_feats)}
    
    {'a_another': 0.3567310993987888,
     'a_example': 0.3567310993987888,
     'a_go': 0.4621515317244458,
     'a_here': 0.10542043232565701,
     'a_is': 0.3567310993987888,
     'a_we': 0.4621515317244458,
     'b_are': 0.4621515317244458,
     'b_column': 0.3567310993987888,
     'b_content': 0.3567310993987888,
     'b_every': 0.3567310993987888,
     'b_has': 0.3567310993987888,
     'b_hello': 0.4621515317244458,
     'b_here': 0.4621515317244458,
     'b_text': 0.3567310993987888,
     'b_we': 0.4621515317244458,
     'c_can': 0.3567310993987888,
     'c_deal': 0.3567310993987888,
     'c_feature': 0.3567310993987888,
     'c_how': 0.3567310993987888,
     'c_is': 0.4621515317244458,
     'c_test': 0.4621515317244458,
     'c_this': 0.4621515317244458,
     'c_union': 0.3567310993987888,
     'c_with': 0.3567310993987888}
    
        2
  •  5
  •   Luca Massaron    7 年前

    在恢复安装的 DataFrameMapper Pipeline ,您可以使用 .features CountVectorizer 用于将字符串转换为一个热编码变量的函数。每个CountVectorIzer都有一个 .vocabulary_ 方法,它确切地告诉您字符串所代表的列。

    因此,您只需按顺序拉出每个 计数矢量器 数据帧映射器 对于它们中的每一个,按顺序提取表示输入矩阵中每一列的字符串。这将允许您拥有一个精确表示系数标签的序列。

    根据您的示例,此代码片段应该满足您的需要,我在上面详细描述了这一点(如果您遇到任何错误,请警告我,我将根据您的反馈进行更正):

    # recover the fitted mapper
    fitted_mapper = pipeline.named_steps['featurize'] 
    
    mapped_labels = list()
    # iterate through the CountVectorizers
    for label, fun in fitted_mapper.features:
        # Iterate through the sorted vocabulary
        for level, _ in sorted(fun.vocabulary_.items()):
            mapped_labels.append(label+'_'+level)
    
    # the ordered sequence of vectorized strings
    print(mapped_labels)
    
    # pick up the coefficients
    coefs = pipeline.named_steps['clf'].coef_[0]
    
    # pair mapped labels and coefs and print them
    for label, coef in zip(mapped_labels, coefs):
        print("%s:%0.5f" % (label, coef))
    
    推荐文章