我是机器学习的新手,目前遇到了这个问题。首先,我使用线性回归来拟合训练集,但得到非常大的 RMSE。然后我尝试使用多项式回归来减少偏差。
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.metrics import mean_squared_error
poly_features = PolynomialFeatures(degree=2, include_bias=False)
X_poly = poly_features.fit_transform(X)
poly_reg = LinearRegression()
poly_reg.fit(X_poly, y)
poly_predict = poly_reg.predict(X_poly)
poly_mse = mean_squared_error(X, poly_predict)
poly_rmse = np.sqrt(poly_mse)
poly_rmse
Run Code Online (Sandbox Code Playgroud)
然后我得到了比线性回归稍微好一点的结果,然后我继续设置degree = 3/4/5,结果越来越好。但随着度数的增加,它可能会有些过拟合。
多项式的最佳次数应该是在交叉验证集中生成最低 RMSE 的次数。但我不知道如何实现这一目标。我应该使用 GridSearchCV 吗?或任何其他方法?
如果你能给我这个,非常感谢。