def crossValidation(train=400, test=100, la=1e-2, run=5): mse = np.zeros(run) coe = gd.gen_coefficients() data_x, data_y, _ = gd.data_generator2(train + test, coe) seq = np.arange(train + test) seq = np.random.permutation(seq) seq = seq.reshape(int((train + test) / test), test) for i in range(run): test_x = [data_x[:, j] for j in seq[i]] test_x = np.transpose(np.matrix(np.ravel(test_x).reshape(test, 48))) test_y = np.transpose(np.matrix([data_y[j, 0] for j in seq[i]])) # print("test_x:",test_x.shape,"type:",type(test_x)) # print("test_y:",test_y.shape,"type:",type(test_y)) train_x = [data_x[:, j] for j in np.ravel(seq) if j not in seq[i]] train_x = np.transpose(np.matrix(np.ravel(train_x).reshape(train, 48))) train_y = np.transpose( np.matrix([data_y[j, 0] for j in np.ravel(seq) if j not in seq[i]])) # print('train_x:',train_x.shape,"type:",type(train_x)) # print('train_y:',train_y.shape,"type:",type(train_y)) w = train_ridgeregression(train_x, train_y, la) mse[i] += cal_mse(test_x, test_y, w) return np.average(mse)
def crossValidation(train=400,test=100,la=1e-2,run=5): mse = np.zeros(run) coe=gd.gen_coefficients() data_x,data_y,_=gd.data_generator2(train+test,coe) seq = np.arange(train+test) seq = np.random.permutation(seq) seq = seq.reshape(int((train+test)/test),test) for i in range(run): test_x=[data_x[:,j] for j in seq[i]] test_x= np.transpose(np.matrix(np.ravel(test_x).reshape(test,48))) test_y= np.transpose(np.matrix([data_y[j,0] for j in seq[i]])) # print("test_x:",test_x.shape,"type:",type(test_x)) # print("test_y:",test_y.shape,"type:",type(test_y)) train_x = [data_x[:,j] for j in np.ravel(seq) if j not in seq[i]] train_x = np.transpose(np.matrix(np.ravel(train_x).reshape(train,48))) train_y = np.transpose(np.matrix([data_y[j,0] for j in np.ravel(seq) if j not in seq[i]])) # print('train_x:',train_x.shape,"type:",type(train_x)) # print('train_y:',train_y.shape,"type:",type(train_y)) w=train_ridgeregression(train_x,train_y,la) mse[i]+=cal_mse(test_x,test_y,w) return np.average(mse)
def holdout(train=400, test=100, la=1e-2): coe = gd.gen_coefficients() data_x, data_y, _ = gd.data_generator2(train + test, coe) train_seq = random.sample([x for x in range(train + test)], train) test_seq = [x for x in range(train + test) if x not in train_seq] #training data train_x = [data_x[:, j] for j in train_seq] train_x = np.transpose(np.matrix(np.ravel(train_x).reshape(train, 48))) train_y = np.transpose(np.matrix([data_y[j, 0] for j in train_seq])) #testing data test_x = [data_x[:, j] for j in test_seq] test_x = np.transpose(np.matrix(np.ravel(test_x).reshape(test, 48))) test_y = np.transpose(np.matrix([data_y[j, 0] for j in test_seq])) w = train_ridgeregression(train_x, train_y, la) # print("w:",w.shape,"data:",w) return cal_mse(test_x, test_y, w)
def holdout(train=400,test=100,la=1e-2): coe=gd.gen_coefficients() data_x,data_y,_=gd.data_generator2(train+test,coe) train_seq = random.sample([x for x in range(train+test)],train) test_seq = [x for x in range(train+test) if x not in train_seq] #training data train_x=[data_x[:,j] for j in train_seq] train_x = np.transpose(np.matrix(np.ravel(train_x).reshape(train,48))) train_y=np.transpose(np.matrix([data_y[j,0] for j in train_seq])) #testing data test_x=[data_x[:,j] for j in test_seq] test_x= np.transpose(np.matrix(np.ravel(test_x).reshape(test,48))) test_y=np.transpose(np.matrix([data_y[j,0] for j in test_seq])) w = train_ridgeregression(train_x,train_y,la) # print("w:",w.shape,"data:",w) return cal_mse(test_x,test_y,w)