• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python multiclass.type_of_target函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中sklearn.utils.multiclass.type_of_target函数的典型用法代码示例。如果您正苦于以下问题:Python type_of_target函数的具体用法?Python type_of_target怎么用?Python type_of_target使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了type_of_target函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_type_of_target

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)
开发者ID:hmshan,项目名称:scikit-learn,代码行数:25,代码来源:test_multiclass.py


示例2: fit

    def fit(self, X, y):
        """Find the classes statistics before to perform sampling.

        Parameters
        ----------
        X : ndarray, shape (n_samples, n_features)
            Matrix containing the data which have to be sampled.

        y : ndarray, shape (n_samples, )
            Corresponding label for each sample in X.

        Returns
        -------
        self : object,
            Return self.

        """

        super(BaseMulticlassSampler, self).fit(X, y)

        # Check that the target type is either binary or multiclass
        if not (type_of_target(y) == 'binary' or
                type_of_target(y) == 'multiclass'):
            warnings.simplefilter('always', UserWarning)
            warnings.warn('The target type should be binary or multiclass.')

        return self
开发者ID:kellyhennigan,项目名称:cueexp_scripts,代码行数:27,代码来源:base.py


示例3: test_type_of_target

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:
        assert_raises(ValueError, type_of_target, example)
开发者ID:Aharobot,项目名称:scikit-learn,代码行数:9,代码来源:test_multiclass.py


示例4: _check_targets_hmc

def _check_targets_hmc(y_true, y_pred):
    check_consistent_length(y_true, y_pred)
    y_type = set([type_of_target(y_true), type_of_target(y_pred)])
    if y_type == set(["binary", "multiclass"]):
        y_type = set(["multiclass"])
    if y_type != set(["multiclass"]):
        raise ValueError("{0} is not supported".format(y_type))
    y_true = column_or_1d(y_true)
    y_pred = column_or_1d(y_pred)
    return y_true, y_pred
开发者ID:davidwarshaw,项目名称:hmc,代码行数:10,代码来源:metrics.py


示例5: _check_clf_targets

def _check_clf_targets(y_true, y_pred):
    """Check that y_true and y_pred belong to the same classification task

    This converts multiclass or binary types to a common shape, and raises a
    ValueError for a mix of multilabel and multiclass targets, a mix of
    multilabel formats, for the presence of continuous-valued or multioutput
    targets, or for targets of different lengths.

    Column vectors are squeezed to 1d.

    Parameters
    ----------
    y_true : array-like,

    y_pred : array-like

    Returns
    -------
    type_true : one of {'multilabel-indicator', 'multilabel-sequences', \
    'multiclass', 'binary'}
    The type of the true target data, as output by
    ``utils.multiclass.type_of_target``

    y_true : array or indicator matrix or sequence of sequences

    y_pred : array or indicator matrix or sequence of sequences
    """

    y_true, y_pred = check_arrays(y_true, y_pred, allow_lists=True)
    type_true = type_of_target(y_true)
    type_pred = type_of_target(y_pred)

    y_type = set([type_true, type_pred])
    if y_type == set(["binary", "multiclass"]):
        y_type = set(["multiclass"])

    if len(y_type) > 1:
        raise ValueError("Can't handle mix of {0} and {1}" "".format(type_true, type_pred))

    # We can't have more than one value on y_type => The set is no more needed
    y_type = y_type.pop()

    # No metrics support "multiclass-multioutput" format
    if y_type not in ["binary", "multiclass", "multilabel-indicator", "multilabel-sequences"]:
        raise ValueError("{0} is not supported".format(y_type))

    if y_type in ["binary", "multiclass"]:
        y_true = column_or_1d(y_true)
        y_pred = column_or_1d(y_pred)

    return y_type, y_true, y_pred
开发者ID:DjalelBBZ,项目名称:SOS14_practical_session,代码行数:51,代码来源:SOS_tools.py


示例6: _posibility

 def _posibility(self, x, tag, event=1):
     """计算触发概率
     Parameters:
     ----------
         x (Sequence): - 离散特征序列
         tag (Sequence): - 用于训练的标签序列
         event (any): - True指代的触发事件
     Returns:
     ----------
         Dict[str,Tuple[rate_T, rate_F]]: - 训练好后的好坏触发概率
     """
     if type_of_target(tag) not in ['binary']:
         raise AttributeError("tag must be a binary array")
     #if type_of_target(x) in ['continuous']:
     #    raise AttributeError("input array must not continuous")
     tag = np.array(tag)
     x = np.array(x)
     event_total = (tag == event).sum()
     non_event_total = tag.shape[-1] - event_total
     x_labels = pd.unique(x[pd.notnull(x)])
     pos_dic = {}
     for x1 in x_labels:
         # 当 x1 是nan时,y1 也为空
         y1 = tag[np.where(x == x1)[0]]
         event_count = (y1 == event).sum()
         non_event_count = y1.shape[-1] - event_count
         rate_event = 1.0 * event_count / event_total
         rate_non_event = 1.0 * non_event_count / non_event_total
         pos_dic[x1] = (rate_event, rate_non_event)
     return pos_dic
