You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromfunctoolsimportwrapsimportwarningsimportnumbersimportnumpyasnpimportscipy.sparseasspfrominspectimportsignature, isclass, Parameterfromsklearn.utils.validationimport_num_samples, column_or_1d, _deprecate_positional_argsfromsklearn.utilsimportcheck_random_state, _approximate_mode, indexable, _safe_indexingfromsklearn.model_selectionimportBaseCrossValidatorfromabcimportABCMeta, abstractmethodclass_BaseKFold(BaseCrossValidator, metaclass=ABCMeta):
"""Base class for KFold, GroupKFold, and StratifiedKFold"""@abstractmethod@_deprecate_positional_argsdef__init__(self, n_splits, *, shuffle, random_state):
ifnotisinstance(n_splits, numbers.Integral):
raiseValueError('The number of folds must be of Integral type. ''%s of type %s was passed.'% (n_splits, type(n_splits)))
n_splits=int(n_splits)
ifn_splits<=1:
raiseValueError(
"k-fold cross-validation requires at least one"" train/test split by setting n_splits=2 or more,"" got n_splits={0}.".format(n_splits))
ifnotisinstance(shuffle, bool):
raiseTypeError("shuffle must be True or False;"" got {0}".format(shuffle))
ifnotshuffleandrandom_stateisnotNone: # None is the defaultraiseValueError(
'Setting a random_state has no effect since shuffle is ''False. You should leave ''random_state to its default (None), or set shuffle=True.',
)
self.n_splits=n_splitsself.shuffle=shuffleself.random_state=random_statedefsplit(self, X, y=None, groups=None):
"""Generate indices to split data into training and test set. Parameters ---------- X : array-like of shape (n_samples, n_features) Training data, where n_samples is the number of samples and n_features is the number of features. y : array-like of shape (n_samples,), default=None The target variable for supervised learning problems. groups : array-like of shape (n_samples,), default=None Group labels for the samples used while splitting the dataset into train/test set. Yields ------ train : ndarray The training set indices for that split. test : ndarray The testing set indices for that split. """X, y, groups=indexable(X, y, groups)
n_samples=_num_samples(X)
ifself.n_splits>n_samples:
raiseValueError(
("Cannot have number of splits n_splits={0} greater"" than the number of samples: n_samples={1}.")
.format(self.n_splits, n_samples))
fortrain, testinsuper().split(X, y, groups):
yieldtrain, testdefget_n_splits(self, X=None, y=None, groups=None):
"""Returns the number of splitting iterations in the cross-validator Parameters ---------- X : object Always ignored, exists for compatibility. y : object Always ignored, exists for compatibility. groups : object Always ignored, exists for compatibility. Returns ------- n_splits : int Returns the number of splitting iterations in the cross-validator. """returnself.n_splits# !! Works only when y is given !!classLastSolveKFold(_BaseKFold):
@_deprecate_positional_argsdef__init__(self, last_ids, n_splits=10, shuffle=False, random_state=None):
super().__init__(n_splits=n_splits, shuffle=shuffle, random_state=random_state)
self.last_ids=last_idsdef_iter_test_indices(self, X, y, groups=None):
n_samples=_num_samples(X)
indices=np.arange(n_samples)
ifself.shuffle:
check_random_state(self.random_state).shuffle(indices)
n_splits=self.n_splitsfold_sizes=np.full(n_splits, n_samples//n_splits, dtype=int)
fold_sizes[:n_samples%n_splits] +=1current=0forfold_sizeinfold_sizes:
start, stop=current, current+fold_sizeyieldindices[start:stop]
current=stopdefsplit(self, X, y, groups=None):
X, y, groups=indexable(X, y, groups)
indices=np.arange(_num_samples(X))
fortest_idinself._iter_test_masks(X, y, groups):
train_index=indices[np.logical_not(test_id)]
test_index=indices[test_id]
test_index=np.intersect1d(test_index, self.last_ids)
yieldtrain_index, test_index
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
@MoonJaeHoon 부탁드립니당...
Beta Was this translation helpful? Give feedback.
All reactions