def to_dict_w_opt(model, metrics=None):
"""Serializes a sklearn model. Saves the parameters,
not the attributes.
Args:
model(sklearn.BaseEstimator): the model to serialize,
must be in SUPPORTED
metrics(list, optionnal): a list of metrics to monitor
Returns:
a dictionnary of the serialized model
"""
config = dict()
typestring = str(type(model))[8:][:-2]
config['config'] = typestring
attr = model.__dict__
for k, v in attr.items():
# check if parameter or attribute
if k[-1:] == '_':
# do not store attributes
pass
else:
config[k] = typeconversion(v)
# to be discussed :
# we add the metrics to the config even if it doesnt
# make sense for a sklearn model
# the metrics are then catch in model_from_dict_w_opt
if metrics is not None:
config['metrics'] = []
for m in metrics:
config['metrics'].append(m)
return config
评论列表
文章目录