本文整理汇总了Python中sklearn.cross_validation方法的典型用法代码示例。如果您正苦于以下问题:Python sklearn.cross_validation方法的具体用法?Python sklearn.cross_validation怎么用?Python sklearn.cross_validation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn
的用法示例。
在下文中一共展示了sklearn.cross_validation方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _cv_len
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import cross_validation [as 别名]
def _cv_len(cv, X, y):
"""This method computes the length of a cross validation
object, agnostic of whether sklearn-0.17 or sklearn-0.18
is being used.
Parameters
----------
cv : `sklearn.cross_validation._PartitionIterator` or `sklearn.model_selection.BaseCrossValidator`
The cv object from which to extract length. If using
sklearn-0.17, this can be computed by calling `len` on
``cv``, else it's computed with `cv.get_n_splits(X, y)`.
X : pd.DataFrame or np.ndarray, shape(n_samples, n_features)
The dataframe or np.ndarray being fit in the grid search.
y : np.ndarray, shape(n_samples,)
The target being fit in the grid search.
Returns
-------
int
"""
return len(cv) if not SK18 else cv.get_n_splits(X, y)
示例2: gen_crossval_idxs
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import cross_validation [as 别名]
def gen_crossval_idxs(problem, n_folds=2):
y = problem.ds.target
rng = 43432
if hasattr(problem.ds, 'nids'):
# Ensure that an individual does not appear in both the train
# and the test dataset
from ibeis_cnn.dataset import stratified_kfold_label_split
labels = problem.ds.nids
_iter = stratified_kfold_label_split(y, labels, n_folds=n_folds, rng=rng)
else:
xvalkw = dict(n_folds=n_folds, shuffle=True, random_state=rng)
import sklearn.cross_validation
skf = sklearn.cross_validation.StratifiedKFold(y, **xvalkw)
_iter = skf
#import sklearn.model_selection
#skf = sklearn.model_selection.StratifiedKFold(**xvalkw)
#_iter = skf.split(X=np.empty(len(y)), y=y)
msg = 'cross-val test on %s' % (problem.ds.name)
progiter = ut.ProgIter(_iter, length=n_folds, lbl=msg)
for train_idx, test_idx in progiter:
yield train_idx, test_idx
# @ut.reloadable_class
示例3: _set_cv
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import cross_validation [as 别名]
def _set_cv(cv, X, y, classifier):
"""This method returns either a `sklearn.cross_validation._PartitionIterator` or
`sklearn.model_selection.BaseCrossValidator` depending on whether sklearn-0.17
or sklearn-0.18 is being used.
Parameters
----------
cv : int, `_PartitionIterator` or `BaseCrossValidator`
The CV object or int to check. If an int, will be converted
into the appropriate class of crossvalidator.
X : pd.DataFrame or np.ndarray, shape(n_samples, n_features)
The dataframe or np.ndarray being fit in the grid search.
y : np.ndarray, shape(n_samples,)
The target being fit in the grid search.
classifier : bool
Whether the estimator being fit is a classifier
Returns
-------
`_PartitionIterator` or `BaseCrossValidator`
"""
return check_cv(cv, X, y, classifier) if not SK18 else check_cv(cv, y, classifier)
示例4: testdata_smk
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import cross_validation [as 别名]
def testdata_smk(*args, **kwargs):
"""
>>> from ibeis.algo.smk.smk_pipeline import * # NOQA
>>> kwargs = {}
"""
import ibeis
import sklearn
import sklearn.cross_validation
# import sklearn.model_selection
ibs, aid_list = ibeis.testdata_aids(defaultdb='PZ_MTEST')
nid_list = np.array(ibs.annots(aid_list).nids)
rng = ut.ensure_rng(0)
xvalkw = dict(n_folds=4, shuffle=False, random_state=rng)
skf = sklearn.cross_validation.StratifiedKFold(nid_list, **xvalkw)
train_idx, test_idx = six.next(iter(skf))
daids = ut.take(aid_list, train_idx)
qaids = ut.take(aid_list, test_idx)
config = {
'num_words': 1000,
}
config.update(**kwargs)
qreq_ = SMKRequest(ibs, qaids, daids, config)
smk = qreq_.smk
#qreq_ = ibs.new_query_request(qaids, daids, cfgdict={'pipeline_root': 'smk', 'proot': 'smk'})
#qreq_ = ibs.new_query_request(qaids, daids, cfgdict={})
return ibs, smk, qreq_
示例5: _set_cv
# 需要导入模块: import sklearn [as 别名]
# 或者: from sklearn import cross_validation [as 别名]
def _set_cv(cv, estimator=None, X=None, y=None):
"""Set the default CV depending on whether clf
is classifier/regressor."""
# Detect whether classification or regression
if estimator in ['classifier', 'regressor']:
est_is_classifier = estimator == 'classifier'
else:
est_is_classifier = is_classifier(estimator)
# Setup CV
if check_version('sklearn', '0.18'):
from sklearn import model_selection as models
from sklearn.model_selection import (check_cv,
StratifiedKFold, KFold)
if isinstance(cv, (int, np.int)):
XFold = StratifiedKFold if est_is_classifier else KFold
cv = XFold(n_splits=cv)
elif isinstance(cv, str):
if not hasattr(models, cv):
raise ValueError('Unknown cross-validation')
cv = getattr(models, cv)
cv = cv()
cv = check_cv(cv=cv, y=y, classifier=est_is_classifier)
else:
from sklearn import cross_validation as models
from sklearn.cross_validation import (check_cv,
StratifiedKFold, KFold)
if isinstance(cv, (int, np.int)):
if est_is_classifier:
cv = StratifiedKFold(y=y, n_folds=cv)
else:
cv = KFold(n=len(y), n_folds=cv)
elif isinstance(cv, str):
if not hasattr(models, cv):
raise ValueError('Unknown cross-validation')
cv = getattr(models, cv)
if cv.__name__ not in ['KFold', 'LeaveOneOut']:
raise NotImplementedError('CV cannot be defined with str'
' for sklearn < .017.')
cv = cv(len(y))
cv = check_cv(cv=cv, X=X, y=y, classifier=est_is_classifier)
# Extract train and test set to retrieve them at predict time
if hasattr(cv, 'split'):
cv_splits = [(train, test) for train, test in
cv.split(X=np.zeros_like(y), y=y)]
else:
# XXX support sklearn.cross_validation cv
cv_splits = [(train, test) for train, test in cv]
if not np.all([len(train) for train, _ in cv_splits]):
raise ValueError('Some folds do not have any train epochs.')
return cv, cv_splits