【问题标题】:How to return the file number from bag of words如何从词袋中返回文件号
【发布时间】:2017-01-01 22:27:49
【问题描述】:

我正在使用来自 sklearn 的CountVectorizer,我想知道我将如何访问或提取文件号,这些我尝试 最喜欢的:(1 ,12 ) 1 我只想要代表文件编号的 1

from sklearn.feature_extraction.text import CountVectorizer
vectorizer=CountVectorizer()
string1="these is my first statment in vectorizer"
string2="hello every one i like the place here"
string3="i am going to school every day day like the student in my school"
email_list=[string1,string2,string3]
bagofword=vectorizer.fit(email_list)
bagofword=vectorizer.transform(email_list)
print(bagofword)
output:
(0, 3)  1
(0, 7)  1
(0, 8)  1
(0, 10) 1
(0, 14) 1
(1, 12) 1
(1, 16) 1
(2, 0)  1
(2, 1)  2

【问题讨论】:

  • file number 是什么意思?例如,如果您想要文件 1 的功能,您可以使用 bagofword[1,:],如果您将 .todense() 应用于它,则可以更容易地可视化,matrix([[0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0]])
  • @rth ,我的意思是文档的订单号(1,12),它代表文档编号 1 中出现的特征编号 12,我想为每个特征计算所有文档编号,其中包含相同的功能

标签: scikit-learn


【解决方案1】:

您可以使用,遍历稀疏数组的列,

features_map = [col.indices.tolist() for col in bagofword.T]

要获取包含特征k 的所有文档的列表,只需获取此列表的元素k

例如,features_map[2] == [1, 2] 表示功能编号 2,存在于文档 1 和 2 中。

【讨论】:

  • ,谢谢它对我有用,但你的意思是 features_map[2]=[1.2] 是功能编号 2 出现在文档 1 和 2 中。
  • @SMO 是的,谢谢,抱歉,这是答案中的错字。
猜你喜欢
  • 2013-03-08
  • 2020-02-14
  • 2018-05-17
  • 2021-05-20
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
  • 2013-03-09
  • 1970-01-01
相关资源
最近更新 更多