对于以下Alpha值,我试图获取每个Alpha的RMSE。但是,岭回归默认alpha值设置为1,而我没有获得每个alpha的RMSE。我的错误在哪里?
alphas = [5.0,2.5,0.05]
def ridge_regression(X_train,y_train,X_test, y_test):
coefs2 = []
for a in alphas:
ridge = Ridge(normalize = False)
ridge.set_params(alpha = a)
ridge.fit(X_train, y_train)
ridge.fit(X_test, y_test)
p = ridge.predict(X_train)
q = ridge.predict(X_test)
err_train = p-(y_train)
err_test= q - (y_test)
total_error_train = np.dot(err_train,err_train)
total_error_test = np.dot (err_test, err_test)
rmse_train = np.sqrt(total_error_train/len(p))
rmse_test = np.sqrt(total_error_test /len(q))
rmse_ridge_train = []
rmse_ridge_test = []
rmse_ridge_train.append(rmse_train)
rmse_ridge_test.append(rmse_test)
coefs.append(ridge.coef_)
#np.shape(coefs)
return coefs2,rmse_ridge_train,rmse_ridge_test
感谢您的回复。