1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ニューラルネットワークの入力(X)と出力(y)をランダムに並び替える

Last updated at Posted at 2018-05-10

はじめに

ニューラルネットワークでミニバッチ学習させるためには、入力(X)と出力(y)をランダムに並べ替える必要がある。そこで、Xとyの各要素の対応を崩さないでランダムに並び替える方法を調べてみた。

結果

ベタな方法は、配列のインデックスをランダムに並べ替え、ランダムに並び替えたインデックスの並びでXとyの配列要素を各々取り出す。

import numpy as np
X = np.array([1, 2, 3, 4])
y = np.array([11, 12, 13, 14])
p = np.random.permutation(len(X))
X = X[p]
y = y[p]

Xとyをzipでまとめると、Xとyの要素を同時に並び替えることができる。

import numpy as np
X = np.array([1, 2, 3, 4])
y = np.array([11, 12, 13, 14])
zipped = list(zip(X, y))
np.random.shuffle(zipped)
X_result, y_result = zip(*zipped)
X = np.asarray(X_result)
y = np.asarray(y_result)

これを関数にすると以下の通り。


def shuffle_dataset(X, y):
    zippded = list(zip(X, y))
    np.random.shuffle(zipped)
    X_result, y_result = zip(*zipped)
    return np.asarray(X_result), np.asarray(y_result)

参考

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?