def no_resampling_data(divide_box, fname): list_acc_loss = [] divide_data = np.array(divide_box) nr.shuffle(divide_data) #print divide_data #print divide_data.shape #10foldするために各データ群を1回ずつテストデータに入力し残りをリサンプリングしてトレーニングデータに入力する for i in range(10): #テストデータを入れる箱を作成し,テストデータに各データ群を入れていく _test = divide_data[i] x_test = _test[:, 1:21].astype(np.float32) y_test = _test[:, 0].astype(np.int32) - 1 training_datapack = np.delete(divide_data, i, 0) training_dataset = [] for k in training_datapack: for n in k: training_dataset.append(n.tolist()) training_data_array = np.array(training_dataset) print training_data_array.shape training_data_list = training_data_array.tolist() random.shuffle(training_data_list) training_data_array = np.array(training_data_list) print training_data_array.shape print training_data_array[:, 1:21] #トレーニングデータに入れる x_train = np.array(training_data_array[:, 1:21]).astype(np.float32) y_train = np.array(training_data_array[:, 0]).astype(np.int32) - 1 #print x_train acc_loss = MLP.learning(x_train, y_train, x_test, y_test, n_fold=i, fname=fname) list_acc_loss.append(acc_loss) list_acc_loss = np.array(list_acc_loss) #print list_acc_loss.shape #list_acc_loss=list_acc_loss.reshape(100,5) print np.average(list_acc_loss)
def no_resampling_data(divide_box,fname): list_acc_loss=[] divide_data=np.array(divide_box) nr.shuffle(divide_data) #print divide_data #print divide_data.shape #10foldするために各データ群を1回ずつテストデータに入力し残りをリサンプリングしてトレーニングデータに入力する for i in range(10): #テストデータを入れる箱を作成し,テストデータに各データ群を入れていく _test=divide_data[i] x_test=_test[:,1:21].astype(np.float32) y_test=_test[:,0].astype(np.int32)-1 training_datapack = np.delete(divide_data,i,0) training_dataset=[] for k in training_datapack: for n in k: training_dataset.append(n.tolist()) training_data_array=np.array(training_dataset) print training_data_array.shape training_data_list=training_data_array.tolist() random.shuffle(training_data_list) training_data_array = np.array(training_data_list) print training_data_array.shape print training_data_array[:, 1:21] #トレーニングデータに入れる x_train = np.array(training_data_array[:, 1:21]).astype(np.float32) y_train = np.array(training_data_array[:, 0]).astype(np.int32)-1 #print x_train acc_loss=MLP.learning(x_train,y_train,x_test,y_test,n_fold=i,fname=fname) list_acc_loss.append(acc_loss) list_acc_loss=np.array(list_acc_loss) #print list_acc_loss.shape #list_acc_loss=list_acc_loss.reshape(100,5) print np.average(list_acc_loss)
def resampling_data_3class(divide_box,fname): list_acc_loss=[] #リサンプリングされたデータを入れる箱を作成する data_resampling=[] divide_data=np.array(divide_box) nr.shuffle(divide_data) #print divide_data #print divide_data.shape #10foldするために各データ群を1回ずつテストデータに入力し残りをリサンプリングしてトレーニングデータに入力する for i in range(10): #テストデータを入れる箱を作成し,テストデータに各データ群を入れていく _test=divide_data[i] #print _test #print _test.shape x_test=_test[:,1:21].astype(np.float32) y_test=_test[:,0].astype(np.int32)-1 #y_test=_test[:,0].astype(np.int32) #print x_test #各評価毎に再分割するために各評価の値を見やすいように配列化する remaining_data=np.delete(divide_data,i,0) #print remaining_data.shape remaining_data=np.array(remaining_data) #print remaining_data #print remaining_data.shape remain_data=[] #データを配列化する for k in remaining_data: for n in k: remain_data.append(n.tolist()) remained_data=np.array(remain_data) #print remained_data #print remained_data.shape value1_count=0 value2_count=0 value3_count=0 #各評価毎に分割する for m in range(remained_data.shape[0]): #print remained_data.shape if remained_data[m,0]==1: if value1_count==0: data_value1=np.array([remained_data[m]]) value1_count+=1 else: data_value1=np.r_[data_value1,[remained_data[m]]] elif remained_data[m,0]==2: if value2_count==0: data_value2=np.array([remained_data[m]]) value2_count+=1 else: data_value2=np.r_[data_value2,[remained_data[m]]] else: if value3_count==0: data_value3=np.array([remained_data[m]]) value3_count+=1 else: data_value3=np.r_[data_value3,[remained_data[m]]] #データに data_value=[] data_value.append(data_value1) data_value.append(data_value2) data_value.append(data_value3) #print len(data_value) #quit() data_resampling=[] for j in range(3): selected_ave=[] num_sum=[] data_value=np.array(data_value) #print data_value.shape data_value.shape for ix in xrange(data_value.shape[0]): data_value[ix]=np.array(data_value[ix]) for ix in xrange(data_value.shape[0]): #print data_value[ix].shape[0] data_value[ix].shape[0] #print data_value data_value=np.array(data_value) _data_value=data_value[j] length = _data_value.shape[0] #quit() for p in range(length): num=[] _data_stock=[] for l in range(3): rand = random.randint(0,length-1) while(rand in num): rand = random.randint(0,length-1) _data_stock.append(_data_value[rand]) #print _data_stock #print len(_data_stock) num.append(rand) num_sum.append(num) l+=1 data_stock=np.array(_data_stock) #print data_stock selected_ave = sum(data_stock) / 5 #print selected_ave #quit() data_resampling.append(selected_ave) n+=1 p+=1 data_resampled=np.array(data_resampling) #print data_resampled print data_resampled.shape #トレーニングデータに入れる x_train = np.array(data_resampled[:, 1:21]).astype(np.float32) y_train = np.array(data_resampled[:, 0]).astype(np.int32)-1 #y_train = np.array(data_resampled[:, 0]).astype(np.int32) #print x_trainacc_loss=MLP.learning(x_train,y_train,x_test,y_test,n_fold=i,fname=fname) acc_loss=MLP.learning(x_train,y_train,x_test,y_test,n_fold=i,fname=fname) list_acc_loss.append(acc_loss) list_acc_loss=np.array(list_acc_loss) print list_acc_loss.shape list_acc_loss=list_acc_loss.reshape(100,5)
def resampling_data(divide_box,fname): """ @param divide_box: 3次元、N-foldに分割された後のデータ (fold数 × サンプル × (特徴量数+ラベル)) 例:10 fold, 10 sample, 20 feature + 1 label @param fname: 被験者名 """ list_acc_loss=[] #リサンプリングされたデータを入れる箱を作成する data_resampling=[] divide_data=np.array(divide_box) nr.shuffle(divide_data) #print divide_data #print divide_data.shape #10foldするために各データ群を1回ずつテストデータに入力し残りをリサンプリングしてトレーニングデータに入力する for i in range(10): #テストデータを入れる箱を作成し,テストデータに各データ群を入れていく _test=divide_data[i] #print _test #print _test.shape x_test=_test[:,1:21].astype(np.float32) y_test=_test[:,0].astype(np.int32)-1 #y_test=_test[:,0].astype(np.int32) #print x_test #各評価毎に再分割するために各評価の値を見やすいように配列化する _remaining_data=np.delete(divide_data,i,0) #print remaining_data.shape remaining_data=np.array(_remaining_data) # print remaining_data #print remaining_data #print remaining_data.shape #quit() remain_data=[] #データを配列化する for k in remaining_data: for n in k: remain_data.append(n.tolist()) remained_data=np.array(remain_data) #print remained_data #print remained_data.shape #quit() value1_count=0 value2_count=0 value3_count=0 value4_count=0 value5_count=0 #各評価毎に分割する for m in range(remained_data.shape[0]): #print remained_data.shape if remained_data[m,0]==1: if value1_count==0: data_value1=np.array([remained_data[m]]) value1_count+=1 else: data_value1=np.r_[data_value1,[remained_data[m]]] elif remained_data[m,0]==2: if value2_count==0: data_value2=np.array([remained_data[m]]) value2_count+=1 else: data_value2=np.r_[data_value2,[remained_data[m]]] elif remained_data[m,0]==3: if value3_count==0: data_value3=np.array([remained_data[m]]) value3_count+=1 else: data_value3=np.r_[data_value3,[remained_data[m]]] elif remained_data[m,0]==4: if value4_count==0: data_value4=np.array([remained_data[m]]) value4_count+=1 else: data_value4=np.r_[data_value4,[remained_data[m]]] else: if value5_count==0: data_value5=np.array([remained_data[m]]) value5_count+=1 else: data_value5=np.r_[data_value5,[remained_data[m]]] #データに data_value=[] data_value.append(data_value1) data_value.append(data_value2) data_value.append(data_value3) data_value.append(data_value4) data_value.append(data_value5) #print len(data_value) #quit() data_resampling=[] for j in range(5): selected_ave=[] num_sum=[] data_value=np.array(data_value) #print data_value.shape data_value.shape for ix in xrange(data_value.shape[0]): data_value[ix]=np.array(data_value[ix]) for ix in xrange(data_value.shape[0]): #print data_value[ix].shape[0] data_value[ix].shape[0] #print data_value data_value=np.array(data_value) _data_value=data_value[j] length = _data_value.shape[0] #quit() for p in range(length): num=[] _data_stock=[] for l in range(5): rand = random.randint(0,length-1) while(rand in num): rand = random.randint(0,length-1) _data_stock.append(_data_value[rand]) #print _data_stock #print len(_data_stock) num.append(rand) num_sum.append(num) l+=1 data_stock=np.array(_data_stock) #print data_stock selected_ave = sum(data_stock) / 5 #print selected_ave #quit() data_resampling.append(selected_ave) # n+=1 # p+=1 data_resampled=np.array(data_resampling) #print data_resampled #print data_resampled.shape #quit() ''' #トレーニングデータ180コ data_increased=data_resampled data_increased=np.append(data_increased,remained_data) data_increased=data_increased.reshape(180,21) #print data_increased.shape #print data_increased #quit() #トレーニングデータに入れる x_train = np.array(data_increased[:, 1:21]).astype(np.float32) y_train = np.array(data_increased[:, 0]).astype(np.int32)-1 ''' #トレーニングデータに入れる x_train = np.array(data_resampled[:, 1:21]).astype(np.float32) y_train = np.array(data_resampled[:, 0]).astype(np.int32)-1 #y_train = np.array(data_resampled[:, 0]).astype(np.int32) #print x_trainacc_loss=MLP.learning(x_train,y_train,x_test,y_test,n_fold=i,fname=fname) acc_loss=MLP.learning(x_train,y_train,x_test,y_test,n_fold=i,fname=fname) list_acc_loss.append(acc_loss) list_acc_loss=np.array(list_acc_loss) print list_acc_loss.shape list_acc_loss=list_acc_loss.reshape(100,5)
def resampling_data_3class(divide_box, fname): list_acc_loss = [] #リサンプリングされたデータを入れる箱を作成する data_resampling = [] divide_data = np.array(divide_box) nr.shuffle(divide_data) #print divide_data #print divide_data.shape #10foldするために各データ群を1回ずつテストデータに入力し残りをリサンプリングしてトレーニングデータに入力する for i in range(10): #テストデータを入れる箱を作成し,テストデータに各データ群を入れていく _test = divide_data[i] #print _test #print _test.shape x_test = _test[:, 1:21].astype(np.float32) y_test = _test[:, 0].astype(np.int32) - 1 #y_test=_test[:,0].astype(np.int32) #print x_test #各評価毎に再分割するために各評価の値を見やすいように配列化する remaining_data = np.delete(divide_data, i, 0) #print remaining_data.shape remaining_data = np.array(remaining_data) #print remaining_data #print remaining_data.shape remain_data = [] #データを配列化する for k in remaining_data: for n in k: remain_data.append(n.tolist()) remained_data = np.array(remain_data) #print remained_data #print remained_data.shape value1_count = 0 value2_count = 0 value3_count = 0 #各評価毎に分割する for m in range(remained_data.shape[0]): #print remained_data.shape if remained_data[m, 0] == 1: if value1_count == 0: data_value1 = np.array([remained_data[m]]) value1_count += 1 else: data_value1 = np.r_[data_value1, [remained_data[m]]] elif remained_data[m, 0] == 2: if value2_count == 0: data_value2 = np.array([remained_data[m]]) value2_count += 1 else: data_value2 = np.r_[data_value2, [remained_data[m]]] else: if value3_count == 0: data_value3 = np.array([remained_data[m]]) value3_count += 1 else: data_value3 = np.r_[data_value3, [remained_data[m]]] #データに data_value = [] data_value.append(data_value1) data_value.append(data_value2) data_value.append(data_value3) #print len(data_value) #quit() data_resampling = [] for j in range(3): selected_ave = [] num_sum = [] data_value = np.array(data_value) #print data_value.shape data_value.shape for ix in xrange(data_value.shape[0]): data_value[ix] = np.array(data_value[ix]) for ix in xrange(data_value.shape[0]): #print data_value[ix].shape[0] data_value[ix].shape[0] #print data_value data_value = np.array(data_value) _data_value = data_value[j] length = _data_value.shape[0] #quit() for p in range(length): num = [] _data_stock = [] for l in range(3): rand = random.randint(0, length - 1) while (rand in num): rand = random.randint(0, length - 1) _data_stock.append(_data_value[rand]) #print _data_stock #print len(_data_stock) num.append(rand) num_sum.append(num) l += 1 data_stock = np.array(_data_stock) #print data_stock selected_ave = sum(data_stock) / 5 #print selected_ave #quit() data_resampling.append(selected_ave) n += 1 p += 1 data_resampled = np.array(data_resampling) #print data_resampled print data_resampled.shape #トレーニングデータに入れる x_train = np.array(data_resampled[:, 1:21]).astype(np.float32) y_train = np.array(data_resampled[:, 0]).astype(np.int32) - 1 #y_train = np.array(data_resampled[:, 0]).astype(np.int32) #print x_trainacc_loss=MLP.learning(x_train,y_train,x_test,y_test,n_fold=i,fname=fname) acc_loss = MLP.learning(x_train, y_train, x_test, y_test, n_fold=i, fname=fname) list_acc_loss.append(acc_loss) list_acc_loss = np.array(list_acc_loss) print list_acc_loss.shape list_acc_loss = list_acc_loss.reshape(100, 5)
def resampling_data(divide_box, fname): """ @param divide_box: 3次元、N-foldに分割された後のデータ (fold数 × サンプル × (特徴量数+ラベル)) 例:10 fold, 10 sample, 20 feature + 1 label @param fname: 被験者名 """ list_acc_loss = [] #リサンプリングされたデータを入れる箱を作成する data_resampling = [] divide_data = np.array(divide_box) nr.shuffle(divide_data) #print divide_data #print divide_data.shape #10foldするために各データ群を1回ずつテストデータに入力し残りをリサンプリングしてトレーニングデータに入力する for i in range(10): #テストデータを入れる箱を作成し,テストデータに各データ群を入れていく _test = divide_data[i] #print _test #print _test.shape x_test = _test[:, 1:21].astype(np.float32) y_test = _test[:, 0].astype(np.int32) - 1 #y_test=_test[:,0].astype(np.int32) #print x_test #各評価毎に再分割するために各評価の値を見やすいように配列化する _remaining_data = np.delete(divide_data, i, 0) #print remaining_data.shape remaining_data = np.array(_remaining_data) # print remaining_data #print remaining_data #print remaining_data.shape #quit() remain_data = [] #データを配列化する for k in remaining_data: for n in k: remain_data.append(n.tolist()) remained_data = np.array(remain_data) #print remained_data #print remained_data.shape #quit() value1_count = 0 value2_count = 0 value3_count = 0 value4_count = 0 value5_count = 0 #各評価毎に分割する for m in range(remained_data.shape[0]): #print remained_data.shape if remained_data[m, 0] == 1: if value1_count == 0: data_value1 = np.array([remained_data[m]]) value1_count += 1 else: data_value1 = np.r_[data_value1, [remained_data[m]]] elif remained_data[m, 0] == 2: if value2_count == 0: data_value2 = np.array([remained_data[m]]) value2_count += 1 else: data_value2 = np.r_[data_value2, [remained_data[m]]] elif remained_data[m, 0] == 3: if value3_count == 0: data_value3 = np.array([remained_data[m]]) value3_count += 1 else: data_value3 = np.r_[data_value3, [remained_data[m]]] elif remained_data[m, 0] == 4: if value4_count == 0: data_value4 = np.array([remained_data[m]]) value4_count += 1 else: data_value4 = np.r_[data_value4, [remained_data[m]]] else: if value5_count == 0: data_value5 = np.array([remained_data[m]]) value5_count += 1 else: data_value5 = np.r_[data_value5, [remained_data[m]]] #データに data_value = [] data_value.append(data_value1) data_value.append(data_value2) data_value.append(data_value3) data_value.append(data_value4) data_value.append(data_value5) #print len(data_value) #quit() data_resampling = [] for j in range(5): selected_ave = [] num_sum = [] data_value = np.array(data_value) #print data_value.shape data_value.shape for ix in xrange(data_value.shape[0]): data_value[ix] = np.array(data_value[ix]) for ix in xrange(data_value.shape[0]): #print data_value[ix].shape[0] data_value[ix].shape[0] #print data_value data_value = np.array(data_value) _data_value = data_value[j] length = _data_value.shape[0] #quit() for p in range(length): num = [] _data_stock = [] for l in range(5): rand = random.randint(0, length - 1) while (rand in num): rand = random.randint(0, length - 1) _data_stock.append(_data_value[rand]) #print _data_stock #print len(_data_stock) num.append(rand) num_sum.append(num) l += 1 data_stock = np.array(_data_stock) #print data_stock selected_ave = sum(data_stock) / 5 #print selected_ave #quit() data_resampling.append(selected_ave) # n+=1 # p+=1 data_resampled = np.array(data_resampling) #print data_resampled #print data_resampled.shape #quit() ''' #トレーニングデータ180コ data_increased=data_resampled data_increased=np.append(data_increased,remained_data) data_increased=data_increased.reshape(180,21) #print data_increased.shape #print data_increased #quit() #トレーニングデータに入れる x_train = np.array(data_increased[:, 1:21]).astype(np.float32) y_train = np.array(data_increased[:, 0]).astype(np.int32)-1 ''' #トレーニングデータに入れる x_train = np.array(data_resampled[:, 1:21]).astype(np.float32) y_train = np.array(data_resampled[:, 0]).astype(np.int32) - 1 #y_train = np.array(data_resampled[:, 0]).astype(np.int32) #print x_trainacc_loss=MLP.learning(x_train,y_train,x_test,y_test,n_fold=i,fname=fname) acc_loss = MLP.learning(x_train, y_train, x_test, y_test, n_fold=i, fname=fname) list_acc_loss.append(acc_loss) list_acc_loss = np.array(list_acc_loss) print list_acc_loss.shape list_acc_loss = list_acc_loss.reshape(100, 5)