注释代码 import numpy as npfrom sklearn import linear_modelfrom sklearnsvm import SVRfrom sklearntree import DecisionTreeRegressorfrom sklearnensemble import RandomForestRegressorimport xgboostimport ligh
导入所需的库
import numpy as np from sklearn import linear_model from sklearn.svm import SVR from sklearn.tree import DecisionTreeRegressor from sklearn.ensemble import RandomForestRegressor import xgboost import lightgbm as lgb from sklearn.neural_network import MLPRegressor from sklearn.metrics import mean_squared_error from sklearn.metrics import r2_score from sklearn.metrics import mean_absolute_error import math import joblib from get_data import merge_crop_data,merge_crop_depth_data from sklearn.preprocessing import PolynomialFeatures from sklearn.pipeline import make_pipeline
定义函数
def math_rmd(y,predict): temp_1 = (100/np.mean(y)) deviation_add = 0 i = 0 while i<len(y): deviation_add = (y[i] - predict[i]) + deviation_add i = i+1 mean_deviation = deviation_add/len(y) rmd= temp_1 *mean_deviation return rmd
def math_ef(y,predict): deviation_square = 0 bias_square = 0 i = 0 while i<len(y): deviation_square = math.pow(y[i]-predict[i],2) + deviation_square bias_square = math.pow(np.mean(y)-y[i],2) + bias_square i = i+1 ef = 1- (deviation_square/bias_square) return ef
定义线性回归函数
def linear_reg(wheat_file_path,paddy_file_path,sampling_depth,cv_n): print('linear') # 初始化一些列表 r2_val_list = [] r2_test_list = [] rmse_val_list = [] rmse_test_list = [] mae_val_list = [] mae_test_list = [] rmd_val_lsit = [] # 平均偏差 rmd_test_list = [] ef_val_list = [] ef_test_list = [] # 交叉验证 for cv_i in range(cv_n): # 获取数据 x_train,y_train,x_val,y_val,x_test,y_test = merge_crop_data(wheat_file_path,paddy_file_path,cv_i,sampling_depth,2,1) # 训练模型 regressor = linear_model.LinearRegression() regressor.fit(x_train, y_train) # 保存模型 joblib.dump(regressor, '../model/linear_' + sampling_depth + '' + str(cv_i) + '.pkl') # 加载模型 regressor = joblib.load('../model/linear' + sampling_depth + '' + str(cv_i) + '.pkl') # 计算指标 r2_val = r2_score(y_val, regressor.predict(x_val)) r2_test = r2_score(y_test, regressor.predict(x_test)) rmse_val = np.sqrt(mean_squared_error(y_val, regressor.predict(x_val))) rmse_test = np.sqrt(mean_squared_error(y_test, regressor.predict(x_test))) mae_val = mean_absolute_error(y_val, regressor.predict(x_val)) mae_test = mean_absolute_error(y_test, regressor.predict(x_test)) rmd_val = math_rmd(y_val,regressor.predict(x_val)) rmd_test = math_rmd(y_test,regressor.predict(x_test)) ef_val = math_ef(y_val,regressor.predict(x_val)) ef_test = math_ef(y_test,regressor.predict(x_test)) # 添加到列表 r2_val_list.append(r2_val) r2_test_list.append(r2_test) rmse_val_list.append(rmse_val) rmse_test_list.append(rmse_test) mae_val_list.append(mae_val) mae_test_list.append(mae_test) rmd_val_lsit.append(rmd_val) rmd_test_list.append(rmd_test) ef_val_list.append(ef_val) ef_test_list.append(ef_test) # 将结果写入文件 with open('../result/result_predict_to_20cm.txt','a') as f: f.write("model,r2,rmse,mae,rmd,ef\n") f.write('linear,' + str(round(np.mean(r2_test_list), 2)) + ',' + str(round(np.mean(rmse_test_list), 2)) + ',' + str(round(np.mean(mae_test_list), 2)) + ',' + str(round(np.mean(rmd_test_list), 2)) + ',' + str( round(np.mean(ef_test_list), 2))+'\n') # 基于全部数据基于最优参数训练出最优模型 x_train,y_train,x_val,y_val,x_test,y_test = merge_crop_data(wheat_file_path,paddy_file_path,1,sampling_depth,2,1) x_data_1 = np.concatenate((x_train, x_val), axis=0) x_data = np.concatenate((x_data_1, x_test), axis=0) y_data_1 = np.concatenate((y_train, y_val), axis=0) y_data = np.concatenate((y_data_1, y_test), axis=0) regressor = linear_model.LinearRegression() regressor.fit(x_data, y_data) joblib.dump(regressor, '../final_model/linear' + sampling_depth + '.pkl'
原文地址: https://www.cveoy.top/t/topic/e3l8 著作权归作者所有。请勿转载和采集!