ParameterSampler#
- class sklearn.model_selection.ParameterSampler(param_distributions, n_iter, *, random_state=None)[source]#
从给定分布中采样的参数生成器。
用于超参数搜索的随机候选组合的非确定性可迭代对象。如果所有参数都以列表形式呈现,则执行无放回抽样。如果至少有一个参数以分布形式给出,则使用有放回抽样。强烈建议为连续参数使用连续分布。
欲了解更多信息,请阅读用户指南。
- 参数:
- param_distributionsdict
字典,键为参数名称(
str),值为要尝试的分布或参数列表。分布必须提供用于采样的rvs方法(例如来自 scipy.stats.distributions)。如果给出了列表,则进行均匀采样。如果给出了字典列表,则首先均匀采样一个字典,然后使用该字典按照上述方式采样一个参数。- n_iter整型
生成的参数设置的数量。
- random_stateint, RandomState instance or None, default=None
用于从可能值列表中进行随机均匀抽样的伪随机数生成器状态,而不是 scipy.stats 分布。传递一个整数以获得跨多个函数调用的可重现输出。请参阅 Glossary。
- 返回:
- paramsdict of str to any
Yields 映射每个估计器参数到采样值的字典。
示例
>>> 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