本文整理汇总了Python中sklearn.utils.multiclass.type_of_target方法的典型用法代码示例。如果您正苦于以下问题:Python multiclass.type_of_target方法的具体用法?Python multiclass.type_of_target怎么用?Python multiclass.type_of_target使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sklearn.utils.multiclass
的用法示例。
在下文中一共展示了multiclass.type_of_target方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _predict_and_score
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def _predict_and_score(self, X_test, y_test):
#XXX: Implement type_of_target(y)
if(self.predict_proba):
y_type = type_of_target(y_test)
if(y_type in ('binary')):
pred = self.model.predict_proba(X_test)[:,1]
else:
pred = self.model.predict_proba(X_test)
else:
pred = self.model.predict(X_test)
if(self.multiclass_average == 'binary'):
return self.metric(y_test, pred), pred
else:
return self.metric(y_test, pred, average=self.multiclass_average), pred
示例2: _dispatch_gbdt_class
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def _dispatch_gbdt_class(algorithm_type: str, type_of_target: str):
is_regression = type_of_target == 'continuous'
if algorithm_type == 'lgbm':
requires_lightgbm()
from lightgbm import LGBMClassifier, LGBMRegressor
return LGBMRegressor if is_regression else LGBMClassifier
elif algorithm_type == 'cat':
requires_catboost()
from catboost import CatBoostClassifier, CatBoostRegressor
return CatBoostRegressor if is_regression else CatBoostClassifier
else:
requires_xgboost()
assert algorithm_type == 'xgb'
from xgboost import XGBClassifier, XGBRegressor
return XGBRegressor if is_regression else XGBClassifier
示例3: _make_1st_stage_preds
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def _make_1st_stage_preds(X, y, X_test):
if type_of_target(y) == 'continuous':
models = [
SVR(),
Ridge(random_state=0),
RandomForestRegressor(n_estimators=30, random_state=0)
]
else:
models = [
SVC(random_state=0),
LogisticRegression(random_state=0),
RandomForestClassifier(n_estimators=30, random_state=0)
]
results = [cross_validate(m, X, y, X_test, cv=5) for m in models]
return [r.oof_prediction for r in results], [r.test_prediction for r in results]
示例4: test_type_of_target
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def test_type_of_target():
for group, group_examples in EXAMPLES.items():
for example in group_examples:
assert_equal(type_of_target(example), group,
msg=('type_of_target(%r) should be %r, got %r'
% (example, group, type_of_target(example))))
for example in NON_ARRAY_LIKE_EXAMPLES:
msg_regex = r'Expected array-like \(array or non-string sequence\).*'
assert_raises_regex(ValueError, msg_regex, type_of_target, example)
for example in MULTILABEL_SEQUENCES:
msg = ('You appear to be using a legacy multi-label data '
'representation. Sequence of sequences are no longer supported;'
' use a binary array or sparse matrix instead.')
assert_raises_regex(ValueError, msg, type_of_target, example)
try:
from pandas import SparseSeries
except ImportError:
raise SkipTest("Pandas not found")
y = SparseSeries([1, 0, 0, 1, 0])
msg = "y cannot be class 'SparseSeries'."
assert_raises_regex(ValueError, msg, type_of_target, y)
示例5: _is_multilabel
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def _is_multilabel(self, y):
"""
Return whether the given target array corresponds to a multilabel
problem.
"""
temp_y = y.copy()
temp_y[np.zeros_like(temp_y, dtype=bool) | (temp_y == -1)] = 1
target_type = type_of_target(temp_y)
if target_type in ['binary', 'multiclass']:
return False
elif target_type == 'multilabel-indicator':
return True
else:
# Raise an error, as in
# sklearn.utils.multiclass.check_classification_targets.
raise ValueError("Unknown label type: %s" % target_type)
示例6: fit
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def fit(self, y):
"""Fit label binarizer
Parameters
----------
y : array of shape [n_samples,] or [n_samples, n_classes]
Target values. The 2-d matrix should only contain 0 and 1,
represents multilabel classification.
Returns
-------
self : returns an instance of self.
"""
self.y_type_ = type_of_target(y)
if 'multioutput' in self.y_type_:
raise ValueError("Multioutput target data is not supported with "
"label binarization")
if _num_samples(y) == 0:
raise ValueError('y has 0 samples: %r' % y)
self.sparse_input_ = sp.issparse(y)
self.classes_ = unique_labels(y)
return self
示例7: _make_test_folds
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def _make_test_folds(self, X, y):
y = np.asarray(y, dtype=bool)
type_of_target_y = type_of_target(y)
if type_of_target_y != 'multilabel-indicator':
raise ValueError(
'Supported target type is: multilabel-indicator. Got {!r} instead.'.format(type_of_target_y))
num_samples = y.shape[0]
rng = check_random_state(self.random_state)
indices = np.arange(num_samples)
if self.shuffle:
rng.shuffle(indices)
y = y[indices]
r = np.asarray([1 / self.n_splits] * self.n_splits)
test_folds = IterativeStratification(labels=y, r=r, random_state=rng)
return test_folds[np.argsort(indices)]
示例8: validate_inputs
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def validate_inputs(self, X, y):
# Things we don't want to allow until we've tested them:
# - Sparse inputs
# - Multiclass outputs (e.g., more than 2 classes in `y`)
# - Non-finite inputs
# - Complex inputs
X, y = check_X_y(X, y, accept_sparse=False, allow_nd=False)
assert_all_finite(X, y)
if type_of_target(y) != 'binary':
raise ValueError("Non-binary targets not supported")
if np.any(np.iscomplex(X)) or np.any(np.iscomplex(y)):
raise ValueError("Complex data not supported")
if np.issubdtype(X.dtype, np.object_) or np.issubdtype(y.dtype, np.object_):
try:
X = X.astype(float)
y = y.astype(int)
except (TypeError, ValueError):
raise ValueError("argument must be a string.* number")
return (X, y)
示例9: check_cv
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def check_cv(cv=3, y=None, classifier=False):
"""Dask aware version of ``sklearn.model_selection.check_cv``
Same as the scikit-learn version, but works if ``y`` is a dask object.
"""
if cv is None:
cv = 3
# If ``cv`` is not an integer, the scikit-learn implementation doesn't
# touch the ``y`` object, so passing on a dask object is fine
if not is_dask_collection(y) or not isinstance(cv, numbers.Integral):
return model_selection.check_cv(cv, y, classifier=classifier)
if classifier:
# ``y`` is a dask object. We need to compute the target type
target_type = delayed(type_of_target, pure=True)(y).compute()
if target_type in ("binary", "multiclass"):
return StratifiedKFold(cv)
return KFold(cv)
示例10: _check_X_y
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def _check_X_y(self, X, y):
# helpful error message for sklearn < 1.17
is_2d = hasattr(y, 'shape') and len(y.shape) > 1 and y.shape[1] >= 2
if is_2d or type_of_target(y) != 'binary':
raise TypeError("Only binary targets supported. For training "
"multiclass or multilabel models, you may use the "
"OneVsRest or OneVsAll metaestimators in "
"scikit-learn.")
X, Y = check_X_y(X, y, dtype=np.double, accept_sparse='csc',
multi_output=False)
self.label_binarizer_ = LabelBinarizer(pos_label=1, neg_label=-1)
y = self.label_binarizer_.fit_transform(Y).ravel().astype(np.double)
return X, y
示例11: feature_discretion
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def feature_discretion(self, X):
'''
Discrete the continuous features of input data X, and keep other features unchanged.
:param X : numpy array
:return: the numpy array in which all continuous features are discreted
'''
temp = []
for i in range(0, X.shape[-1]):
x = X[:, i]
x_type = type_of_target(x)
if x_type == 'continuous':
x1 = self.discrete(x)
temp.append(x1)
else:
temp.append(x)
return np.array(temp).T
示例12: test_type_of_target
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def test_type_of_target():
for group, group_examples in iteritems(EXAMPLES):
for example in group_examples:
assert_equal(type_of_target(example), group,
msg=('type_of_target(%r) should be %r, got %r'
% (example, group, type_of_target(example))))
for example in NON_ARRAY_LIKE_EXAMPLES:
msg_regex = 'Expected array-like \(array or non-string sequence\).*'
assert_raises_regex(ValueError, msg_regex, type_of_target, example)
for example in MULTILABEL_SEQUENCES:
msg = ('You appear to be using a legacy multi-label data '
'representation. Sequence of sequences are no longer supported;'
' use a binary array or sparse matrix instead.')
assert_raises_regex(ValueError, msg, type_of_target, example)
try:
from pandas import SparseSeries
except ImportError:
raise SkipTest("Pandas not found")
y = SparseSeries([1, 0, 0, 1, 0])
msg = "y cannot be class 'SparseSeries'."
assert_raises_regex(ValueError, msg, type_of_target, y)
示例13: check_cv
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def check_cv(cv: Union[int, Iterable, BaseCrossValidator] = 5,
y: Optional[Union[pd.Series, np.ndarray]] = None,
stratified: bool = False,
random_state: int = 0):
if cv is None:
cv = 5
if isinstance(cv, numbers.Integral):
if stratified and (y is not None) and (type_of_target(y) in ('binary', 'multiclass')):
return StratifiedKFold(cv, shuffle=True, random_state=random_state)
else:
return KFold(cv, shuffle=True, random_state=random_state)
return model_selection.check_cv(cv, y, stratified)
示例14: check_averaging
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def check_averaging(name, y_true, y_true_binarize, y_pred, y_pred_binarize,
y_score):
is_multilabel = type_of_target(y_true).startswith("multilabel")
metric = ALL_METRICS[name]
if name in METRICS_WITH_AVERAGING:
_check_averaging(metric, y_true, y_pred, y_true_binarize,
y_pred_binarize, is_multilabel)
elif name in THRESHOLDED_METRICS_WITH_AVERAGING:
_check_averaging(metric, y_true, y_score, y_true_binarize,
y_score, is_multilabel)
else:
raise ValueError("Metric is not recorded as having an average option")
示例15: check_binarized_results
# 需要导入模块: from sklearn.utils import multiclass [as 别名]
# 或者: from sklearn.utils.multiclass import type_of_target [as 别名]
def check_binarized_results(y, classes, pos_label, neg_label, expected):
for sparse_output in [True, False]:
if ((pos_label == 0 or neg_label != 0) and sparse_output):
assert_raises(ValueError, label_binarize, y, classes,
neg_label=neg_label, pos_label=pos_label,
sparse_output=sparse_output)
continue
# check label_binarize
binarized = label_binarize(y, classes, neg_label=neg_label,
pos_label=pos_label,
sparse_output=sparse_output)
assert_array_equal(toarray(binarized), expected)
assert_equal(issparse(binarized), sparse_output)
# check inverse
y_type = type_of_target(y)
if y_type == "multiclass":
inversed = _inverse_binarize_multiclass(binarized, classes=classes)
else:
inversed = _inverse_binarize_thresholding(binarized,
output_type=y_type,
classes=classes,
threshold=((neg_label +
pos_label) /
2.))
assert_array_equal(toarray(inversed), toarray(y))
# Check label binarizer
lb = LabelBinarizer(neg_label=neg_label, pos_label=pos_label,
sparse_output=sparse_output)
binarized = lb.fit_transform(y)
assert_array_equal(toarray(binarized), expected)
assert_equal(issparse(binarized), sparse_output)
inverse_output = lb.inverse_transform(binarized)
assert_array_equal(toarray(inverse_output), toarray(y))
assert_equal(issparse(inverse_output), issparse(y))