ParameterSampler#

class sklearn.model_selection.ParameterSampler(param_distributions, n_iter, *, random_state=None)#

生成器在给定分布中采样参数。

用于超参数搜索的随机候选组合的非确定性可迭代对象。如果所有参数都以列表形式呈现,则进行不放回采样。如果至少有一个参数以分布形式给出,则使用放回采样。强烈建议对连续参数使用连续分布。

更多信息请参阅 用户指南

Parameters:
param_distributionsdict

以参数名称 ( str ) 为键的字典,包含要尝试的参数分布或列表。分布必须提供用于采样的 rvs 方法(如 scipy.stats.distributions 中的那些)。如果给定一个列表,则均匀采样。如果给定一个字典列表,首先均匀采样一个字典,然后使用该字典如上所述采样参数。

n_iterint

生成的参数设置数量。

random_stateint, RandomState 实例或 None, 默认=None

用于从可能值列表中随机均匀采样的伪随机数生成器状态,而不是 scipy.stats 分布。传递一个 int 以在多次函数调用中获得可重现的输出。 参见 术语

Returns:
paramsdict of str to any

生成映射每个估计器参数到采样值的字典。

Examples

>>> from sklearn.model_selection import ParameterSampler
>>> from scipy.stats.distributions import expon
>>> import numpy as np
>>> rng = np.random.RandomState(0)
>>> param_grid = {'a':[1, 2], 'b': expon()}
>>> param_list = list(ParameterSampler(param_grid, n_iter=4,
...                                    random_state=rng))
>>> rounded_list = [dict((k, round(v, 6)) for (k, v) in d.items())
...                 for d in param_list]
>>> rounded_list == [{'b': 0.89856, 'a': 1},
...                  {'b': 0.923223, 'a': 1},
...                  {'b': 1.878964, 'a': 2},
...                  {'b': 1.038159, 'a': 2}]
True