tfidfvectorizer - 如何解释 TfidfVectorizer 输出
问题描述
我正在做情感分析和从文本生成特征,我正在使用 TF-IDF 方法,但我无法解释输出。
我使用了 Sklearn 的 TfidfVectorizer 函数。
我使用了以下代码:
从 sklearn.feature_extraction.text 导入 TfidfVectorizer tfidf_vectorizer = TfidfVectorizer(max_df=0.90, min_df=2, max_features=1000, stop_words='english') tfidf = tfidf_vectorizer.fit_transform(combi['tidy_tweet'])
输出如下:
(0, 302) 0.46871135687055143 (0, 463) 0.5896490179849546 (0, 738) 0.6577413621857342 (1, 879) 0.3938403468675415 (1, 131) 0.6145629375807904 (1, 600) 0.6835218920644196 (2, 79) 1.0 (3, 557) 0.7040384885805177 (3 , 518) 0.44016705593507854 (3, 888) 0.5572995329862621 (4, 566) 1.0 (5, 423) 0.586120951905663 (5, 858) 0.4743403266916206 (5, 69) 0.4637175931713698 (5, 485) 0.4652198168550412 (6, 121) 0.809676118019697 (6, 894 ) 0.5868769751051355 (7, 749) 0.47546741144240784 (7, 992) 0.40382612331421974 (7, 283) 0.6221668428341786 (7, 883) 0.20713435439054187 (7, 393) 0.22953868678391207 (7, 432) 0.29836739781603
我可以理解最后一列是 TF-IDF 值,但其他列是什么。
解决方案
tfidfvectorizer
用于将数据转换为术语文档矩阵。
在上面的输出中,第一列中的条目,例如(0,302)
,0
表示提取的特征的索引,并且302
是该特定特征的数字符号。例如,考虑一个简单的数据框,如下所示:
col1
0 cat
1 dog
2 egg
3 god
4 cat
5 man
6 dog
要从上述数据中提取特征,
vect = TfidfVectorizer(stop_words='english', min_df=0, encoding = 'utf-8')
X = vect.fit_transform(df['col1'].values.astype('U'))
上述代码给出的输出如下:
the document term matrix of data is
(0, 0) 1.0
(1, 1) 1.0
(2, 2) 1.0
(3, 3) 1.0
(4, 0) 1.0
(5, 4) 1.0
(6, 1) 1.0
所有行的第一列中的第一个条目(即 0,1,2....6)只是提取特征的索引。第二个条目(即 0,1,2,3,0, 4,1) 表示特征的数字符号。您可以使用 tfidfvectorizer 的 get_feature_names 函数观察到这一点。
print(vect.get_feature_names()[0])
print(vect.get_feature_names()[1])
print(vect.get_feature_names()[2])
print(vect.get_feature_names()[4])
print(vect.get_feature_names()[3])
上面的代码给出的输出如下:
cat
dog
egg
man
god
这意味着对应于值 0 的特征是猫,1 是狗,4 是人……以此类推。
推荐阅读
- python - ping LAN 的 Python 函数未将结果存储在全局列表中
- javascript - 如何使用 jqueries .load() 设置样式或包含在 div 中的内容加载?另一个快速
- javascript - module.exports undefined 当需要 sequelize Model 类时,什么是正确的结构?
- c++ - PangoLayout 中忽略的新行
- java - 如何解决问题(用 java 中的突变体进行测试)?
- docsify - Docsify 上的 API 文档?
- javascript - 如何在点击 ReactJS 时调用两个箭头函数
- regex - 如何将文本文件中的字符串作为标记变量提取到电子表格中
- c++ - 如何处理非 default_constructible tyname 变量
- python - 如果您按屏幕上的任意位置,则会显示消息