SJE/2015/ex05 課題A のプログラムをより汎用に使えるようにするために,次のことをやろう.
import numpy as np import cv2 def getData(): fnCat = '../cat/cat%03d.png' fnFace = '../face100/hoge%03d.png' X = np.empty( ( 131 + 100, 64 * 64 ) ) label = np.empty( 131 + 100, dtype = int ) for i in range( 131 ): img = cv2.imread( fnCat % i, cv2.IMREAD_GRAYSCALE ) X[i, :] = img.reshape( -1 ) label[i] = 0 for i in range( 100 ): img = cv2.imread( fnFace % i, cv2.IMREAD_GRAYSCALE ) X[i + 131, :] = img.reshape( -1 ) if i < 50: label[i + 131] = 1 else: label[i + 131] = 2 return X, label if __name__ == '__main__': X, lab = getData() print X.shape, lab.shape idxL = np.arange( X.shape[0], dtype = int ) % 2 == 0 print idxL XL, labL = X[idxL], lab[idxL] ndatL = XL.shape[0] XT, labT = X[-idxL], lab[-idxL] ndatT = XT.shape[0] print ndatL, ndatT
上記のプログラムを利用して,SJE/2015/ex05 の最短距離法のプログラムを作りなおそう.
ヒント:
In [26]: labL == 0 Out[26]: array([ True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False], dtype=bool) In [27]: hoge = XL[labL == 0, :] In [28]: hoge.shape Out[28]: (66, 4096) In [29]: np.mean(hoge, axis = 0)