【发布时间】:2019-10-14 15:04:54
【问题描述】:
我正在尝试创建一个允许未观察级别的字符列表的单一热编码 (ohe)。使用来自Convert array of indices to 1-hot encoded numpy array 和Finding the index of an item given a list containing it in Python 的答案,我确实想要以下内容:
# example data
# this is the full list including unobserved levels
av = list(map(chr, range(ord('a'), ord('z')+1)))
# this is the vector to apply ohe
v = ['a', 'f', 'u']
# apply one hot encoding
ohe = np.zeros((len(v), len(av)))
for i in range(len(v)): ohe[i, av.index(v[i])] = 1
ohe
有没有更标准/更快的方法来做到这一点,请注意上面的第二个链接提到了.index() 的瓶颈。
(我的问题规模:全向量 (av) 有大约 1000 个级别,ohe (v) 的值长度为 0.5M。谢谢。
【问题讨论】: