【发布时间】:2019-05-01 20:51:27
【问题描述】:
我在 sklearn 的 RandomForestRegressor 的帮助下尝试在 Boston 数据集上使用随机森林算法来预测 medv 的房价。总之,我尝试了 3 iterations 如下
迭代 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
所以我用 best 超参数训练了一个新模型,如下所示
#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}
迭代 3:我使用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
所以在 3 次迭代后我有以下问题
- 即使我使用
RandomSearchCV和GridSearchCV,为什么tuned 模型的结果比使用默认参数的模型最差。理想情况下,通过交叉验证进行调整时,模型应该会产生良好的结果 - 我知道交叉验证只会针对
param_grid中存在的值的组合进行。可能存在良好但未包含在我的param_grid中的值。那么我该如何处理这种情况 - 我如何决定我应该为
max_features、min_samples_split、max_depth或机器学习模型中的任何超参数尝试什么值的范围,以提高其准确性.(这样我至少可以得到一个比具有默认超参数的模型更好的调整模型)
【问题讨论】:
-
对于您的问题 #2 和 #3,没有“硬”的科学答案(甚至是广泛的指导方针);这是艺术的一部分,来自经验(可以说你自己已经获得了一些,这可能在实践中转化为“也总是尝试默认参数”);您的问题 #1 的可能答案可能在于您的问题 #2...
标签: python machine-learning scikit-learn random-forest grid-search