# create a count vectorizer object
count_vectorizer = CountVectorizer()
# fit the count vectorizer using the text data
count_vectorizer.fit(data['text'])
# collect the vocabulary items used in the vectorizer
dictionary = count_vectorizer.vocabulary_.items()据我所知,在count_vectorizer适合data['text']之后,它会生成一系列特性。在我的例子中,它生成了25,257特性,当我调用count_vectorizer.vocabulary_时,这些特性被映射为dict数据类型。它仍然是25,257元组。它的意思是,它使用了所有的功能。
问题是,当我调用count_vectorizer.vocabulary_.items()时,它以dict_items的形式返回15,142元组。为什么这里的人数减少了?难道不应该使用所有的特性来制作dictionary吗?
下面是我要说的长度:
len(data['text']) #19579
len(count_vectorizer.get_feature_names()) #25257 items
len(count_vectorizer.vocabulary_) #25257 items
len(dictionary) #15142 items (??????)发布于 2020-03-26 21:56:12
对于未来的读者:正如Ben在评论中所写的那样,实际上真的很难让len(my_dict) != len(my_dict.items())。当发生这种行为时,进行一些例行检查总是一种很好的做法:
如果上述任何检查都不能解决这个问题,那么您就可以开始考虑一个bug、一个库的损坏安装或其他一些嵌套的问题。
https://datascience.stackexchange.com/questions/70288
复制相似问题