开发者ID:gasongjian,项目名称:reportgen,代码行数:30,代码来源:preprocessing.py


示例7: check_target_type

def check_target_type(y, indicate_one_vs_all=False):
    """Check the target types to be conform to the current samplers.

    The current samplers should be compatible with ``'binary'``,
    ``'multilabel-indicator'`` and ``'multiclass'`` targets only.

    Parameters
    ----------
    y : ndarray,
        The array containing the target.

    indicate_one_vs_all : bool, optional
        Either to indicate if the targets are encoded in a one-vs-all fashion.

    Returns
    -------
    y : ndarray,
        The returned target.

    is_one_vs_all : bool, optional
        Indicate if the target was originally encoded in a one-vs-all fashion.
        Only returned if ``indicate_multilabel=True``.

    """
    type_y = type_of_target(y)
    if type_y == 'multilabel-indicator':
        if np.any(y.sum(axis=1) > 1):
            raise ValueError(
                "When 'y' corresponds to '{}', 'y' should encode the "
                "multiclass (a single 1 by row).".format(type_y))
        y = y.argmax(axis=1)

    return (y, type_y == 'multilabel-indicator') if indicate_one_vs_all else y
开发者ID:chkoar,项目名称:imbalanced-learn,代码行数:33,代码来源:_validation.py


示例8: fit

    def fit(self, X, y):
        """Find the classes statistics before to perform sampling.

        Parameters
        ----------
        X : ndarray, shape (n_samples, n_features)
            Matrix containing the data which have to be sampled.

        y : ndarray, shape (n_samples, )
            Corresponding label for each sample in X.

        Returns
        -------
        self : object,
            Return self.

        """

        super(BaseBinarySampler, self).fit(X, y)

        # Check that the target type is binary
        if not type_of_target(y) == 'binary':
            warnings.warn('The target type should be binary.')

        return self
开发者ID:dvro,项目名称:imbalanced-learn,代码行数:25,代码来源:base.py


示例9: test_type_of_target

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)
开发者ID:0664j35t3r,项目名称:scikit-learn,代码行数:16,代码来源:test_multiclass.py


示例10: _sampling_strategy_float

def _sampling_strategy_float(sampling_strategy, y, sampling_type):
    """Take a proportion of the majority (over-sampling) or minority
    (under-sampling) class in binary classification."""
    type_y = type_of_target(y)
    if type_y != 'binary':
        raise ValueError(
            '"sampling_strategy" can be a float only when the type '
            'of target is binary. For multi-class, use a dict.')
    target_stats = Counter(y)
    if sampling_type == 'over-sampling':
        n_sample_majority = max(target_stats.values())
        class_majority = max(target_stats, key=target_stats.get)
        sampling_strategy_ = {
            key: int(n_sample_majority * sampling_strategy - value)
            for (key, value) in target_stats.items() if key != class_majority
        }
    elif (sampling_type == 'under-sampling'):
        n_sample_minority = min(target_stats.values())
        class_minority = min(target_stats, key=target_stats.get)
        sampling_strategy_ = {
            key: int(n_sample_minority / sampling_strategy)
            for (key, value) in target_stats.items() if key != class_minority
        }
    else:
        raise ValueError("'clean-sampling' methods do let the user "
                         "specify the sampling ratio.")
    return sampling_strategy_
开发者ID:bodycat,项目名称:imbalanced-learn,代码行数:27,代码来源:_validation.py


示例11: cross_val_score_one_vs_all_per_class

    def cross_val_score_one_vs_all_per_class(estimator, X, y=None, *args, **kargs):
        y_type = type_of_target(y)
        positive_example_amount = y.sum(axis=0)
        error = ""
        if (positive_example_amount < kargs["cv"]).any():
            error = (
                str((positive_example_amount < kargs["cv"]).sum())
                + " : too little examples for "
                + str(np.where(positive_example_amount < kargs["cv"]))
                + str(positive_example_amount[np.where(positive_example_amount < kargs["cv"])])
            )
        if (positive_example_amount > y.shape[0] - kargs["cv"]).any():
            error += (
                str((positive_example_amount > y.shape[0] - kargs["cv"]).sum())
                + " : too many examples for "
                + str(np.where(positive_example_amount > y.shape[0] - kargs["cv"]))
                + str(positive_example_amount[np.where(positive_example_amount > y.shape[0] - kargs["cv"])])
            )
        #        if error:
        #            raise Exception(error)
        if y_type.startswith("multilabel") and isinstance(estimator, OneVsRestClassifier):
            res = []
            for yy in y.transpose():
                res.append(_cross_val_score(deepcopy(estimator.estimator), X, yy, *args, **kargs))
            import pdb

            pdb.set_trace()
        else:
            res = _cross_val_score(estimator, X, y, *args, **kargs)
        return np.array(list(res))
