我正在尝试使用波士顿数据集上的随机森林算法来预测房价
所以我使用以下最佳超参数训练了一个新模型。
我的函数用于评估RMSE。
medv
,并借助sklearn的RandomForestRegressor
。总共进行了3次迭代
,如下所示:
迭代1:使用默认超参数的模型
#1. import the class/model
from sklearn.ensemble import RandomForestRegressor
#2. Instantiate the estimator
RFReg = RandomForestRegressor(random_state = 1, n_jobs = -1)
#3. Fit the model with data aka model training
RFReg.fit(X_train, y_train)
#4. Predict the response for a new observation
y_pred = RFReg.predict(X_test)
y_pred_train = RFReg.predict(X_train)
迭代1的结果
{'RMSE Test': 2.9850839211419435, 'RMSE Train': 1.2291604936401441}
迭代2:我使用RandomizedSearchCV来获取超参数的最佳值
from sklearn.ensemble import RandomForestRegressor
RFReg = RandomForestRegressor(n_estimators = 500, random_state = 1, n_jobs = -1)
param_grid = {
'max_features' : ["auto", "sqrt", "log2"],
'min_samples_split' : np.linspace(0.1, 1.0, 10),
'max_depth' : [x for x in range(1,20)]
from sklearn.model_selection import RandomizedSearchCV
CV_rfc = RandomizedSearchCV(estimator=RFReg, param_distributions =param_grid, n_jobs = -1, cv= 10, n_iter = 50)
CV_rfc.fit(X_train, y_train)
所以我得到的最佳超参数如下
CV_rfc.best_params_
#{'min_samples_split': 0.1, 'max_features': 'auto', 'max_depth': 18}
CV_rfc.best_score_
#0.8021713812777814
所以我使用以下最佳超参数训练了一个新模型。
#1. import the class/model
from sklearn.ensemble import RandomForestRegressor
#2. Instantiate the estimator
RFReg = RandomForestRegressor(n_estimators = 500, random_state = 1, n_jobs = -1, min_samples_split = 0.1, max_features = 'auto', max_depth = 18)
#3. Fit the model with data aka model training
RFReg.fit(X_train, y_train)
#4. Predict the response for a new observation
y_pred = RFReg.predict(X_test)
y_pred_train = RFReg.predict(X_train)
迭代2的结果
{'RMSE Test': 3.2836794902147926, 'RMSE Train': 2.71230367772569}
迭代三: 我使用GridSearchCV获取超参数的最优值
from sklearn.ensemble import RandomForestRegressor
RFReg = RandomForestRegressor(n_estimators = 500, random_state = 1, n_jobs = -1)
param_grid = {
'max_features' : ["auto", "sqrt", "log2"],
'min_samples_split' : np.linspace(0.1, 1.0, 10),
'max_depth' : [x for x in range(1,20)]
}
from sklearn.model_selection import GridSearchCV
CV_rfc = GridSearchCV(estimator=RFReg, param_grid=param_grid, cv= 10, n_jobs = -1)
CV_rfc.fit(X_train, y_train)
所以我得到的最佳超参数如下
CV_rfc.best_params_
#{'max_depth': 12, 'max_features': 'auto', 'min_samples_split': 0.1}
CV_rfc.best_score_
#0.8021820114800677
迭代 3 的结果
{'RMSE Test': 3.283690568225705, 'RMSE Train': 2.712331014201783}
我的函数用于评估RMSE。
def model_evaluate(y_train, y_test, y_pred, y_pred_train):
metrics = {}
#RMSE Test
rmse_test = np.sqrt(mean_squared_error(y_test, y_pred))
#RMSE Train
rmse_train = np.sqrt(mean_squared_error(y_train, y_pred_train))
metrics = {
'RMSE Test': rmse_test,
'RMSE Train': rmse_train}
return metrics
在进行了三次迭代后,我有以下问题:
- 为什么经过调参的模型的结果比使用默认参数的模型还要差,即使我使用了
RandomSearchCV
和GridSearchCV
。理论上,在交叉验证的情况下,经过调参的模型应该会给出更好的结果。 - 我知道交叉验证只会针对
param_grid
中存在的值的组合进行。可能存在一些不在我的param_grid
中但是很好的值。那么我该如何处理这种情况? - 我该如何决定尝试哪些取值范围来增加机器学习模型的准确性,例如
max_features
、min_samples_split
、max_depth
或者其他任何超参数。(这样我就至少可以得到比使用默认超参数的模型更好的调参模型)