一种方法是手工修改超参数,直到你找到超参数值的一个很好的组合。这将是非常繁琐的工作,您可能没有时间去探索许多超参数组合。

相反,你应该使用Scikit-Learn的GridSearchCV来为你搜索。你所需要做的就是告诉它你想让它操作哪些超参数,以及都尝试什么值,它会使用交叉验证来评估超参数值的所有可能组合。例如,下面的代码搜索关于随机森林回归[RandomForestRegressor]的超参数值的最佳组合。

from sklearn.model_selection import GridSearchCV
param_grid = [
    {'n_estimators': [3, 10, 30], 'max_features': [2, 4, 6, 8]},
    {'bootstrap': [False], 'n_estimators': [3, 10], 'max_features': [2, 3, 4]},
]
forest_reg = RandomForestRegressor()
grid_search = GridSearchCV(forest_reg, param_grid, cv=5,
                scoring='neg_mean_squared_error')
grid_search.fit(housing_prepared, housing_labels)

当您不知道一个超参数应该有什么值时,一个简单的方法是尝试连续的10次幂(如果您想要更细粒度的搜索,那么可以使用一个更小的值,如本例中所示的n_estimators超参数)。

这个param_grid告诉Scikit-Learn,根据传入额第一个dict字典,首先评估3×4 = 12个n_estimators和max_features超参数的组合(不要担心这些hyperparameters意味着什么;我们将在第7章解释),然后根据第二个传入的dict字典,来尝试所有2×3 = 6个超参数的组合,但这一次bootstrap超参数设置为False,而不是默认值True。

总而言之,网格搜索将探索12 + 6 = 18种随机森林超参数值的组合,它将训练每个模型5次(因为我们使用的是5-折交叉验证)。换句话说,总而言之,会有18 × 5 = 90轮训练!这可能需要很长时间,但当它完成时,你可以得到像这样的参数的最佳组合:

>>> grid_search.best_params_
{'max_features': 6, 'n_estimators': 30}

你也可以直接得到最好的估计器:

>>> grid_search.best_estimator_
RandomForestRegressor(bootstrap=True, criterion='mse', max_depth=None,
    max_features=6, max_leaf_nodes=None, min_samples_leaf=1,
    min_samples_split=2, min_weight_fraction_leaf=0.0,
    n_estimators=30, n_jobs=1, oob_score=False, random_state=None,
    verbose=0, warm_start=False)

如果GridSearchCV的refit配置为True(这也是默认值),那么一旦它找到了使用交叉验证的最佳估计器,它就会在整个训练集中重新训练它。这通常是一个好主意,因为提供更多的数据可能会提高它的性能。(简而言之,先以交叉验证评测出最佳的评估期,然后,使用全部的测试数据,再重新训练一下它)

当然,评估分数也是可用的:

>>> cvres = grid_search.cv_results_
... for mean_score, params in zip(cvres["mean_test_score"], cvres["params"]):
... print(np.sqrt(-mean_score), params)
...
64912.0351358 {'max_features': 2, 'n_estimators': 3}
55535.2786524 {'max_features': 2, 'n_estimators': 10}
52940.2696165 {'max_features': 2, 'n_estimators': 30}
60384.0908354 {'max_features': 4, 'n_estimators': 3}
52709.9199934 {'max_features': 4, 'n_estimators': 10}
50503.5985321 {'max_features': 4, 'n_estimators': 30}
59058.1153485 {'max_features': 6, 'n_estimators': 3}
52172.0292957 {'max_features': 6, 'n_estimators': 10}
49958.9555932 {'max_features': 6, 'n_estimators': 30}
59122.260006 {'max_features': 8, 'n_estimators': 3}
52441.5896087 {'max_features': 8, 'n_estimators': 10}
50041.4899416 {'max_features': 8, 'n_estimators': 30}
62371.1221202 {'bootstrap': False, 'max_features': 2, 'n_estimators': 3}
54572.2557534 {'bootstrap': False, 'max_features': 2, 'n_estimators': 10}
59634.0533132 {'bootstrap': False, 'max_features': 3, 'n_estimators': 3}
52456.0883904 {'bootstrap': False, 'max_features': 3, 'n_estimators': 10}
58825.665239 {'bootstrap': False, 'max_features': 4, 'n_estimators': 3}
52012.9945396 {'bootstrap': False, 'max_features': 4, 'n_estimators': 10}

在本例中,我们通过将max_features超参数设置为6,并将n_estimators超参数设置为30来获得最佳解决方案。这个组合的RMSE评分为49,959,比您之前使用默认超参数值(52,634)的分数稍好一点。祝贺您,您已经成功地调整了您的最佳模型!

不要忘记,您可以将一些数据准备步骤作为超参数处理。例如,网格搜索将自动发现是否添加您不确定的特性(例如,您的CombinedAttributesAdder转换器所使用的add_bedrooms_per_room超参数)。同样,它也可以用于自动找到处理异常值、特性缺失、特征选择等的最佳方法。

results matching ""

    No results matching ""