【问题标题】:countvectorizer not able to detect , wordscountvectorizer 无法检测到单词
【发布时间】:2022-01-05 00:35:48
【问题描述】:
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 ???

【问题讨论】:

    标签: python nlp countvectorizer


    【解决方案1】:

    您的CountVectorizer 缺少两件事:

    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())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      • 2019-01-14
      • 2020-06-11
      • 1970-01-01
      • 2018-08-29
      • 2020-11-30
      • 2014-08-14
      相关资源
      最近更新 更多