Multi label encoding for classes with duplicates

 ̄綄美尐妖づ 提交于 2020-07-08 13:34:11

问题


How can I n-hot encode a column of lists with duplicates?

Something like MultiLabelBinarizer from sklearn which counts the number of instances of duplicate classes instead of binarizing.

Example input:

x = pd.Series([['a', 'b', 'a'], ['b', 'c'], ['c','c']])

Expected output:

    a   b   c
0   2   1   0
1   0   1   1
2   0   0   2

回答1:


I have written a new class MultiLabelCounter based on the MultiLabelBinarizer code.

import itertools
import numpy as np

class MultiLabelCounter():
    def __init__(self, classes=None):
        self.classes_ = classes

    def fit(self,y):
        self.classes_ = sorted(set(itertools.chain.from_iterable(y)))
        self.mapping = dict(zip(self.classes_,
                                         range(len(self.classes_))))
        return self

    def transform(self,y):
        yt = []
        for labels in y:
            data = [0]*len(self.classes_)
            for label in labels:
                data[self.mapping[label]] +=1
            yt.append(data)
        return yt

    def fit_transform(self,y):
        return self.fit(y).transform(y)
import pandas as pd
x = pd.Series([['a', 'b', 'a'], ['b', 'c'], ['c','c']])

mlc = MultiLabelCounter()
mlc.fit_transform(x)

# [[2, 1, 0], [0, 1, 1], [0, 0, 2]]


来源:https://stackoverflow.com/questions/57371409/multi-label-encoding-for-classes-with-duplicates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!