本文整理汇总了Python中sklearn.model_selection.ParameterSampler方法的典型用法代码示例。如果您正苦于以下问题:Python model_selection.ParameterSampler方法的具体用法?Python model_selection.ParameterSampler怎么用?Python model_selection.ParameterSampler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.model_selection
的用法示例。
在下文中一共展示了model_selection.ParameterSampler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fit
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def fit(self, X, y=None):
"""Run fit on the estimator with randomly drawn parameters.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vector, where n_samples in the number of samples and
n_features is the number of features.
y : array-like, shape = [n_samples] or [n_samples, n_output], optional
Target relative to X for classification or regression;
None for unsupervised learning.
"""
sampled_params = ParameterSampler(self.param_distributions,
self.n_iter,
random_state=self.random_state)
# the super class will handle the X, y validation
return self._fit(X, y, sampled_params)
示例2: test_param_sampler
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def test_param_sampler():
# test basic properties of param sampler
param_distributions = {"kernel": ["rbf", "linear"],
"C": uniform(0, 1)}
sampler = ParameterSampler(param_distributions=param_distributions,
n_iter=10, random_state=0)
samples = [x for x in sampler]
assert_equal(len(samples), 10)
for sample in samples:
assert sample["kernel"] in ["rbf", "linear"]
assert 0 <= sample["C"] <= 1
# test that repeated calls yield identical parameters
param_distributions = {"C": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
sampler = ParameterSampler(param_distributions=param_distributions,
n_iter=3, random_state=0)
assert_equal([x for x in sampler], [x for x in sampler])
if sp_version >= (0, 16):
param_distributions = {"C": uniform(0, 1)}
sampler = ParameterSampler(param_distributions=param_distributions,
n_iter=10, random_state=0)
assert_equal([x for x in sampler], [x for x in sampler])
示例3: sample_hyperparameters
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def sample_hyperparameters(random_state, num):
space = {
'n_iter': N_ITER,
'batch_size': BATCH_SIZE,
'l2': L2,
'learning_rate': LEARNING_RATES,
'loss': LOSSES,
'embedding_dim': EMBEDDING_DIM,
}
sampler = ParameterSampler(space,
n_iter=num,
random_state=random_state)
for params in sampler:
yield params
示例4: sample_cnn_hyperparameters
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def sample_cnn_hyperparameters(random_state, num):
space = {
'n_iter': N_ITER,
'batch_size': BATCH_SIZE,
'l2': L2,
'learning_rate': LEARNING_RATES,
'loss': LOSSES,
'embedding_dim': EMBEDDING_DIM,
'kernel_width': [3, 5, 7],
'num_layers': list(range(1, 10)),
'dilation_multiplier': [1, 2],
'nonlinearity': ['tanh', 'relu'],
'residual': [True, False]
}
sampler = ParameterSampler(space,
n_iter=num,
random_state=random_state)
for params in sampler:
params['dilation'] = list(params['dilation_multiplier'] ** (i % 8)
for i in range(params['num_layers']))
yield params
示例5: sample_lstm_hyperparameters
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def sample_lstm_hyperparameters(random_state, num):
space = {
'n_iter': N_ITER,
'batch_size': BATCH_SIZE,
'l2': L2,
'learning_rate': LEARNING_RATES,
'loss': LOSSES,
'embedding_dim': EMBEDDING_DIM,
}
sampler = ParameterSampler(space,
n_iter=num,
random_state=random_state)
for params in sampler:
yield params
示例6: sample_pooling_hyperparameters
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def sample_pooling_hyperparameters(random_state, num):
space = {
'n_iter': N_ITER,
'batch_size': BATCH_SIZE,
'l2': L2,
'learning_rate': LEARNING_RATES,
'loss': LOSSES,
'embedding_dim': EMBEDDING_DIM,
}
sampler = ParameterSampler(space,
n_iter=num,
random_state=random_state)
for params in sampler:
yield params
示例7: fit
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def fit(self, X, y=None, groups=None):
"""Run fit on the estimator with randomly drawn parameters.
Parameters
----------
X : array-like, shape = [n_samples, n_features]
Training vector, where n_samples in the number of samples and
n_features is the number of features.
y : array-like, shape = [n_samples] or [n_samples, n_output], optional
Target relative to X for classification or regression;
None for unsupervised learning.
groups : array-like, with shape (n_samples,), optional
Group labels for the samples used while splitting the dataset into
train/test set.
"""
sampled_params = ParameterSampler(self.param_distributions,
self.n_iter,
random_state=self.random_state)
return self._fit(X, y, groups, sampled_params)
示例8: test_param_sampler
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def test_param_sampler():
# test basic properties of param sampler
param_distributions = {"kernel": ["rbf", "linear"],
"C": uniform(0, 1)}
sampler = ParameterSampler(param_distributions=param_distributions,
n_iter=10, random_state=0)
samples = [x for x in sampler]
assert_equal(len(samples), 10)
for sample in samples:
assert_true(sample["kernel"] in ["rbf", "linear"])
assert_true(0 <= sample["C"] <= 1)
# test that repeated calls yield identical parameters
param_distributions = {"C": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
sampler = ParameterSampler(param_distributions=param_distributions,
n_iter=3, random_state=0)
assert_equal([x for x in sampler], [x for x in sampler])
if sp_version >= (0, 16):
param_distributions = {"C": uniform(0, 1)}
sampler = ParameterSampler(param_distributions=param_distributions,
n_iter=10, random_state=0)
assert_equal([x for x in sampler], [x for x in sampler])
示例9: fit
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def fit(self, frame):
"""Fit the grid search.
Parameters
----------
frame : H2OFrame, shape=(n_samples, n_features)
The training frame on which to fit.
"""
sampled_params = ParameterSampler(self.param_grid,
self.n_iter,
random_state=self.random_state)
return self._fit(frame, sampled_params)
示例10: run_one_classifier
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def run_one_classifier(x, y, small = True, normalize_x = True, n_jobs=cpu_count()-1, brain=False, test_size=0.2, n_splits=5, upsample=True, scoring=None, verbose=False, grid_search=True):
all_params = (linear_models_n_params_small if small else linear_models_n_params) + (nn_models_n_params_small if small else nn_models_n_params) + ([] if small else gaussianprocess_models_n_params) + neighbor_models_n_params + (svm_models_n_params_small if small else svm_models_n_params) + (tree_models_n_params_small if small else tree_models_n_params)
all_params = random.choice(all_params)
return all_params[0](**(list(ParameterSampler(all_params[1], n_iter=1))[0]))
示例11: run_one_regressor
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def run_one_regressor(x, y, small = True, normalize_x = True, n_jobs=cpu_count()-1, brain=False, test_size=0.2, n_splits=5, upsample=True, scoring=None, verbose=False, grid_search=True):
all_params = (linear_models_n_params_small if small else linear_models_n_params) + (nn_models_n_params_small if small else nn_models_n_params) + ([] if small else gaussianprocess_models_n_params) + neighbor_models_n_params + (svm_models_n_params_small if small else svm_models_n_params) + (tree_models_n_params_small if small else tree_models_n_params)
all_params = random.choice(all_params)
return all_params[0](**(list(ParameterSampler(all_params[1], n_iter=1))[0]))
示例12: test_parameters_sampler_replacement
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def test_parameters_sampler_replacement():
# raise warning if n_iter is bigger than total parameter space
params = {'first': [0, 1], 'second': ['a', 'b', 'c']}
sampler = ParameterSampler(params, n_iter=7)
n_iter = 7
grid_size = 6
expected_warning = ('The total space of parameters %d is smaller '
'than n_iter=%d. Running %d iterations. For '
'exhaustive searches, use GridSearchCV.'
% (grid_size, n_iter, grid_size))
assert_warns_message(UserWarning, expected_warning,
list, sampler)
# degenerates to GridSearchCV if n_iter the same as grid_size
sampler = ParameterSampler(params, n_iter=6)
samples = list(sampler)
assert_equal(len(samples), 6)
for values in ParameterGrid(params):
assert values in samples
# test sampling without replacement in a large grid
params = {'a': range(10), 'b': range(10), 'c': range(10)}
sampler = ParameterSampler(params, n_iter=99, random_state=42)
samples = list(sampler)
assert_equal(len(samples), 99)
hashable_samples = ["a%db%dc%d" % (p['a'], p['b'], p['c'])
for p in samples]
assert_equal(len(set(hashable_samples)), 99)
# doesn't go into infinite loops
params_distribution = {'first': bernoulli(.5), 'second': ['a', 'b', 'c']}
sampler = ParameterSampler(params_distribution, n_iter=7)
samples = list(sampler)
assert_equal(len(samples), 7)
示例13: tuning
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def tuning(mode, n_iter, n_gpu, devices, save_interval, n_blocks, block_id):
if n_gpu == -1:
n_gpu = len(devices.split(','))
space = [
{
# 'loss': ['arcface', 'cosface'],
'loss': ['arcface', 'cosface', 'softmax'],
'epochs': [5],
'augmentation': ['soft'],
},
]
if mode == 'grid':
candidate_list = list(ParameterGrid(space))
elif mode == 'random':
candidate_list = list(ParameterSampler(space, n_iter, random_state=params['seed']))
else:
raise ValueError
n_per_block = math.ceil(len(candidate_list) / n_blocks)
candidate_chunk = candidate_list[block_id * n_per_block: (block_id + 1) * n_per_block]
utils.launch_tuning(mode=mode, n_iter=n_iter, n_gpu=n_gpu, devices=devices,
params=params, root=ROOT, save_interval=save_interval,
candidate_list=candidate_chunk)
示例14: tuning
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def tuning(mode, n_iter, n_gpu, devices, save_interval, n_blocks, block_id):
if n_gpu == -1:
n_gpu = len(devices.split(','))
space = [
{
'loss': ['arcface'],
# 'verifythresh': [20, 30, 40, 50],
# 'freqthresh': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
},
]
if mode == 'grid':
candidate_list = list(ParameterGrid(space))
elif mode == 'random':
candidate_list = list(ParameterSampler(space, n_iter, random_state=params['seed']))
else:
raise ValueError
n_per_block = math.ceil(len(candidate_list) / n_blocks)
candidate_chunk = candidate_list[block_id * n_per_block: (block_id + 1) * n_per_block]
utils.launch_tuning(mode=mode, n_iter=n_iter, n_gpu=n_gpu, devices=devices,
params=params, root=ROOT, save_interval=save_interval,
candidate_list=candidate_chunk)
示例15: _run_search
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ParameterSampler [as 别名]
def _run_search(self, evaluate_candidates):
"""Search n_iter candidates from param_distributions"""
ps = ParameterSampler(self.param_distributions, self.n_iter,
random_state=self.random_state)
evaluate_candidates(ps)