機械学習・自然言語処理の勉強メモ

学んだことのメモやまとめ

学習データのシャッフル

学習データがカテゴリ順になっていて、それをランダムな順序に変更したい、
でも、X(学習データ),y(Xの各データに対応するラベル)の順序は維持したい場合の工夫。

import numpy as np

X = np.array([[11, 12, 13], [22, 23, 26], [31, 33, 34], [40, 41, 45]])
y = np.array([1, 2, 3, 4])

for l in [X, y]:
    np.random.seed(1)
    np.random.shuffle(l)

print X
[[40 41 45]
 [31 33 34]
 [11 12 13]
 [22 23 26]]

print y

[4 3 1 2]

こうした場合、X,yの並び順の対応は保証されない。

np.random.seed(1)
np.random.shuffle(X)
np.random.shuffle(y

seedはそれぞれの直前で定義する必要がある。

np.random.seed(1)
np.random.shuffle(X)
np.random.seed(1)
np.random.shuffle(y

これならOK