from sklearn.linear_model import LinearRegression lr = LinearRegression() #线性回归模型 lr.fit(X_train,y_train) lr_y_predict = lr.predict(X_test) linear_result = [] linear_result = [metrics.mean_absolute_error(y_test,lr_y_predict),metrics.mean_squared_error(y_test,lr_y_predict),np.mean(np.abs((y_test - lr_y_predict) / y_test))] print'use LinearRegression predict:' #print 'the mean_absolute_error of is ',linear_result[0] #print 'the mean_squared_error of is ',linear_result[1] print'the MAPE of is ',linear_result[2]
# 集成-回归模型 model_gbr_disorder=GradientBoostingRegressor() model_gbr_disorder.fit(X_train,y_train) gbr_y_predict=model_gbr_disorder.predict(X_test) model_gbr_result = [] model_gbr_result = [metrics.mean_absolute_error(y_test,gbr_y_predict),metrics.mean_squared_error(y_test,gbr_y_predict),np.mean(np.abs((y_test - gbr_y_predict) / y_test))] print'use model_mlp predict:' # print 'the mean_absolute_error of is ',model_gbr_result[0] # print 'the mean_squared_error of is ',model_gbr_result[1] print'the MAPE of is ',model_gbr_result[2] gbr_score_disorder=model_gbr_disorder.score(X_test,y_test) print('sklearn集成-回归模型得分',gbr_score_disorder)#准确率较高 0.853817723868
1 2 3 4 5 6 7 8 9 10 11 12
# 多层感知器-回归模型 model_mlp = MLPRegressor(solver='lbfgs', hidden_layer_sizes=(20, 20, 20), random_state=1) model_mlp.fit(X_train,y_train) mlp_y_predict=model_mlp.predict(X_test) model_mlp_result = [] model_mlp_result = [metrics.mean_absolute_error(y_test,mlp_y_predict),metrics.mean_squared_error(y_test,mlp_y_predict),np.mean(np.abs((y_test - mlp_y_predict) / y_test))] print'use model_mlp predict:' #print 'the mean_absolute_error of is ',model_mlp_result[0] #print 'the mean_squared_error of is ',model_mlp_result[1] print'the MAPE of is ',model_mlp_result[2] mlp_score=model_mlp.score(X_test,y_test) print('sklearn多层感知器-回归模型得分',mlp_score)
from sklearn.svm import SVR #支持向量机 linear_svr=SVR(kernel='linear') #线性核函数配置 linear_svr.fit(X_train,y_train) linear_svr_y_predict=linear_svr.predict(X_test) linear_svr_result = [] linear_svr_result = [metrics.mean_absolute_error(y_test,linear_svr_y_predict),metrics.mean_squared_error(y_test,linear_svr_y_predict),np.sqrt(metrics.mean_squared_error(y_test, linear_svr_y_predict))] print'use svr predict:' print'the mean_absolute_error of is ',linear_svr_result[0] print'the mean_squared_error of is ',linear_svr_result[1] print'the sqrt_error of is ',linear_svr_result[2]
1 2 3 4 5 6 7 8 9 10
poly_svr=SVR(kernel='poly') #多项式核函数配置 poly_svr.fit(X_train,y_train) poly_svr_y_predict=poly_svr.predict(X_test) poly_svr_result = [] poly_svr_result = [metrics.mean_absolute_error(y_test,poly_svr_y_predict),metrics.mean_squared_error(y_test,poly_svr_y_predict),np.sqrt(metrics.mean_squared_error(y_test, poly_svr_y_predict))] print'use poly svr predict:' print'the mean_absolute_error of is ',poly_svr_result[0] print'the mean_squared_error of is ',poly_svr_result[1] print'the sqrt_error of is ',poly_svr_result[2]
1 2 3 4 5 6 7 8 9 10
rbf_svr=SVR(kernel='rbf') rbf_svr.fit(X_train,y_train) rbf_svr_y_predict=rbf_svr.predict(X_test) rbf_svr_result = [] rbf_svr_result = [metrics.mean_absolute_error(y_test,rbf_svr_y_predict),metrics.mean_squared_error(y_test,rbf_svr_y_predict),np.sqrt(metrics.mean_squared_error(y_test, rbf_svr_y_predict))] print'use rbf svr predict:' print'the mean_absolute_error of is ',rbf_svr_result[0] print'the mean_squared_error of is ',rbf_svr_result[1] print'the sqrt_error of is ',rbf_svr_result[2]
print'use rbf KNeighbor predict:' # print 'the mean_absolute_error of is ',uni_knr_result[0] # print 'the mean_squared_error of is ',uni_knr_result[1] print'the sqrt_error of is ',uni_knr_result[2]
print'use dis KNeighbor predict:' #print 'the mean_absolute_error of is ',dis_knr_result[0] #print 'the mean_squared_error of is ',dis_knr_result[1] print'the MAPE of is ',dis_knr_result[2]
决策树:
决策树是一种无监督的学习方法,用于分类和回归。它对数据中蕴含的决策规则建模,以预测目标变量的值。
1 2 3 4 5 6 7 8 9 10 11
#回归树 from sklearn.tree import DecisionTreeRegressor dtr=DecisionTreeRegressor() dtr.fit(X_train,y_train) dtr_y_predict=dtr.predict(X_test) dtr_result = [] dtr_result = [metrics.mean_absolute_error(y_test,dtr_y_predict),metrics.mean_squared_error(y_test,dtr_y_predict),np.mean(np.abs((y_test - dtr_y_predict) / y_test))] print'use rbf DecisionTreeRegressor predict:' # print 'the mean_absolute_error of is ',dtr_result[0] # print 'the mean_squared_error of is ',dtr_result[1] print'the MAPE of is ',dtr_result[2]
#随机森林 from sklearn.ensemble import RandomForestRegressor,ExtraTreesRegressor rfr=RandomForestRegressor() rfr.fit(X_train,y_train) rfr_y_predict=rfr.predict(X_test) rfr_result = [] rfr_result = [metrics.mean_absolute_error(y_test,rfr_y_predict),metrics.mean_squared_error(y_test,rfr_y_predict),np.mean(np.abs((y_test - rfr_y_predict) / y_test)) ] print'use rbf RandomForestRegressor predict:' # print 'the mean_absolute_error of is ',rfr_result[0] # print 'the mean_squared_error of is ',rfr_result[1] print'the MAPE of is ',rfr_result[2]
极端随机森林回归模型
1 2 3 4 5 6 7 8 9
etr=ExtraTreesRegressor() etr.fit(X_train,y_train) etr_y_predict=etr.predict(X_test) etr_result = [] etr_result = [metrics.mean_absolute_error(y_test,etr_y_predict),metrics.mean_squared_error(y_test,etr_y_predict),np.mean(np.abs((y_test - etr_y_predict) / y_test))] print'use rbf ExtraTreesRegressor predict:' #print 'the mean_absolute_error of is ',etr_result[0] #print 'the mean_squared_error of is ',etr_result[1] print'the MAPE of is ',etr_result[2]