Python 编码";“n个标签中的k个”;sklearn/pandas中的功能

Python 编码";“n个标签中的k个”;sklearn/pandas中的功能,python,pandas,scikit-learn,Python,Pandas,Scikit Learn,我有一个功能是一组标签的子集 >>> labels = ['ini', '', 'pdf', 'flac', 'php'] >>> data = [random.sample(labels, random.randint(0, len(labels))) for _ in range(20)] >>> data[:5] [['pdf'], [], ['pdf', 'flac'], ['php', 'pdf', 'ini'], ['', 'ph

我有一个功能是一组标签的子集

>>> labels = ['ini', '', 'pdf', 'flac', 'php']
>>> data = [random.sample(labels, random.randint(0, len(labels))) for _ in range(20)]
>>> data[:5]
[['pdf'], [], ['pdf', 'flac'], ['php', 'pdf', 'ini'], ['', 'php', 'ini']]
我需要一个“n取k编码器”来编码这个功能。我尝试使用/破解OneHotEncoder、LabelEncoder、get_假人,但无法很好地表示这些数据。可能无法提前知道标签集

在纯python中,(缓慢的)实现可能是-

>>>> feature_space = sorted(list(set(sum(data, []))))
>>>> data2 = [[int(c in row) for c in feature_space] for row in data]
>>> data2[:5]
[[0, 0, 1, 1, 0], [1, 1, 0, 1, 0], [1, 1, 0, 0, 0], [0, 0, 1, 0, 1], [1, 0, 1, 1, 1]]

是否有pandas或sklearn函数/管道对此类功能进行编码

使用熊猫系列在其索引中跟踪标签。然后通过
.loc
方法访问
1
的值。用
0
填充缺少的值

import pandas as pd
import numpy as np

s1 = pd.Series(np.ones(len(labels)), labels)
s0 = pd.Series(np.zeros(len(labels)), labels)

df = pd.concat([s1.loc[d].combine_first(s0) for d in data], axis=1)
df.astype(int).T[labels].values
安装程序 验证
数据[0]
为空

data[0]

[]

用它切片
s1
,得到一个空序列

s1.loc[data[0]]

Series([], dtype: float64)

结合
s0
填充0 s1.定位[数据[0]]。首先合并(s0)


pd.concat
将它们集合在一起

df = pd.concat([s1.loc[d].combine_first(s0) for d in data], axis=1).T

print df.head()

       flac  ini  pdf  php
0   0     0    1    0    0
1   0     0    0    0    1
2   1     1    0    1    1
3   0     1    0    0    0
4   0     0    0    1    0

按标签切片以获得正确的顺序并获取值

df.astype(int)[labels].values

array([[1, 0, 0, 0, 0],
       [0, 0, 0, 0, 1],
       [0, 1, 1, 1, 1],
       [0, 0, 0, 1, 0],
       [0, 0, 1, 0, 0],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 0, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [0, 0, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 0, 1, 1, 0],
       [0, 0, 0, 0, 0],
       [0, 1, 0, 0, 1],
       [0, 0, 1, 0, 1],
       [1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1],
       [0, 0, 0, 1, 0],
       [1, 1, 0, 1, 1]])

这看起来很有希望,但它忽略了空的集合数据点(如我的示例中的数据[1])(请注意,标签事先不知道(我创建该列表是为了创建数据)。是否有更好的(优于集合(sum(data,[]))方法来发现所有标签?可能是使用LabelEncoder或get_dummies?使用
pd.Series(pd.DataFrame(data)).values.flatte()).dropna().unique()
谢谢!有没有类似的方法来编码另一种形式的功能,其中我们没有一个集合,而是有一个计数的dict?例如,[{“:1,“pdf”:20},{“ini”:2,“php”:3}]
pd.DataFrame([{“:1,“pdf”:20},{“ini”:2,“php”:3}]).columns.tolist()
df = pd.concat([s1.loc[d].combine_first(s0) for d in data], axis=1).T

print df.head()

       flac  ini  pdf  php
0   0     0    1    0    0
1   0     0    0    0    1
2   1     1    0    1    1
3   0     1    0    0    0
4   0     0    0    1    0
df.astype(int)[labels].values

array([[1, 0, 0, 0, 0],
       [0, 0, 0, 0, 1],
       [0, 1, 1, 1, 1],
       [0, 0, 0, 1, 0],
       [0, 0, 1, 0, 0],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 0, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [0, 0, 1, 1, 1],
       [1, 1, 1, 1, 1],
       [1, 0, 1, 1, 0],
       [0, 0, 0, 0, 0],
       [0, 1, 0, 0, 1],
       [0, 0, 1, 0, 1],
       [1, 1, 1, 1, 1],
       [0, 0, 0, 1, 1],
       [0, 0, 0, 1, 0],
       [1, 1, 0, 1, 1]])