【问题标题】:Getting counts in MultiLabelBinarizer在 MultiLabelBinarizer 中获取计数
【发布时间】:2019-10-15 18:14:20
【问题描述】:

如何获取 MultiLabelBinarizer 中的项目计数?

import pandas as pd
from sklearn.preprocessing import MultiLabelBinarizer
mlb = MultiLabelBinarizer()

pd.DataFrame(mlb.fit_transform([(1,1,2), (3,3,2,5)]),columns=mlb.classes_)

Out[0]: 
   1  2  3  5
0  1  1  0  0
1  0  1  1  1

我想要得到的不是这个,而是

Out[0]: 
   1  2  3  5
0  2  1  0  0
1  0  1  2  1

因为 1 在第 1 行重复了 2 次,而 3 在第 2 行重复了 2 次​​p>

【问题讨论】:

    标签: python-3.x machine-learning scikit-learn data-manipulation


    【解决方案1】:
    from collections import Counter
    
    data = [(1,1,2), (3,3,2,5)]
    pd.DataFrame([Counter(x) for x in data]).fillna(0)
    

    输出:

        1       2   3       5
    0   2.0     1   0.0     0.0
    1   0.0     1   2.0     1.0
    

    【讨论】:

      猜你喜欢
      • 2018-10-20
      • 2017-09-01
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 2014-03-10
      • 2020-09-20
      • 2020-02-19
      相关资源
      最近更新 更多