training/testing 我的多项式回归 before/after 标准化时的不同 RMSE

Different RMSE when training/testing my polynomial regression before/after standardizing

我正在构建最终将被其他用户使用的回归模型。该模型用于通过使用多个大气变量(例如气温、湿度、太阳辐射、风等)来预测花朵温度。

经过大量研究后,我开始注意到通过 SKlearn 进行的二阶多项式回归为我的训练和测试数据提供了良好的 RMSE。然而,由于有超过 36 个系数发生共线性,并且根据对此 post 的评论:https://stats.stackexchange.com/questions/29781/when-conducting-multiple-regression-when-should-you-center-your-predictor-varia,共线性会干扰 beta,因此我得到的 RMSE 是不正确的。

我听说也许我应该标准化以消除共线性或使用正交分解,但我不知道哪个更好。在任何情况下,我都尝试过标准化我的 x 变量,当我为我的训练和测试数据计算 RMSE 时,我得到了相同的训练数据 RMSE 但不同的测试数据 RMSE。

代码如下:

import pandas as pd
import numpy as np 
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn import metrics

def OpenFile(ThePath):
    path = Location + ThePath
    Prepared_df = pd.read_csv(path, sep=',', encoding='utf-8')
    Prepared_df = Prepared_df.loc[:, ~Prepared_df.columns.str.contains('^Unnamed')]
    return(Prepared_df)

def EvaluateRegression(Test_data,Predict_data):
    MAE = np.round(metrics.mean_absolute_error(Test_data, Predict_data),3)
    MSE = np.round(metrics.mean_squared_error(Test_data, Predict_data),3)
    RMSE = np.round(np.sqrt(metrics.mean_squared_error(Test_data, Predict_data)),3)
    print('Mean absolute error :',MAE)
    print('Mean square error :',MSE)
    print('RMSE :',RMSE)
    return MAE,MSE,RMSE

#Read files ------------------------------------------------------------------------------------------------------------
Location = 'C:\Users\...'

#Training data
File_Station_day = 'Flower_Station_data_day.csv' #X training data
File_TD = 'Flower_Y_data_day.csv' #Y training data
Chosen_Air = OpenFile(File_Station_day)
Day_TC = OpenFile(File_TD)

#Testing data 
File_Fluke_Station= 'Fluke_Station_data.csv' #X testing data
File_Fluke = 'Flower_Fluke_data.csv' #Y testing data
Chosen_Air_Fluke = OpenFile(File_Fluke)
Fluke_Station = OpenFile(File_Fluke_Station)     

#Prepare data --------------------------------------------------------------------------------------------------------
y_train = Day_TC
y_test = Fluke_data
#Get the desired atmospheric variables
Air_cols = ['MAXTemp_data', 'MINTemp_data', 'Humidity', 'Precipitation', 'Pression', 'Arti_InSW', 'sin_time'] #Specify the desired atmospheriv variables
X_train = Chosen_Air[Air_cols]  
X_test = Chosen_Air_Fluke[Air_cols]

#If not standardizing
poly = PolynomialFeatures(degree=2)
linear_poly = LinearRegression()
X_train_rdy = poly.fit_transform(X_train)
linear_poly.fit(X_train_rdy,y_train)
X_test_rdy = poly.fit_transform(X_test)

Input_model= linear_poly
print('Regression: For train')
MAE, MSE, RMSE = EvaluateRegression(y_train, Input_model.predict(X_train_rdy))
#For testing data
print('Regression: For test')
MAE, MSE, RMSE = EvaluateRegression(y_test,  Input_model.predict(X_test_rdy))

#Output:
Regression: For train
Mean absolute error : 0.391
Mean square error : 0.256
RMSE : 0.506
Regression: For test
Mean absolute error : 0.652
Mean square error : 0.569
RMSE : 0.754

#If standardizing
std = StandardScaler()
X_train_std = pd.DataFrame(std.fit_transform(X_train),columns = Air_cols)
X_test_std = pd.DataFrame(std.fit_transform(X_test),columns = Air_cols)
poly = PolynomialFeatures(degree=2)
linear_poly_std = LinearRegression()
X_train_std_rdy = poly.fit_transform(X_train_std)
linear_poly_std.fit(X_train_std_rdy,y_train)
X_test_std_rdy = poly.fit_transform(X_test_std)

Input_model= linear_poly_std
print('Regression: For train')
MAE, MSE, RMSE = EvaluateRegression(y_train, Input_model.predict(X_train_std_rdy))
#For testing data
print('Regression: For test')
MAE, MSE, RMSE = EvaluateRegression(y_test,  Input_model.predict(X_test_std_rdy))

#Output:
Regression: For train
Mean absolute error : 0.391
Mean square error : 0.256
RMSE : 0.506
Regression: For test
Mean absolute error : 10.901
Mean square error : 304.53
RMSE : 17.451

为什么我为标准化测试数据获得的 RMSE 与非标准化测试数据如此不同?也许我这样做的方式一点都不好?如果我应该将文件附加到 post,请告诉我。

感谢您的宝贵时间!

IIRC,至少你不应该调用 poly.fit_transform 两次——你这样做的方式与回归模型相同——用训练数据拟合一次,稍后用测试转换。现在你正在重新训练缩放器(这可能会给你不同的mean/std),但应用相同的回归模型。

旁注:您的代码很难 read/debug,而且很容易导致简单 typos/mistakes。我建议您将训练逻辑包装在单个函数中,并可选择使用 sklearn pipelines。这将使测试缩放器 [un] 注释单行,从字面上看。