【问题标题】:NLP: Apply CountVectorizer to column containing a list of featuresNLP:将 CountVectorizer 应用于包含特征列表的列
【发布时间】:2020-08-18 09:54:20
【问题描述】:

我想将CountVectorizer 应用于包含单词和短语列表的列。换句话说,语料库不是一个字符串,而是一个列表。问题是CountVectorizer 或我遇到的任何其他相关函数都需要一个字符串作为输入。将列表加入一个字符串并进行标记是没有意义的,因为某些短语包含 2 个单词。有任何想法吗?

示例:

ID      corpus
1       ["Harry Potter","Batman"]
2       ["Batman", "Superman", "Lord of the Rings"]

想要的结果:

ID   Harry Potter    Batman    Superman    Lord of the Rings
1    1               1         0           0
2    0               1         1           1

【问题讨论】:

    标签: python scikit-learn nlp countvectorizer


    【解决方案1】:

    由于您已经对句子进行了标记,因此可能不需要CountVectorizer

    我写了MultiLabelCounter()here,可以解决你的问题。

    import pandas as pd
    x = [["Harry Potter","Batman"], ["Batman", "Superman", "Lord of the Rings"]]
    
    mlc = MultiLabelCounter()
    mlc.fit_transform(x)
    # [[1, 1, 0, 0], [1, 0, 1, 1]]
    
    mlc.classes_
    # ['Batman', 'Harry Potter', 'Lord of the Rings', 'Superman']
    

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 2018-05-04
      • 1970-01-01
      • 2021-04-28
      • 2020-06-16
      • 2018-07-19
      • 2019-10-23
      • 2019-05-26
      • 1970-01-01
      相关资源
      最近更新 更多