本文整理汇总了Python中sklearn.utils.check_random_state函数的典型用法代码示例。如果您正苦于以下问题:Python check_random_state函数的具体用法?Python check_random_state怎么用?Python check_random_state使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了check_random_state函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _iter_test_indices
def _iter_test_indices(self, X, y=None, groups=None):
"""Internal method for providing scikit-learn's split with folds
Parameters
----------
X : array-like, shape (n_samples, n_features)
Training data, where n_samples is the number of samples
and n_features is the number of features.
Note that providing ``y`` is sufficient to generate the splits and
hence ``np.zeros(n_samples)`` may be used as a placeholder for
``X`` instead of actual training data.
y : array-like, shape (n_samples,)
The target variable for supervised learning problems.
Stratification is done based on the y labels.
groups : object
Always ignored, exists for compatibility.
Yields
------
fold : List[int]
indexes of test samples for a given fold, yielded for each of the folds
"""
if self.random_state:
check_random_state(self.random_state)
rows, rows_used, all_combinations, per_row_combinations, samples_with_combination, folds = \
self._prepare_stratification(y)
self._distribute_positive_evidence(rows_used, folds, samples_with_combination, per_row_combinations)
self._distribute_negative_evidence(rows_used, folds)
for fold in folds:
yield fold
示例2: __init__
def __init__(self, dataset_properties, random_state=None):
"""
Parameters
----------
dataset_properties : dict
Describes the dataset to work on, this can change the
configuration space constructed by auto-sklearn. Mandatory
properties are:
* target_type: classification or regression
Optional properties are:
* multiclass: whether the dataset is a multiclass classification
dataset.
* multilabel: whether the dataset is a multilabel classification
dataset
"""
# Since all calls to get_hyperparameter_search_space will be done by the
# pipeline on construction, it is not necessary to construct a
# configuration space at this location!
# self.configuration = self.get_hyperparameter_search_space(
# dataset_properties).get_default_configuration()
if random_state is None:
self.random_state = check_random_state(1)
else:
self.random_state = check_random_state(random_state)
# Since the pipeline will initialize the hyperparameters, it is not
# necessary to do this upon the construction of this object
# self.set_hyperparameters(self.configuration)
self.choice = None
示例3: test_number_of_subsets_of_features
def test_number_of_subsets_of_features():
# In RFE, 'number_of_subsets_of_features'
# = the number of iterations in '_fit'
# = max(ranking_)
# = 1 + (n_features + step - n_features_to_select - 1) // step
# After optimization #4534, this number
# = 1 + np.ceil((n_features - n_features_to_select) / float(step))
# This test case is to test their equivalence, refer to #4534 and #3824
def formula1(n_features, n_features_to_select, step):
return 1 + ((n_features + step - n_features_to_select - 1) // step)
def formula2(n_features, n_features_to_select, step):
return 1 + np.ceil((n_features - n_features_to_select) / float(step))
# RFE
# Case 1, n_features - n_features_to_select is divisible by step
# Case 2, n_features - n_features_to_select is not divisible by step
n_features_list = [11, 11]
n_features_to_select_list = [3, 3]
step_list = [2, 3]
for n_features, n_features_to_select, step in zip(
n_features_list, n_features_to_select_list, step_list):
generator = check_random_state(43)
X = generator.normal(size=(100, n_features))
y = generator.rand(100).round()
rfe = RFE(estimator=SVC(kernel="linear"),
n_features_to_select=n_features_to_select, step=step)
rfe.fit(X, y)
# this number also equals to the maximum of ranking_
assert_equal(np.max(rfe.ranking_),
formula1(n_features, n_features_to_select, step))
assert_equal(np.max(rfe.ranking_),
formula2(n_features, n_features_to_select, step))
# In RFECV, 'fit' calls 'RFE._fit'
# 'number_of_subsets_of_features' of RFE
# = the size of 'grid_scores' of RFECV
# = the number of iterations of the for loop before optimization #4534
# RFECV, n_features_to_select = 1
# Case 1, n_features - 1 is divisible by step
# Case 2, n_features - 1 is not divisible by step
n_features_to_select = 1
n_features_list = [11, 10]
step_list = [2, 2]
for n_features, step in zip(n_features_list, step_list):
generator = check_random_state(43)
X = generator.normal(size=(100, n_features))
y = generator.rand(100).round()
rfecv = RFECV(estimator=SVC(kernel="linear"), step=step, cv=5)
rfecv.fit(X, y)
assert_equal(rfecv.grid_scores_.shape[0],
formula1(n_features, n_features_to_select, step))
assert_equal(rfecv.grid_scores_.shape[0],
formula2(n_features, n_features_to_select, step))
示例4: test_xgboost_random_states
def test_xgboost_random_states():
X, y, weights = generate_classification_data(n_classes=2, distance=5)
for random_state in [145, None, check_random_state(None), check_random_state(145)]:
clf1 = XGBoostClassifier(n_estimators=5, max_depth=1, subsample=0.1, random_state=random_state)
clf1.fit(X, y)
clf2 = XGBoostClassifier(n_estimators=5, max_depth=1, subsample=0.1, random_state=random_state)
clf2.fit(X, y)
if isinstance(random_state, numpy.random.RandomState):
assert not numpy.allclose(clf1.predict_proba(X), clf2.predict_proba(X)), 'seed: {}'.format(random_state)
else:
assert numpy.allclose(clf1.predict_proba(X), clf2.predict_proba(X)), 'seed: {}'.format(random_state)
示例5: _iter_test_indices
def _iter_test_indices(self, frame, y=None):
n_obs = frame.shape[0]
indices = np.arange(n_obs)
if self.shuffle:
check_random_state(self.random_state).shuffle(indices)
n_folds = self.n_folds
fold_sizes = (n_obs // n_folds) * np.ones(n_folds, dtype=np.int)
fold_sizes[:n_obs % n_folds] += 1
current = 0
for fold_size in fold_sizes:
start, stop = current, current + fold_size
yield indices[start:stop]
current = stop
示例6: _init_fit
def _init_fit(self, n_features):
"""Initialize weight and bias parameters."""
rng = check_random_state(self.random_state)
weight_init_bound1 = np.sqrt(6. / (n_features + self.n_hidden))
weight_init_bound2 = np.sqrt(6. / (n_features + self.n_hidden))
rng = check_random_state(self.random_state)
self.coef_hidden_ = rng.uniform(-weight_init_bound1, weight_init_bound1, (n_features, self.n_hidden))
rng = check_random_state(self.random_state)
self.intercept_hidden_ = rng.uniform(-weight_init_bound1, weight_init_bound1, self.n_hidden)
rng = check_random_state(self.random_state)
self.coef_output_ = rng.uniform(-weight_init_bound2, weight_init_bound2, (self.n_hidden, n_features))
rng = check_random_state(self.random_state)
self.intercept_output_ = rng.uniform(-weight_init_bound2, weight_init_bound2, n_features)
示例7: test_make_rng
def test_make_rng():
"""Check the check_random_state utility function behavior"""
assert check_random_state(None) is np.random.mtrand._rand
assert check_random_state(np.random) is np.random.mtrand._rand
rng_42 = np.random.RandomState(42)
assert check_random_state(42).randint(100) == rng_42.randint(100)
rng_42 = np.random.RandomState(42)
assert check_random_state(rng_42) is rng_42
rng_42 = np.random.RandomState(42)
assert check_random_state(43).randint(100) != rng_42.randint(100)
assert_raises(ValueError, check_random_state, "some invalid seed")
示例8: run_stochastic_models
def run_stochastic_models(self,
params,
n_input_sample,
return_input_samples=True,
random_state=None,
verbose=False):
"""This function considers the model output as a stochastic function by
taking the dependence parameters as inputs.
Parameters
----------
params : list, or `np.ndarray`
The list of parameters associated to the predefined copula.
n_input_sample : int, optional (default=1)
The number of evaluations for each parameter
random_state :
"""
check_random_state(random_state)
func = self.model_func
# Get all the input_sample
if verbose:
print('Time taken:', time.clock())
print('Creating the input samples')
input_samples = []
for param in params:
full_param = np.zeros((self._corr_dim, ))
full_param[self._pair_ids] = param
full_param[self._fixed_pairs_ids] = self._fixed_params_list
intput_sample = self._get_sample(full_param, n_input_sample)
input_samples.append(intput_sample)
if verbose:
print('Time taken:', time.clock())
print('Evaluate the input samples')
# Evaluate the through the model
outputs = func(np.concatenate(input_samples))
# List of output sample for each param
output_samples = np.split(outputs, len(params))
if verbose:
print('Time taken:', time.clock())
if return_input_samples:
return output_samples, input_samples
else:
return output_samples
示例9: rvs
def rvs(self, n=1, random_state=None):
"""Generate random samples from the model.
Parameters
----------
n : int
Number of samples to generate.
Returns
-------
obs : array_like, length `n`
List of samples
"""
random_state = check_random_state(random_state)
startprob_pdf = self.startprob
startprob_cdf = np.cumsum(startprob_pdf)
transmat_pdf = self.transmat
transmat_cdf = np.cumsum(transmat_pdf, 1)
# Initial state.
rand = random_state.rand()
currstate = (startprob_cdf > rand).argmax()
obs = [self._generate_sample_from_state(
currstate, random_state=random_state)]
for x in xrange(n - 1):
rand = random_state.rand()
currstate = (transmat_cdf[currstate] > rand).argmax()
obs.append(self._generate_sample_from_state(
currstate, random_state=random_state))
return np.array(obs)
示例10: test_space_net_alpha_grid_pure_spatial
def test_space_net_alpha_grid_pure_spatial():
rng = check_random_state(42)
X = rng.randn(10, 100)
y = np.arange(X.shape[0])
for is_classif in [True, False]:
assert_false(np.any(np.isnan(_space_net_alpha_grid(
X, y, l1_ratio=0., logistic=is_classif))))
示例11: random_non_singular
def random_non_singular(p, sing_min=1., sing_max=2., random_state=0):
"""Generate a random nonsingular matrix.
Parameters
----------
p : int
The first dimension of the array.
sing_min : float, optional (default to 1.)
Minimal singular value.
sing_max : float, optional (default to 2.)
Maximal singular value.
random_state : int or numpy.random.RandomState instance, optional
random number generator, or seed.
Returns
-------
output : numpy.ndarray, shape (p, p)
A nonsingular matrix with the given minimal and maximal singular
values.
"""
random_state = check_random_state(random_state)
diag = random_diagonal(p, v_min=sing_min, v_max=sing_max,
random_state=random_state)
mat1 = random_state.randn(p, p)
mat2 = random_state.randn(p, p)
unitary1, _ = linalg.qr(mat1)
unitary2, _ = linalg.qr(mat2)
return unitary1.dot(diag).dot(unitary2.T)
示例12: random_diagonal
def random_diagonal(p, v_min=1., v_max=2., random_state=0):
"""Generate a random diagonal matrix.
Parameters
----------
p : int
The first dimension of the array.
v_min : float, optional (default to 1.)
Minimal element.
v_max : float, optional (default to 2.)
Maximal element.
random_state : int or numpy.random.RandomState instance, optional
random number generator, or seed.
Returns
-------
output : numpy.ndarray, shape (p, p)
A diagonal matrix with the given minimal and maximal elements.
"""
random_state = check_random_state(random_state)
diag = random_state.rand(p) * (v_max - v_min) + v_min
diag[diag == np.amax(diag)] = v_max
diag[diag == np.amin(diag)] = v_min
return np.diag(diag)
示例13: test_accessible_kl_divergence
def test_accessible_kl_divergence():
# Ensures that the accessible kl_divergence matches the computed value
random_state = check_random_state(0)
X = random_state.randn(100, 2)
tsne = TSNE(n_iter_without_progress=2, verbose=2,
random_state=0, method='exact')
old_stdout = sys.stdout
sys.stdout = StringIO()
try:
tsne.fit_transform(X)
finally:
out = sys.stdout.getvalue()
sys.stdout.close()
sys.stdout = old_stdout
# The output needs to contain the accessible kl_divergence as the error at
# the last iteration
for line in out.split('\n')[::-1]:
if 'Iteration' in line:
_, _, error = line.partition('error = ')
if error:
error, _, _ = error.partition(',')
break
assert_almost_equal(tsne.kl_divergence_, float(error), decimal=5)
示例14: test_gradient
def test_gradient():
# Test gradient of Kullback-Leibler divergence.
random_state = check_random_state(0)
n_samples = 50
n_features = 2
n_components = 2
alpha = 1.0
distances = random_state.randn(n_samples, n_features).astype(np.float32)
distances = np.abs(distances.dot(distances.T))
np.fill_diagonal(distances, 0.0)
X_embedded = random_state.randn(n_samples, n_components).astype(np.float32)
P = _joint_probabilities(distances, desired_perplexity=25.0,
verbose=0)
def fun(params):
return _kl_divergence(params, P, alpha, n_samples, n_components)[0]
def grad(params):
return _kl_divergence(params, P, alpha, n_samples, n_components)[1]
assert_almost_equal(check_grad(fun, grad, X_embedded.ravel()), 0.0,
decimal=5)
示例15: test_oneclass_decision_function
def test_oneclass_decision_function():
# Test OneClassSVM decision function
clf = svm.OneClassSVM()
rnd = check_random_state(2)
# Generate train data
X = 0.3 * rnd.randn(100, 2)
X_train = np.r_[X + 2, X - 2]
# Generate some regular novel observations
X = 0.3 * rnd.randn(20, 2)
X_test = np.r_[X + 2, X - 2]
# Generate some abnormal novel observations
X_outliers = rnd.uniform(low=-4, high=4, size=(20, 2))
# fit the model
clf = svm.OneClassSVM(nu=0.1, kernel="rbf", gamma=0.1)
clf.fit(X_train)
# predict things
y_pred_test = clf.predict(X_test)
assert_greater(np.mean(y_pred_test == 1), .9)
y_pred_outliers = clf.predict(X_outliers)
assert_greater(np.mean(y_pred_outliers == -1), .9)
dec_func_test = clf.decision_function(X_test)
assert_array_equal((dec_func_test > 0).ravel(), y_pred_test == 1)
dec_func_outliers = clf.decision_function(X_outliers)
assert_array_equal((dec_func_outliers > 0).ravel(), y_pred_outliers == 1)