本文整理汇总了Python中sklearn.model_selection.ShuffleSplit方法的典型用法代码示例。如果您正苦于以下问题:Python model_selection.ShuffleSplit方法的具体用法?Python model_selection.ShuffleSplit怎么用?Python model_selection.ShuffleSplit使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.model_selection
的用法示例。
在下文中一共展示了model_selection.ShuffleSplit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Train
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def Train(self, C, A, Y, SF):
'''
Train the classifier using the sample matrix A and target matrix Y
'''
C.fit(A, Y)
YH = np.zeros(Y.shape, dtype = np.object)
for i in np.array_split(np.arange(A.shape[0]), 32): #Split up verification into chunks to prevent out of memory
YH[i] = C.predict(A[i])
s1 = SF(Y, YH)
print('All:{:8.6f}'.format(s1))
'''
ss = ShuffleSplit(random_state = 1151) #Use fixed state for so training can be repeated later
trn, tst = next(ss.split(A, Y)) #Make train/test split
mi = [8] * 1 #Maximum number of iterations at each iter
YH = np.zeros((A.shape[0]), dtype = np.object)
for mic in mi: #Chunk size to split dataset for CV results
#C.SetMaxIter(mic) #Set the maximum number of iterations to run
#C.fit(A[trn], Y[trn]) #Perform training iterations
'''
示例2: test_safe_split_with_precomputed_kernel
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def test_safe_split_with_precomputed_kernel():
clf = SVC()
clfp = SVC(kernel="precomputed")
iris = datasets.load_iris()
X, y = iris.data, iris.target
K = np.dot(X, X.T)
cv = ShuffleSplit(test_size=0.25, random_state=0)
train, test = list(cv.split(X))[0]
X_train, y_train = _safe_split(clf, X, y, train)
K_train, y_train2 = _safe_split(clfp, K, y, train)
assert_array_almost_equal(K_train, np.dot(X_train, X_train.T))
assert_array_almost_equal(y_train, y_train2)
X_test, y_test = _safe_split(clf, X, y, test, train)
K_test, y_test2 = _safe_split(clfp, K, y, test, train)
assert_array_almost_equal(K_test, np.dot(X_test, X_train.T))
assert_array_almost_equal(y_test, y_test2)
示例3: test_2d_y
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def test_2d_y():
# smoke test for 2d y and multi-label
n_samples = 30
rng = np.random.RandomState(1)
X = rng.randint(0, 3, size=(n_samples, 2))
y = rng.randint(0, 3, size=(n_samples,))
y_2d = y.reshape(-1, 1)
y_multilabel = rng.randint(0, 2, size=(n_samples, 3))
groups = rng.randint(0, 3, size=(n_samples,))
splitters = [LeaveOneOut(), LeavePOut(p=2), KFold(), StratifiedKFold(),
RepeatedKFold(), RepeatedStratifiedKFold(),
ShuffleSplit(), StratifiedShuffleSplit(test_size=.5),
GroupShuffleSplit(), LeaveOneGroupOut(),
LeavePGroupsOut(n_groups=2), GroupKFold(), TimeSeriesSplit(),
PredefinedSplit(test_fold=groups)]
for splitter in splitters:
list(splitter.split(X, y, groups))
list(splitter.split(X, y_2d, groups))
try:
list(splitter.split(X, y_multilabel, groups))
except ValueError as e:
allowed_target_types = ('binary', 'multiclass')
msg = "Supported target types are: {}. Got 'multilabel".format(
allowed_target_types)
assert msg in str(e)
示例4: _update_train_test
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def _update_train_test(self):
"""
This function take care of the test_type parameter.
"""
if self.test_type == 'passive':
return True
if len(self._queries) > 0:
return True
else:
# active test split
all_indices = np.concatenate([self.train_indices, self.test_indices], axis=0)
all_y = np.concatenate([self._Y_train, self._Y_test], axis=0)
# select randomly
ss = ShuffleSplit(n_splits=1, test_size=self.test_size, train_size=None, random_state=90)
for train_indices, test_indices in ss.split(all_indices):
# test
self._Y_test = all_y[test_indices]
self.test_indices = all_indices[test_indices]
# train
self._Y_train = all_y[train_indices]
self.train_indices = all_indices[train_indices]
示例5: subset_indices
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def subset_indices(d_source, d_target, subsetsize, subsetseed):
if subsetsize > 0:
if subsetseed != 0:
subset_rng = np.random.RandomState(subsetseed)
else:
subset_rng = np.random
strat = StratifiedShuffleSplit(n_splits=1, test_size=subsetsize, random_state=subset_rng)
shuf = ShuffleSplit(n_splits=1, test_size=subsetsize, random_state=subset_rng)
_, source_indices = next(strat.split(d_source.y, d_source.y))
n_src = source_indices.shape[0]
if d_target.has_ground_truth:
_, target_indices = next(strat.split(d_target.y, d_target.y))
else:
_, target_indices = next(shuf.split(np.arange(len(d_target.images))))
n_tgt = target_indices.shape[0]
else:
source_indices = None
target_indices = None
n_src = len(d_source.images)
n_tgt = len(d_target.images)
return source_indices, target_indices, n_src, n_tgt
示例6: gen_samples
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def gen_samples(self, y, n_samples, problem_type):
if problem_type == 'classification':
splits = StratifiedShuffleSplit(
n_splits=n_samples,
test_size=self.cal_portion
)
split_ = splits.split(np.zeros((y.size, 1)), y)
else:
splits = ShuffleSplit(
n_splits=n_samples,
test_size=self.cal_portion
)
split_ = splits.split(np.zeros((y.size, 1)))
for train, cal in split_:
yield train, cal
# -----------------------------------------------------------------------------
# Conformal ensemble
# -----------------------------------------------------------------------------
示例7: get_single_train_test_split
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def get_single_train_test_split(self):
splits = dict()
cv_iter = ShuffleSplit(
n_splits=1, random_state=self.random_state, test_size=0.80
)
for n_obj, arr in self.X_dict.items():
if arr.shape[0] == 1:
splits[n_obj] = ([0], [0])
else:
splits[n_obj] = list(cv_iter.split(arr))[0]
self.X_train = dict()
self.Y_train = dict()
self.X_test = dict()
self.Y_test = dict()
for n_obj, itr in splits.items():
train_idx, test_idx = itr
self.X_train[n_obj] = np.copy(self.X_dict[n_obj][train_idx])
self.X_test[n_obj] = np.copy(self.X_dict[n_obj][test_idx])
self.Y_train[n_obj] = np.copy(self.Y_dict[n_obj][train_idx])
self.Y_test[n_obj] = np.copy(self.Y_dict[n_obj][test_idx])
self.X, self.Y = self.sub_sampling_from_dictionary()
self.__check_dataset_validity__()
self.X, self.X_test = standardize_features(self.X, self.X_test)
return self.X, self.Y, self.X_test, self.Y_test
示例8: TestPerformance
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def TestPerformance(self, df=None):
# If no dataframe is provided, use the currently learned one
if (df is None):
D = self.D
else:
D = self.S.transform(df.copy())
# Get features from the data frame
A = self._ExtractFeat(D)
# Get the target values and their corresponding column names
y, _ = self._ExtractTarg(D)
# Begin cross validation
ss = ShuffleSplit(n_splits=1)
for trn, tst in ss.split(A):
s1 = cross_val_score(self.R, A, y, cv=3, scoring=make_scorer(r2_score))
s2 = cross_val_score(self.R, A[tst], y[tst], cv=3, scoring=make_scorer(r2_score))
s3 = cross_val_score(self.R, A[trn], y[trn], cv=3, scoring=make_scorer(r2_score))
print('C-V:\t' + str(s1) + '\nTst:\t' + str(s2) + '\nTrn:\t' + str(s3))
示例9: estimate_predictive_performance
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def estimate_predictive_performance(x_y,
estimator=None,
n_splits=10,
random_state=1):
"""estimate_predictive_performance."""
x, y = x_y
cv = ShuffleSplit(n_splits=n_splits,
test_size=0.3,
random_state=random_state)
scoring = make_scorer(average_precision_score)
scores = cross_val_score(estimator, x, y, cv=cv, scoring=scoring)
return scores
示例10: cv_reg
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def cv_reg(x, test_size = 0.2, n_splits = 5, random_state=None): return ss(n_splits, test_size, random_state=random_state).split(x)
示例11: test_cross_val_score_client
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def test_cross_val_score_client(trend):
"Test the deprecated dask Client interface"
coords, data = trend[:2]
model = Trend(degree=1)
nsplits = 5
cross_validator = ShuffleSplit(n_splits=nsplits, random_state=0)
client = Client(processes=False)
futures = cross_val_score(model, coords, data, cv=cross_validator, client=client)
scores = [future.result() for future in futures]
client.close()
assert len(scores) == nsplits
npt.assert_allclose(scores, 1)
示例12: test_shuffle_split
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def test_shuffle_split():
ss1 = ShuffleSplit(test_size=0.2, random_state=0).split(X)
ss2 = ShuffleSplit(test_size=2, random_state=0).split(X)
ss3 = ShuffleSplit(test_size=np.int32(2), random_state=0).split(X)
ss4 = ShuffleSplit(test_size=int(2), random_state=0).split(X)
for t1, t2, t3, t4 in zip(ss1, ss2, ss3, ss4):
assert_array_equal(t1[0], t2[0])
assert_array_equal(t2[0], t3[0])
assert_array_equal(t3[0], t4[0])
assert_array_equal(t1[1], t2[1])
assert_array_equal(t2[1], t3[1])
assert_array_equal(t3[1], t4[1])
示例13: test_shufflesplit_errors
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def test_shufflesplit_errors(test_size, train_size):
with pytest.raises(ValueError):
next(ShuffleSplit(test_size=test_size, train_size=train_size).split(X))
示例14: test_fit_and_score_working
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def test_fit_and_score_working():
X, y = make_classification(n_samples=30, random_state=0)
clf = SVC(kernel="linear", random_state=0)
train, test = next(ShuffleSplit().split(X))
# Test return_parameters option
fit_and_score_args = [clf, X, y, dict(), train, test, 0]
fit_and_score_kwargs = {'parameters': {'max_iter': 100, 'tol': 0.1},
'fit_params': None,
'return_parameters': True}
result = _fit_and_score(*fit_and_score_args,
**fit_and_score_kwargs)
assert result[-1] == fit_and_score_kwargs['parameters']
示例15: test_fit_and_score_verbosity
# 需要导入模块: from sklearn import model_selection [as 别名]
# 或者: from sklearn.model_selection import ShuffleSplit [as 别名]
def test_fit_and_score_verbosity(capsys, return_train_score, scorer, expected):
X, y = make_classification(n_samples=30, random_state=0)
clf = SVC(kernel="linear", random_state=0)
train, test = next(ShuffleSplit().split(X))
# test print without train score
fit_and_score_args = [clf, X, y, scorer, train, test, 10, None, None]
fit_and_score_kwargs = {'return_train_score': return_train_score}
_fit_and_score(*fit_and_score_args, **fit_and_score_kwargs)
out, _ = capsys.readouterr()
assert out.split('\n')[1] == expected