我计算了3种不同训练模型的rmse,线性回归,决策树回归和随机森林。并且,基于它们中最低的rmse,我试图在网格搜索或随机搜索上进行微调。这里我有随机森林微调的代码。我不知道如何在线性回归和决策树模型上做同样的事情。
# GridSearchCV
from sklearn.model_selection import GridSearchCV
param_grid = [
# try 12 (3×4) combinations of hyperparameters
{'n_estimators': [3, 10, 30], 'max_features': [2, 4, 6, 8]},
# then try 6 (2×3) combinations with bootstrap set as False
{'bootstrap': [False], 'n_estimators': [3, 10], 'max_features': [2, 3, 4]},
]
if rmse_min is 2:
algo = RandomForestRegressor(random_state=42)
grid_search = GridSearchCV(algo, param_grid, cv=10,
scoring='neg_mean_squared_error')
elif rmse_min is 1:
algo = LinearRegression()
else:
algo = DecisionTreeRegressor(random_state=42)
# train across 10 folds
grid_search.fit(Melbourne_land_dataset_prepared, Melbourne_land_dataset_labels)