开发者ID:otadmor,项目名称:Open-Knesset,代码行数:30,代码来源:tags_autolearn_play.py


示例12: check_target_binary

 def check_target_binary(self, y):
     '''
     check if the target variable is binary, raise error if not.
     :param y:
     :return:
     '''
     y_type = type_of_target(y)
     if y_type not in ['binary']:
         raise ValueError('Label type must be binary')
开发者ID:iiicherry,项目名称:information_value,代码行数:9,代码来源:information_value.py


示例13: fit

    def fit(self, X, y):
        """Fit MLP Classifier according to X, y

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
        Training vectors, where n_samples is the number of samples
        and n_features is the number of features.

        y : array-like, shape = [n_samples] or [n_samples, n_classes]
        Target values. It determines the problem type.

        *binary*
        If y is a vector of integers with two unique values.

        *multiclass*
        If y is a vector of integers with three or more values
        or if y is a two-dimensional array of integers and there exists only
        one non-zero element per row.

        *multiclass-multioutput*
        If y is two-dimensional array of integers with two unique values
        and there exists more than one non-zero element per row.

        *continuous*
        If y is a vector of floats.

        *continuous-multioutput*
        If y is a two-dimensional array of floats.

        Returns
        -------
        self : object
        Returns self.
        """
        X, = check_arrays(X, sparse_format='dense')

        n_samples, self.input_size_ = X.shape

        y = np.atleast_1d(y)

        self.type_of_target_ = type_of_target(y)
        if self.verbose > 0:
            print("The inferred type of y is %s" % self.type_of_target_)
        if self.type_of_y != None:
            if self.type_of_y != self.type_of_target_:
                print("Passed type of y is %s, inferred type is %s"
                      % (self.type_of_y, self.type_of_target_))
                raise("derp")

        self.check_type_implemented()
        y = self._get_output(y)
        X, y = self._scale(X, y)
        self._inst_mlp()
        self._fit_mlp(X, y)
        if self.dropout and self.type_of_target_ in ['continuous', 'continuous-multioutput']:
            self._lineregress(X, y)
开发者ID:JakeMick,项目名称:graymatter,代码行数:57,代码来源:mlp.py


示例14: check_samplers_multiclass_ova

def check_samplers_multiclass_ova(name, Sampler):
    # Check that multiclass target lead to the same results than OVA encoding
    X, y = make_classification(n_samples=1000, n_classes=3, n_informative=4,
                               weights=[0.2, 0.3, 0.5], random_state=0)
    y_ova = label_binarize(y, np.unique(y))
    sampler = Sampler()
    # FIXME: in 0.6 set the random_state for all
    if name not in DONT_HAVE_RANDOM_STATE:
        set_random_state(sampler)
    X_res, y_res = sampler.fit_resample(X, y)
    X_res_ova, y_res_ova = sampler.fit_resample(X, y_ova)
    assert_allclose(X_res, X_res_ova)
    if issubclass(Sampler, BaseEnsembleSampler):
        for batch_y, batch_y_ova in zip(y_res, y_res_ova):
            assert type_of_target(batch_y_ova) == type_of_target(y_ova)
            assert_allclose(batch_y, batch_y_ova.argmax(axis=1))
    else:
        assert type_of_target(y_res_ova) == type_of_target(y_ova)
        assert_allclose(y_res, y_res_ova.argmax(axis=1))
开发者ID:chkoar,项目名称:imbalanced-learn,代码行数:19,代码来源:estimator_checks.py


示例15: _check_cv

