質問の意向を完全に無視する形になりますがディレクトリに分けず、全ての画像データとラベルデータをひとまとめにして分けることを考えれば、例えばsklearnなどのライブラリを使えば簡単に分類することが可能です。
python
1from sklearn.cross_validation import train_test_split
2import numpy as np
3
4
5def main():
6 data_set = np.array([
7 [0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
8 [1, 1, 0, 1, 0, 1, 0, 1, 0, 1],
9 [2, 1, 0, 1, 0, 1, 0, 1, 0, 1],
10 [3, 1, 0, 1, 0, 1, 0, 1, 0, 1],
11 [4, 1, 0, 1, 0, 1, 0, 1, 0, 1],
12 [5, 1, 0, 1, 0, 1, 0, 1, 0, 1],
13 [6, 1, 0, 1, 0, 1, 0, 1, 0, 1],
14 [7, 1, 0, 1, 0, 1, 0, 1, 0, 1],
15 [8, 1, 0, 1, 0, 1, 0, 1, 0, 1],
16 [9, 1, 0, 1, 0, 1, 0, 1, 0, 1]
17 ])
18 print(data_set.shape)
19 label = np.array([0, 1, 0, 1, 0, 1, 0, 1, 0, 1])
20 _train_x, _train_y, _test_x, _test_y = train_test_split(
21 data_set,
22 label,
23 test_size=0.1
24 )
25 print(_train_x.shape)
26 print(_test_x.shape)
27 """
28 >>> (10, 10)
29 >>> (9, 10)
30 >>> (9,)
31 test_sizeを変化させることによって割合を変えることができる。
32 今回は0.1指定なので9:1(train:test)
33 """
34
35if __name__ == "__main__":
36 main()
37
参考:
sklearn.model_selection.train_test_split
scikit-learnでトレーニングデータとテストデータを分割する