def DBN_Train(seq_len,win, lr): data_src = LD.loadCsvData_Np("../data/co2-ppm-mauna-loa-19651980.csv", skiprows=1, ColumnList=[1]) # Australia/US British/US Canadian/US Dutch/US French/US German/US Japanese/US Swiss/US data_t = data_src[:, 0:1].copy() # 数据还源时使用 t_mean = np.mean(data_t) t_min = np.min(data_t) t_max = np.max(data_t) # 数据预处理 result, x_result, y_result = LD.dataRecombine_Single(data_t,seq_len) # print(x_result, y_result) result_len = len(result) row = round(0.8 * result.shape[0]) row = result_len - 87 windowSize = row windowSize = win # 数据归一化 # data_normalization = EvaluationIndex.归一化.normalization_max_min_负1_1(data_src) x_result_GY = ((x_result - t_min) / (t_max - t_min)).copy() y_result_GY = ((y_result - t_min) / (t_max - t_min)).copy() # x_result = (x_result - t_mean) / np.std(x_result) # y_result = (y_result - t_mean) / np.std(x_result) y_rbf_all = [] y_test_all = [] rng = np.random.RandomState(1233) for y_i in range(row, row+1): if y_i < windowSize: continue x_train = x_result_GY[y_i - windowSize:y_i] y_train = y_result_GY[y_i - windowSize:y_i] x_test = x_result[y_i:y_i + 1] y_test = y_result[y_i:y_i + 1] net = DBN(layer_sizes=[seq_len,20,40], bp_layer=[1]) net.pretrain(x_train,lr=lr, epochs=200) net.fineTune(x_train, y_train,lr=lr, epochs=10000) y_rbf = net.predict(x_train) import MDLearn.utils.EvalueationIndex as EI # ei = EI.evalueationIndex(y_rbf, y_train) # print("归一化训练RMSE") # ei.show() # import MDLearn.utils.Draw as draw # draw.plot_results_point(y_rbf, y_train) y_rbf_haunYuan = y_rbf*(t_max - t_min)+t_min y_train_haunYuan = y_result[y_i - windowSize:y_i] print("还原训练RMSE") ei = EI.evalueationIndex(y_rbf_haunYuan, y_train_haunYuan) ei.show() draw.plot_results_point(y_rbf_haunYuan, y_train_haunYuan) '''DBN_T 效果不好 RMSE 在0.08左右'''
def DBN_BP_Test(seq_len, win, lr): data_src = LD.loadCsvData_Np("../data/co2-ppm-mauna-loa-19651980.csv", skiprows=1, ColumnList=[1]) # Australia/US British/US Canadian/US Dutch/US French/US German/US Japanese/US Swiss/US data_t =data_src[:,0:1].copy() # 数据还源时使用 t_mean = np.mean(data_t) t_min = np.min(data_t) t_max = np.max(data_t) # 数据预处理 result,x_result,y_result = LD.dataRecombine_Single(data_t, seq_len) # print(x_result, y_result) result_len = len(result) row = round(0.8 * result.shape[0]) row = result_len - 87 windowSize = row windowSize = win # 数据归一化 # data_normalization = EvaluationIndex.归一化.normalization_max_min_负1_1(data_src) x_result_GY = ((x_result - t_min) / (t_max - t_min)).copy() y_result_GY = ((y_result - t_min) / (t_max - t_min)).copy() # x_result = (x_result - t_mean) / np.std(x_result) # y_result = (y_result - t_mean) / np.std(x_result) y_rbf_all = [] y_test_all = [] rng = np.random.RandomState(1233) for y_i in range(row, result_len): if y_i < windowSize: continue x_train = x_result_GY[y_i - windowSize:y_i] y_train = y_result_GY[y_i - windowSize:y_i] x_test = x_result_GY[y_i:y_i + 1] y_test = y_result_GY[y_i:y_i + 1] # print(x_train, y_train) # assert False net = DBN(layer_sizes=[seq_len,20,40], bp_layer=[1]) net.pretrain(x_train,lr=lr, epochs=200) # net.fineTune(x_train, y_train,lr=lr, epochs=10000) bp = BP([seq_len,20,40, 1]) w_list, b_list = net.getHyperParameter() bp.setHyperParameter(w_list, b_list) bp.train(x_test, y_test, lr=lr, epochs=10000) y_rbf = bp.predict(x_train) y_rbf_all.append(y_rbf) y_test_all.append(y_test) # print(np.array(y_rbf_all).ravel()) # print(np.array(y_test_all).ravel())#, np.array(y_test_all)) # print("全部预测RMSE") # y_rbf_all = np.array(y_rbf_all).ravel() # y_test_all = np.array(y_test_all).ravel() # ei = EI.evalueationIndex(y_rbf_all, y_test_all) # ei.show() import MDLearn.utils.Draw as draw # draw.plot_results_point(y_rbf_all, y_test_all) '''还原数据''' print("DBN_BP_Test还原预测RMSE") y_rbf_all = np.array(y_rbf_all) y_test_all = np.array(y_test_all) y_rbf_haunYuan = y_rbf_all * (t_max - t_min) + t_min y_test_haunYuan = y_test_all * (t_max - t_min) + t_min ei = EI.evalueationIndex(y_rbf_haunYuan, y_test_haunYuan) ei.show()
def BP_Test(): data_src = LD.loadCsvData_Np("data/co2-ppm-mauna-loa-19651980.csv", skiprows=1, ColumnList=[1]) # Australia/US British/US Canadian/US Dutch/US French/US German/US Japanese/US Swiss/US data_t =data_src[:,0:1].copy() # 数据还源时使用 t_mean = np.mean(data_t) t_min = np.min(data_t) t_max = np.max(data_t) # 数据预处理 result,x_result,y_result = LD.dataRecombine_Single(data_t, 3) # print(x_result, y_result) result_len = len(result) row = round(0.8 * result.shape[0]) row = result_len - 87 windowSize = row windowSize = row # 数据归一化 # data_normalization = EvaluationIndex.归一化.normalization_max_min_负1_1(data_src) x_result_GY = ((x_result - t_min) / (t_max - t_min)).copy() y_result_GY = ((y_result - t_min) / (t_max - t_min)).copy() # x_result = (x_result - t_mean) / np.std(x_result) # y_result = (y_result - t_mean) / np.std(x_result) y_rbf_all = [] y_test_all = [] rng = np.random.RandomState(1233) for y_i in range(row, result_len): if y_i < windowSize: continue x_train = x_result_GY[y_i - windowSize:y_i] y_train = y_result_GY[y_i - windowSize:y_i] x_test = x_result_GY[y_i:y_i + 1] y_test = y_result_GY[y_i:y_i + 1] # print(x_train, y_train) # assert False bp = BP.BP( [len(x_test[0]), 6, 1]) bp.train(x_train, y_train, lr=0.01, epochs=10000, residual=0.0001) y_rbf = bp.predict(x_test) y_rbf_all.append(y_rbf) y_test_all.append(y_test) # print(np.array(y_rbf_all).ravel()) # print(np.array(y_test_all).ravel())#, np.array(y_test_all)) y_rbf_all = np.array(y_rbf_all).ravel() y_test_all = np.array(y_test_all).ravel() import MDLearn.utils.EvalueationIndex as EI ei = EI.evalueationIndex(y_rbf_all, y_test_all) ei.show() import MDLearn.utils.Draw as draw draw.plot_results_point(y_rbf_all, y_test_all) '''还原数据''' y_rbf_haunYuan = y_rbf_all * (t_max - t_min) + t_min y_test_haunYuan = y_test_all * (t_max - t_min) + t_min ei = EI.evalueationIndex(y_rbf_haunYuan, y_test_haunYuan) ei.show() draw.plot_results_point(y_rbf_haunYuan, y_test_haunYuan)
def Test_MLP(): data_src = LD.loadCsvData_Np("data/co2-ppm-mauna-loa-19651980.csv", skiprows=1, ColumnList=[1]) # Australia/US British/US Canadian/US Dutch/US French/US German/US Japanese/US Swiss/US data_t = data_src[:, 0:1].copy() # 数据还源时使用 t_mean = np.mean(data_t) t_min = np.min(data_t) t_max = np.max(data_t) # 数据预处理 result, x_result, y_result = LD.dataRecombine_Single(data_t, 3) # print(x_result, y_result) result_len = len(result) row = round(0.8 * result.shape[0]) row = result_len - 87 windowSize = row windowSize = row # 数据归一化 # data_normalization = EvaluationIndex.归一化.normalization_max_min_负1_1(data_src) x_result = (x_result - t_min) / (t_max - t_min) y_result = (y_result - t_min) / (t_max - t_min) # x_result = (x_result - t_mean) / np.std(x_result) # y_result = (y_result - t_mean) / np.std(x_result) y_rbf_all = [] y_test_all = [] rng = np.random.RandomState(1233) for y_i in range(row, result_len): if y_i < windowSize: continue x_train = x_result[y_i - windowSize:y_i] y_train = y_result[y_i - windowSize:y_i] x_test = x_result[y_i:y_i + 1] y_test = y_result[y_i:y_i + 1] # y_train = y_train[np.newaxis].T # construct MLP svr_rbf = MLP.MLP(input=x_train, label=y_train, n_in=len(x_test[0]), n_hidden=8, n_out=1, rng=rng) # classifier = MLP(input=x, label=y, n_in=2, n_hidden=3, n_out=1, rng=rng) # train svr_rbf.train() # test y_rbf = svr_rbf.predict(x_test) y_rbf_all.append(y_rbf) y_test_all.append(y_test) # print(np.array(y_rbf_all))#, np.array(y_test_all)) y_rbf_all = np.array(y_rbf_all).ravel() y_test_all = np.array(y_test_all).ravel() import MDLearn.utils.EvalueationIndex as EI ei = EI.evalueationIndex(y_rbf_all, y_test_all) ei.show() import MDLearn.utils.Draw as draw
continue x_train = x_result[y_i - windowSize:y_i] y_train = y_result[y_i - windowSize:y_i] x_test = x_result[y_i:y_i + 1] y_test = y_result[y_i:y_i + 1] # y_train = y_train[np.newaxis].T svr_rbf = SVR(kernel='rbf', C=1000, gamma=0.1) y_rbf = svr_rbf.fit(x_train, y_train).predict(x_test) # sda = SDA.SdA(input=x_train, label=y_train, n_ins=seq_len, hidden_layer_sizes=[8], n_outs=1) # sda.pretrain(lr=0.5, epochs=1000) # sda.finetune(lr=0.5, epochs=100) y_rbf = svr_rbf.predict(x_test) y_rbf_all.append(y_rbf) y_test_all.append(y_test) # print(np.array(y_rbf_all))#, np.array(y_test_all)) y_rbf_all = np.array(y_rbf_all).ravel() y_test_all = np.array(y_test_all).ravel() import MDLearn.utils.EvalueationIndex as EI ei = EI.evalueationIndex(y_rbf_all, y_test_all) ei.show() import MDLearn.utils.Draw as draw draw.plot_results_point(y_rbf_all, y_test_all) # print(y_rbf_all) # print(y_test_all)