Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python CountVectorizer为单词提供错误计数?_Python_Scikit Learn_Nlp_Nltk_Countvectorizer - Fatal编程技术网

Python CountVectorizer为单词提供错误计数?

Python CountVectorizer为单词提供错误计数?,python,scikit-learn,nlp,nltk,countvectorizer,Python,Scikit Learn,Nlp,Nltk,Countvectorizer,假设我的文本文件由以下文本组成: 那只敏捷的棕色狐狸跳过了那些懒狗。及时缝一针可以省钱 九。快速的棕色缝线跳过了懒惰的时间。狐狸在 时间救狗 我想使用sk learn的countvectorier来获取文件中所有单词的单词数。(我知道还有其他方法可以做到这一点,但出于一些原因,我想使用CountVectorizer。)这是我的代码: from nltk.corpus import stopwords from sklearn.feature_extraction.text import Coun

假设我的文本文件由以下文本组成:

那只敏捷的棕色狐狸跳过了那些懒狗。及时缝一针可以省钱 九。快速的棕色缝线跳过了懒惰的时间。狐狸在 时间救狗

我想使用sk learn的countvectorier来获取文件中所有单词的单词数。(我知道还有其他方法可以做到这一点,但出于一些原因,我想使用CountVectorizer。)这是我的代码:

from nltk.corpus import stopwords
from sklearn.feature_extraction.text import CountVectorizer

text = input('Please enter the filepath for the text: ') 
text = open(text, 'r', encoding = 'utf-8')
tokens = CountVectorizer(analyzer = 'word', stop_words = 'english')


X = tokens.fit_transform(text)
dictionary = tokens.vocabulary_
除了当我调用
字典时,它给了我错误的计数:

>>> dictionary
{'time': 9, 'dog': 1, 'stitch': 8, 'quick': 6, 'lazy': 5, 'brown': 0, 'saves': 7, 'jumped': 4, 'fox': 3, 'dogs': 2}

有人能就我在这里犯的(无疑是明显的)错误提出建议吗?

词汇表
是术语到文档术语矩阵索引的dict/映射,而不是计数:

词汇表
:术语到特征索引的映射

X
实际上为您提供了特征索引和相应计数的矩阵

>>> for i in X:
...    print(i)
... 
  (0, 1)    1
  (0, 7)    2
  (0, 9)    3
  (0, 8)    2
  (0, 2)    1
  (0, 5)    2
  (0, 4)    2
  (0, 3)    2
  (0, 0)    2
  (0, 6)    2

e、 g.
9->“时间”
的计数是3。

啊,我明白了!真的非常感谢这个非常简洁的决议。