【问题标题】:Vectorizing a list of lists with sklearn learn?使用 sklearn learn 对列表列表进行矢量化?
【发布时间】:2020-01-15 21:00:45
【问题描述】:

我正在尝试将 sklearn 中的 CountVectorizer 与列表一起使用。

Lst=[['apple','peach','mango'],['apple','apple','mango']]

我希望输出返回每个列表中的字数。 例如:

0:apple:1
0:peach:1
0:mango:1

1:apple:2
1:peach:0
1:mango:1

或任何其他格式。

我发现这篇文章与我的相似,但答案并不完整。

How should I vectorize the following list of lists with scikit learn?

感谢任何帮助。

【问题讨论】:

    标签: python countvectorizer


    【解决方案1】:

    试试这个,使用Counter

    >>> from collections import Counter
    >>> lst=[['apple','peach','mango'],['apple','apple','mango']]
    

    输出:

    >>> {i:Counter(v) for i,v in enumerate(lst)}
    {0: Counter({'apple': 1, 'peach': 1, 'mango': 1}),
     1: Counter({'apple': 2, 'mango': 1})}
    

    获得预期的格式(在列表中)

    >>> [[i, obj, count] for i,v in enumerate(lst) for obj,count in Counter(v).items()]
    [[0, 'apple', 1],
     [0, 'peach', 1],
     [0, 'mango', 1],
     [1, 'apple', 2],
     [1, 'mango', 1]]
    

    【讨论】:

      猜你喜欢
      • 2014-08-06
      • 2015-02-24
      • 1970-01-01
      • 2021-05-06
      • 1970-01-01
      • 2015-07-10
      • 1970-01-01
      • 2019-06-18
      • 2017-05-29
      相关资源
      最近更新 更多