countvectorizer 无法检测到,单词

countvectorizer not able to detect , words

final_vocab = {'Amazon',
'Big Bazaar',
'Brand Factory',
'Central',
'Cleartrip',
'Dominos',
'Flipkart',
'IRCTC',
'Lenskart',
'Lifestyle',
'MAX',
'MMT',
'More',
'Myntra'}
 
vect = CountVectorizer(vocabulary=final_vocab)
token_df = pd.DataFrame(vect.fit_transform(['Big Bazaar','Brand Factory']).todense(), columns=vect.get_feature_names())

为什么所有的输出都是零???对于 Big Bazaar 和品牌工厂值应该是 1 ???

您的 CountVectorizer 缺少 2 个东西:

  1. ngram_range=(2,2) 如文档中所述:All values of n such such that min_n <= n <= max_n will be used。这有助于 CountVectorizer 从输入中获得 2 克向量(Big Bazaar 而不是 ['Big','Bazaar']
  2. lowercase=False 表示:Convert all characters to lowercase before tokenizing。这将使 Big BazaarBrand Factory 变成小写,从而无法在词汇表中找到。设置为 False 将防止这种情况发生。

此外,因为您已经为 CountVectorizer 提供了词汇表,请使用 transform 而不是 fit_transform

from sklearn.feature_extraction.text import CountVectorizer

final_vocab = ['Amazon',
'Big Bazaar',
'Brand Factory',
'Central',
'Cleartrip',
'Dominos',
'Flipkart',
'IRCTC',
'Lenskart',
'Lifestyle',
'MAX',
'MMT',
'More',
'Myntra']
 
vect = CountVectorizer(vocabulary=final_vocab, ngram_range=(2, 2), lowercase=False)
token_df = pd.DataFrame(vect.transform(['Big Bazaar','Brand Factory']).todense(), columns=vect.get_feature_names())