使用mae模型进行预测

时间:2020-03-12 19:21:25

标签: python python-3.x dataframe machine-learning scikit-learn

我正在使用几种模型来预测回归问题。我需要使用将给出最低错误率的模型。我已经为每个模型计算了mape,但是我需要编写代码,它将自动为我提供最低mape模型的预测。谁可以帮我这个事?我的代码的一部分在这里。 预先感谢。

print('\n\n---------Training Decision tree Model----------------')
dtree = AdaBoostRegressor(tree.DecisionTreeRegressor(random_state=1))
model_dt = dtree.fit(X_train_scaled, y_train)
pred_dt = dtree.predict(X_test_scaled)
mape_dt = np.mean((abs(pred_dt - y_test)/y_test)*100)
print('\nMean Absolute Percentage Error = {:.3f}'.format(mape_dt),'% ' )
print('R-squared score (training): {:.3f}'
     .format(dtree.score(X_train_scaled,y_train)))
print('R-squared score (test): {:.3f}'
     .format(dtree.score(X_test_scaled, y_test)))
result['dtree-pred']=pred_dt
result['error%_dtree']=mape_dt



#KNN
print('\n\n---------Training K-nearest neighbour Model----------------')
knn = AdaBoostRegressor(KNeighborsRegressor(n_neighbors = 5),random_state=1)
model_knn=knn.fit(X_train_scaled, y_train)
pred_knn = knn.predict(X_test_scaled)
mape_knn = np.mean((abs(pred_knn - y_test)/y_test)*100)
print('\nMean Absolute Percentage Error = {:.3f}'.format(mape_knn),'% ' )
print('R-squared score (training): {:.3f}'
     .format(knn.score(X_train_scaled,y_train)))
print('R-squared score (test): {:.3f}'
     .format(knn.score(X_test_scaled, y_test)))
result['knn-pred']=pred_knn
result['error%_knn']=mape_knn


#MLR
print('--------Training Linear Regression Model---------------')
lreg = AdaBoostRegressor(LinearRegression(),random_state=1)
model_lreg= lreg.fit(X_train_scaled,y_train)
pred_lreg = lreg.predict(X_test_scaled)
mape_lreg = np.mean((abs(pred_lreg - y_test)/y_test)*100)
print('\nMean Absolute Percentage Error = {:.3f}'.format(mape_lreg),'% ' )
print('R-squared score (training): {:.3f}'
     .format(lreg.score(X_train_scaled,y_train)))
print('R-squared score (test): {:.3f}'
     .format(lreg.score(X_test_scaled, y_test)))
result['mlr-pred']=pred_lreg


#Ridge
print('\n\n---------Training Ridge Regression Model----------------')
ridge = AdaBoostRegressor(Ridge(alpha=0),random_state=1)
model_ridge=ridge.fit(X_train_scaled,y_train)
pred_ridge = ridge.predict(X_test_scaled)
mape_ridge = np.mean((abs(pred_ridge - y_test)/y_test)*100)
print('\nMean Absolute Percentage Error =  {:.3f}'.format(mape_ridge),'% ' )
print('R-squared score (training): {:.3f}'
     .format(ridge.score(X_train_scaled,y_train)))
print('R-squared score (test): {:.3f}'
     .format(ridge.score(X_test_scaled, y_test)))
result['ridge-pred']=pred_ridge
result['error%_ridge']=mape_ridge

0 个答案:

没有答案