【问题标题】:How to vectorize a list of words python?如何矢量化单词列表python?
【发布时间】:2017-08-01 13:02:44
【问题描述】:

我正在尝试将 CountVectorizer 模块与 Sci-kit Learn 一起使用。根据我的阅读,它似乎可以用于句子列表,例如:

['这是第一个文档。','这是第二个第二个文档。','还有第三个。','这是第一个文档吗?']

但是,有没有办法对列表形式的单词集合进行向量化,例如 [['this', 'is', 'text', 'document', 'to', 'analyze'], [' and', 'this', 'is', 'the', 'second'],['and', 'this', 'and', 'that', 'are', 'third']?

我正在尝试使用' '.join(wordList) 将每个列表转换为一个句子,但出现错误:

TypeError:序列项 13329:预期的字符串或 Unicode,生成器 找到了

当我尝试运行时:

vectorizer = CountVectorizer(min_df=50)
ratings = vectorizer.fit_transform([' '.join(wordList)]) 

谢谢!

【问题讨论】:

    标签: python machine-learning scikit-learn nlp


    【解决方案1】:

    我猜你需要这样做:

    counts = vectorizer.fit_transform(wordList)  # sparse matrix with columns corresponding to words
    words = vectorizer.get_feature_names()  # array with words corresponding to columns
    

    最后,得到[['this', 'is', 'text', 'document', 'to', 'analyze']]

    sample_idx = 1
    sample_words = [words[i] for i, count in 
                    enumerate(counts.toarray()[sample_idx]) if count > 0]
    

    【讨论】:

      猜你喜欢
      • 2017-07-04
      • 2021-05-06
      • 1970-01-01
      • 2013-03-29
      • 2019-01-27
      • 2013-01-05
      • 1970-01-01
      • 2013-08-01
      • 2020-11-12
      相关资源
      最近更新 更多