虽然您最初的数据帧确实只包含三个特性的列
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}