one_hot编码
import numpy as np
X=np.array([1,6,3,9,0])
T = np.zeros((X.size, 10))#10种分类,故为10
for idx, row in enumerate(T):
row[X[idx]] = 1
print(T)
[[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]
[1. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
当采用one-hot编码时,标签保存为one-hot形式;
当不采用one-hot编码时,标签保存为0,1,3,4,5,6,7,8,9,这种形式。
one-hot数组是指[0,0,1,0,0,0,0,0,0,0]这样的数组
函数表示为
def _change_one_hot_label(X):
T = np.zeros((X.size, 10))
for idx, row in enumerate(T):
row[X[idx]] = 1
return T