One Hot Encoder-按类别分类
One Hot Encoder- Classification by categories
对于模型训练,我有一个具有重复值(数字)的向量
我想按数字接近度(一种簇)将这个向量分成 10 个不同的类别,这样我的输出将是 N * 10(N 是向量中值的数量)稀疏矩阵,每次我得到 1正确类别的索引。
这是我的代码:
a = np.array([1, 8, 7, 6, 5, 8,
95, 44, 778, 12, 25,
12, 12, 65, 47, 85,
32, 99, 88])
a = a.reshape(-1, 1)
max_a = np.max(a) # 99
min_a = np.min(a) # 1
div = (max_a - min_a) / 10 # 9.8
for i in range(a.shape[0]):
x = 1
while a[i] > (min_a + x * div):
x = x + 1
a[i] = x
# a = [1,1,1,1,1,1,1,10,5,8,2,3,2,2,7,5,9,4,10,9]
onehot_a = OneHotEncoder(sparse=False)
a = onehot_a.fit_transform(a)
print(a.shape) # (20,9)
但我希望输出的形状为 (20,10)
。
我哪里错了?
使用 np.digitize
和 答案:
a = np.array([1, 8, 7, 6, 5, 8,
95, 44, 78, 12, 25, #had a typo in this line
12, 12, 65, 47, 85,
32, 99, 88])
def onehot(a, bins = 10):
return np.eye(bins)[np.digitize(a, np.linspace(a.min(), a.max(), bins))-1]
onehot(a)
Out[]:
array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.]])
对于模型训练,我有一个具有重复值(数字)的向量 我想按数字接近度(一种簇)将这个向量分成 10 个不同的类别,这样我的输出将是 N * 10(N 是向量中值的数量)稀疏矩阵,每次我得到 1正确类别的索引。
这是我的代码:
a = np.array([1, 8, 7, 6, 5, 8,
95, 44, 778, 12, 25,
12, 12, 65, 47, 85,
32, 99, 88])
a = a.reshape(-1, 1)
max_a = np.max(a) # 99
min_a = np.min(a) # 1
div = (max_a - min_a) / 10 # 9.8
for i in range(a.shape[0]):
x = 1
while a[i] > (min_a + x * div):
x = x + 1
a[i] = x
# a = [1,1,1,1,1,1,1,10,5,8,2,3,2,2,7,5,9,4,10,9]
onehot_a = OneHotEncoder(sparse=False)
a = onehot_a.fit_transform(a)
print(a.shape) # (20,9)
但我希望输出的形状为 (20,10)
。
我哪里错了?
使用 np.digitize
和
a = np.array([1, 8, 7, 6, 5, 8,
95, 44, 78, 12, 25, #had a typo in this line
12, 12, 65, 47, 85,
32, 99, 88])
def onehot(a, bins = 10):
return np.eye(bins)[np.digitize(a, np.linspace(a.min(), a.max(), bins))-1]
onehot(a)
Out[]:
array([[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 0.]])