我正在使用H2O(Python),我正在使用H2OGridSearch
来获取GLM(H2OGeneralizedLinearEstimator
)的alpha值,同时使用lambda_search=True
使用k-fold交叉验证。< / p>
如何获得最佳模型的lambda值?
编辑:完全可重复的示例
数据:
34.40 17:1 73:1 127:1 265:1 912:1 1162:1 1512:1 1556:1 1632:1 1738:1
205.10 127:1 138:1 338:1 347:1 883:1 912:1 1120:1 1122:1 1512:1
7.75 66:1 127:1 347:1 602:1 1422:1 1512:1 1535:1 1738:1
8.85 127:1 608:1 906:1 979:1 1077:1 1512:1 1738:1
51.80 127:1 347:1 608:1 766:1 912:1 928:1 952:1 1034:1 1512:1 1610:1 1738:1
110.00 127:1 229:1 347:1 602:1 608:1 1171:1 1512:1 1718:1
8.90 66:1 127:1 205:1 347:1 490:1 589:1 912:1 1016:1 1512:1
将此文件称为h2o_example.svmlight
然后运行:
h2o_data = h2o.import_file("h2o_example.svmlight")
cols = h2o_data.columns[1:]
hyper_parameters = {"alpha": [0.0, 0.01, 0.99, 1.0]}
grid = H2OGridSearch(H2OGeneralizedLinearEstimator(family="gamma", link="log", lambda_search=True, nfolds=2, intercept=True, standardize=False),
hyper_params=hyper_parameters)
grid.train(y="C1", x=cols, training_frame=h2o_data)
grid_table = grid.get_grid(sort_by="r2", decreasing=True)
best = grid_table.models[0]
best.actual_params["lambda"]
best.actual_params["alpha"]
最后两个命令失败,给我一个错误:
TypeError: 'property' object has no attribute '__getitem__'
显然,我以错误的方式使用lambda_search
。如何根据我的标准获得最佳模型的单个alpha和lambda值?
答案 0 :(得分:3)
最终编辑
获取lambda有多种方法(如下所示),但这里有两种简明的方法来获取lambda。(注意完全可重现的代码位于底部)
如果您有lambda_search = True
,则可以查看lambda_search
列下的模型摘要表,看看为lambda.min
设置了哪个值,这是您最好的lambda
model.summary()['lambda_search']
将生成一个包含类似于:
的字符串的列表['nlambda = 100, lambda.max = 12.733, lambda.min = 0.05261, lambda.1se = -1.0']
如果您不使用lambda搜索并且没有设置lambda值(或设置它),您也可以使用摘要表
model.summary()['regularization']
输出如下:
['Elastic Net (alpha = 0.5, lambda = 0.01289 )']
其他选项:
查看模型的实际参数:
best.actual_params['lambda']
best.actual_params['alpha']
其中best
是网格搜索结果中的最佳模型
首次编辑
获得您可以做的最佳模型
grid_table = grid.get_grid(sort_by='r2', decreasing=True)
best = grid_table.models[0]
然后你可以使用:
best.actual_params['lambda']
完全可重复的示例
import h2o
from h2o.estimators.glm import H2OGeneralizedLinearEstimator
h2o.init()
# import the airlines dataset:
# This dataset is used to classify whether a flight will be delayed 'YES' or not "NO"
# original data can be found at http://www.transtats.bts.gov/
airlines= h2o.import_file("https://s3.amazonaws.com/h2o-public-test-data/smalldata/airlines/allyears2k_headers.zip")
# convert columns to factors
airlines["Year"]= airlines["Year"].asfactor()
airlines["Month"]= airlines["Month"].asfactor()
airlines["DayOfWeek"] = airlines["DayOfWeek"].asfactor()
airlines["Cancelled"] = airlines["Cancelled"].asfactor()
airlines['FlightNum'] = airlines['FlightNum'].asfactor()
# set the predictor names and the response column name
predictors = ["Origin", "Dest", "Year", "UniqueCarrier", "DayOfWeek", "Month", "Distance", "FlightNum"]
response = "IsDepDelayed"
# split into train and validation sets
train, valid= airlines.split_frame(ratios = [.8])
# try using the `lambda_` parameter:
# initialize your estimator
airlines_glm = H2OGeneralizedLinearEstimator(family = 'binomial', lambda_ = .0001)
# then train your model
airlines_glm.train(x = predictors, y = response, training_frame = train, validation_frame = valid)
# print the auc for the validation data
print(airlines_glm.auc(valid=True))
# Example of values to grid over for `lambda`
# import Grid Search
from h2o.grid.grid_search import H2OGridSearch
# select the values for lambda_ to grid over
hyper_params = {'lambda': [1, 0.5, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0]}
# this example uses cartesian grid search because the search space is small
# and we want to see the performance of all models. For a larger search space use
# random grid search instead: {'strategy': "RandomDiscrete"}
# initialize the glm estimator
airlines_glm_2 = H2OGeneralizedLinearEstimator(family = 'binomial')
# build grid search with previously made GLM and hyperparameters
grid = H2OGridSearch(model = airlines_glm_2, hyper_params = hyper_params,
search_criteria = {'strategy': "Cartesian"})
# train using the grid
grid.train(x = predictors, y = response, training_frame = train, validation_frame = valid)
# sort the grid models by decreasing AUC
grid_table = grid.get_grid(sort_by = 'auc', decreasing = True)
print(grid_table)
best = grid_table.models[0]
print(best.actual_params['lambda'])
答案 1 :(得分:0)
我不确定为什么以下不起作用
best = grid_table.models[0]
best.actual_params["lambda"]
best.actual_params["alpha"]
这可能是h2o的一个问题,但如果您将上述内容更改为以下内容,您应该至少可以访问这些参数:
best = grid.models[x]
best.actual_params["lambda"]
best.actual_params["alpha"]
请注意,我已将0
更改为x
,因为您需要注意哪个模型根据您的错误条件执行得最好,因为grid
中的内容可能无法按照grid_table
排序你的错误标准。这需要您查看grid
并注意model_id并查看模型如何存储在lambda
然后您应该至少能够引用alpha
和lambda
。但是,当您在alpha上运行网格搜索并且通过lambda_search
属性best.actual_params["lambda"]
启用lambda_search
的搜索时,将返回搜索到的lambda的完整列表。您仍然可以通过考虑Lauren建议的内容来引用它,但我通常希望看到表中的所有内容并建议关闭import numpy as np
lambda_search_range = list(np.linspace(0,1,100))
h2o_data = h2o.import_file("h2o_example.svmlight")
cols = h2o_data.columns[1:]
hyper_parameters = {"alpha": [0.0, 0.01, 0.99, 1.0],
"lambda": lambda_search_range}
grid = H2OGridSearch(H2OGeneralizedLinearEstimator(family="gamma",
link="log", lambda_search=False, nfolds=2,
intercept=True, standardize=False), hyper_params=hyper_parameters)
grid.train(y="C1", x=cols, training_frame=h2o_data)
grid_table = grid.get_grid(sort_by="r2", decreasing=True)
param_dict = grid_table.get_hyperparams_dict(grid_table.model_ids[0])
并将其添加到您搜索的超参数中。
param_dict
=IF(ISNA(INDEX(INDIRECT("'PURCHASING'!"&TEXT(ranges!G12,"")),MATCH("Lower bound (weeks)",INDIRECT("purchasing!"&TEXT(ranges!G12,"")),0)+1,1)),"NULL",INDEX(INDIRECT("'PURCHASING'!"&TEXT(ranges!G12,"")),MATCH("Lower bound (weeks)",INDIRECT("purchasing!"&TEXT(ranges!G12,"")),0)+1,1))
应该是一个字典,根据您指定的错误标准,包含最佳模型的alpha和lambda值。