randomなone-hotベクトルを作るとき
numpy
を使って、
ランダムなデータを作ってそれをonehotにするやり方です。
n_dim
はラベルの数。
n_data
は生成するrandomデータの数。
y = np.random.randint(0, n_dim, size=[n_data]) # to one-hot n_labels = len(np.unique(y)) y = np.eye(n_labels)[y]
これでy.shape = (n_data, n_dim)
のone-hotな行列が手に入る