def _check_cv(cv=3, y=None, classifier=False, **kwargs):
    """Input checker utility for building a cross-validator.

    Parameters
    ----------
    cv : int, cross-validation generator or an iterable, optional
        Determines the cross-validation splitting strategy.
        Possible inputs for cv are:
          - None, to use the default 3-fold cross-validation,
          - integer, to specify the number of folds.
          - An object to be used as a cross-validation generator.
          - An iterable yielding train/test splits.

        For integer/None inputs, if classifier is True and ``y`` is either
        binary or multiclass, :class:`StratifiedKFold` is used. In all other
        cases, :class:`KFold` is used.

        Refer :ref:`User Guide <cross_validation>` for the various
        cross-validation strategies that can be used here.

    y : array-like, optional
        The target variable for supervised learning problems.

    classifier : boolean, optional, default False
        Whether the task is a classification task, in which case
        stratified KFold will be used.

    kwargs : dict
        Other parameters for StratifiedShuffleSplit or ShuffleSplit.

    Returns
    -------
    checked_cv : a cross-validator instance.
        The return value is a cross-validator which generates the train/test
        splits via the ``split`` method.
    """
    if cv is None:
        cv = kwargs.pop('n_splits', 0) or 10

    if isinstance(cv, numbers.Integral):
        if (classifier and (y is not None) and
                (type_of_target(y) in ('binary', 'multiclass'))):
            return StratifiedShuffleSplit(cv, **kwargs)
        else:
            return ShuffleSplit(cv, **kwargs)

    if not hasattr(cv, 'split') or isinstance(cv, str):
        if not isinstance(cv, Iterable) or isinstance(cv, str):
            raise ValueError("Expected cv as an integer, cross-validation "
                             "object (from sklearn.model_selection) "
                             "or an iterable. Got %s." % cv)
        return _CVIterableWrapper(cv)

    return cv  # New style cv objects are passed without any modification
开发者ID:slipguru,项目名称:palladio,代码行数:54,代码来源:model_assessment.py


示例16: check_averaging

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")
开发者ID:r-mart,项目名称:scikit-learn,代码行数:11,代码来源:test_common.py


示例17: _validate_target

    def _validate_target(self, y):
        """
        Raises a value error if the target is not a classification target.
        """
        # Ignore None values
        if y is None:
            return

        y_type = type_of_target(y)
        if y_type not in ("binary", "multiclass"):
            raise YellowbrickValueError((
                "'{}' target type not supported, only binary and multiclass"
            ).format(y_type))
开发者ID:DistrictDataLabs,项目名称:yellowbrick,代码行数:13,代码来源:class_balance.py


示例18: woe

def woe(X,y,event=1):
    res_woe = [] 
    iv_dict = {}
    for feature in X.columns:
        x = X[feature].values
        # 判断x 是否为连续变量,如果是,就要进行离散化
        if type_of_target(x) == 'continuous':
            x = discrete(x)
        woe_dict,iv = woe_single_x(x, y, feature, event)
        iv_dict[feature] = iv
        res_woe.append(woe_dict)
     
    return iv_dict
开发者ID:gdzsgcj,项目名称:mygit,代码行数:13,代码来源:feature_engineeing.py


示例19: check_target_type

def check_target_type(y):
    """Check the target types to be conform to the current samplers.

    The current samplers should be compatible with ``'binary'`` and
    ``'multiclass'`` targets only.

    Parameters
    ----------
    y : ndarray,
        The array containing the target

    Returns
    -------
    y : ndarray,
        The returned target.

    """
    if type_of_target(y) not in TARGET_KIND:
        # FIXME: perfectly we should raise an error but the sklearn API does
        # not allow for it
        warnings.warn("'y' should be of types {} only. Got {} instead.".format(
            TARGET_KIND, type_of_target(y)))
    return y
开发者ID:glemaitre,项目名称:imbalanced-learn,代码行数:23,代码来源:validation.py


示例20: check_target_type

def check_target_type(y, indicate_one_vs_all=False):
    """Check the target types to be conform to the current samplers.

    The current samplers should be compatible with ``'binary'``,
    ``'multilabel-indicator'`` and ``'multiclass'`` targets only.

    Parameters
    ----------
    y : ndarray,
        The array containing the target.

    indicate_one_vs_all : bool, optional
        Either to indicate if the targets are encoded in a one-vs-all fashion.

    Returns
    -------
    y : ndarray,
        The returned target.

    is_one_vs_all : bool, optional
        Indicate if the target was originally encoded in a one-vs-all fashion.
        Only returned if ``indicate_multilabel=True``.

    """
    type_y = type_of_target(y)
    if type_y not in TARGET_KIND:
        # FIXME: perfectly we should raise an error but the sklearn API does
        # not allow for it
        warnings.warn("'y' should be of types {} only. Got {} instead.".format(
            TARGET_KIND, type_of_target(y)))

    if indicate_one_vs_all:
        return (y.argmax(axis=1) if type_y == 'multilabel-indicator' else y,
                type_y == 'multilabel-indicator')
    else:
        return y.argmax(axis=1) if type_y == 'multilabel-indicator' else y
开发者ID:bodycat,项目名称:imbalanced-learn,代码行数:36,代码来源:_validation.py



注:本文中的sklearn.utils.multiclass.type_of_target函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python multiclass.unique_labels函数代码示例发布时间:2022-05-27
下一篇:
Python linear_assignment_.linear_assignment函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap