我试图在Boston数据集上使用随机森林算法,借助sklearn的RandomForestRegressor来预测房价
迭代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的结果
^{pr2}$迭代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}
迭代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次迭代之后,我有以下问题
param_grid
中的值的组合。可能有一些值是好的,但不包括在我的param_grid
中。那么我该如何处理这种情况呢max_features
、min_samples_split
、max_depth
或机器学习模型中的任何超参数来提高其准确性(这样我至少可以得到比默认超参数模型更好的优化模型)的值
你的第二个问题回答了你的第一个问题,但我试图在波士顿数据集上重现你的结果,我得到了}是网格搜索。然后我使用
{'test_rmse':3.987, 'train_rmse':1.442}
和默认参数,{'test_rmse':3.98, 'train_rmse':3.426}
是随机搜索的“优化”参数,而{hyperopt
和以下参数空间在大约200次测试后,结果是这样的, 所以我把这个空间扩大到}=34,对于这样一个小的数据集来说可能是很大的。在
'min_samples_split', 0.01, 1
,这使我得到了{'test_rmse':3.278, 'train_rmse':1.716}
的最佳结果,min_samples_split
等于0.01。根据文献资料,min_samples_split
的公式是ceil(min_samples_split * n_samples)
,在我们的例子中给出了{你不可能事先知道这一点,所以你必须对每个算法进行研究,看看通常会搜索到什么样的参数空间(这方面的好来源是kaggle,例如google
kaggle kernel random forest
),合并它们,考虑您的数据集特性,并使用某种Bayesian Optimization算法(有multiple existing libraries算法)对其进行优化,该算法尝试为新的参数值进行最佳选择。在相关问题 更多 >
编程相关推荐