(*, alpha=0.1, squared=True)
return decorate
Balance any scoring function using the index balanced accuracy.

    This factory function wraps scoring function to express it as the
    index balanced accuracy (IBA). You need to use this function to
    decorate any scoring function.

    Only metrics requiring ``y_pred`` can be corrected with the index
    balanced accuracy. ``y_score`` cannot be used since the dominance
    cannot be computed.

    Read more in the :ref:`User Guide <imbalanced_metrics>`.

    Parameters
    ----------
    alpha : float, default=0.1
        Weighting factor.

    squared : bool, default=True
        If ``squared`` is True, then the metric computed will be squared
        before to be weighted.

    Returns
    -------
    iba_scoring_func : callable,
        Returns the scoring metric decorated which will automatically compute
        the index balanced accuracy.

    Notes
    -----
    See :ref:`sphx_glr_auto_examples_evaluation_plot_metrics.py`.

    References
    ----------
    .. [1] García, Vicente, Javier Salvador Sánchez, and Ramón Alberto
       Mollineda. "On the effectiveness of preprocessing methods when dealing
       with different levels of class imbalance." Knowledge-Based Systems 25.1
       (2012): 13-21.

    Examples
    --------
    >>> from imblearn.metrics import geometric_mean_score as gmean
    >>> from imblearn.metrics import make_index_balanced_accuracy as iba
    >>> gmean = iba(alpha=0.1, squared=True)(gmean)
    >>> y_true = [1, 0, 0, 1, 0, 1]
    >>> y_pred = [0, 0, 1, 1, 0, 1]
    >>> print(gmean(y_true, y_pred, average=None))
    [0.44...  0.44...]
Balance any scoring function using the index balanced accuracy.
Balance any scoring function using the index balanced accuracy .
def make_index_balanced_accuracy(*, alpha=0.1, squared=True):
    """Balance any scoring function using the index balanced accuracy.

    This factory function wraps scoring function to express it as the
    index balanced accuracy (IBA). You need to use this function to
    decorate any scoring function.

    Only metrics requiring ``y_pred`` can be corrected with the index
    balanced accuracy. ``y_score`` cannot be used since the dominance
    cannot be computed.

    Read more in the :ref:`User Guide <imbalanced_metrics>`.

    Parameters
    ----------
    alpha : float, default=0.1
        Weighting factor.

    squared : bool, default=True
        If ``squared`` is True, then the metric computed will be squared
        before to be weighted.

    Returns
    -------
    iba_scoring_func : callable,
        Returns the scoring metric decorated which will automatically compute
        the index balanced accuracy.

    Notes
    -----
    See :ref:`sphx_glr_auto_examples_evaluation_plot_metrics.py`.

    References
    ----------
    .. [1] García, Vicente, Javier Salvador Sánchez, and Ramón Alberto
       Mollineda. "On the effectiveness of preprocessing methods when dealing
       with different levels of class imbalance." Knowledge-Based Systems 25.1
       (2012): 13-21.

    Examples
    --------
    >>> from imblearn.metrics import geometric_mean_score as gmean
    >>> from imblearn.metrics import make_index_balanced_accuracy as iba
    >>> gmean = iba(alpha=0.1, squared=True)(gmean)
    >>> y_true = [1, 0, 0, 1, 0, 1]
    >>> y_pred = [0, 0, 1, 1, 0, 1]
    >>> print(gmean(y_true, y_pred, average=None))
    [0.44...  0.44...]
    """

    def decorate(scoring_func):
        @functools.wraps(scoring_func)
        def compute_score(*args, **kwargs):
            signature_scoring_func = signature(scoring_func)
            params_scoring_func = set(signature_scoring_func.parameters.keys())

            # check that the scoring function does not need a score
            # and only a prediction
            prohibitied_y_pred = set(["y_score", "y_prob", "y2"])
            if prohibitied_y_pred.intersection(params_scoring_func):
                raise AttributeError(
                    f"The function {scoring_func.__name__} has an unsupported"
                    f" attribute. Metric with`y_pred` are the"
                    f" only supported metrics is the only"
                    f" supported."
                )

            args_scoring_func = signature_scoring_func.bind(*args, **kwargs)
            args_scoring_func.apply_defaults()
            _score = scoring_func(*args_scoring_func.args, **args_scoring_func.kwargs)
            if squared:
                _score = np.power(_score, 2)

            signature_sens_spec = signature(sensitivity_specificity_support)
            params_sens_spec = set(signature_sens_spec.parameters.keys())
            common_params = params_sens_spec.intersection(
                set(args_scoring_func.arguments.keys())
            )

            args_sens_spec = {k: args_scoring_func.arguments[k] for k in common_params}

            if scoring_func.__name__ == "geometric_mean_score":
                if "average" in args_sens_spec:
                    if args_sens_spec["average"] == "multiclass":
                        args_sens_spec["average"] = "macro"
            elif (
                scoring_func.__name__ == "accuracy_score"
                or scoring_func.__name__ == "jaccard_score"
            ):
                # We do not support multilabel so the only average supported
                # is binary
                args_sens_spec["average"] = "binary"

            sensitivity, specificity, _ = sensitivity_specificity_support(
                **args_sens_spec
            )

            dominance = sensitivity - specificity
            return (1.0 + alpha * dominance) * _score

        return compute_score

    return decorate
def make_index_balanced_accuracy ( * , alpha = 0.1 , squared = True ) : def decorate ( scoring_func ) : @ functools . wraps ( scoring_func ) def compute_score ( * args , * * kwargs ) : signature_scoring_func = signature ( scoring_func ) params_scoring_func = set ( signature_scoring_func . parameters . keys ( ) ) # check that the scoring function does not need a score # and only a prediction prohibitied_y_pred = set ( [ "y_score" , "y_prob" , "y2" ] ) if prohibitied_y_pred . intersection ( params_scoring_func ) : raise AttributeError ( f"The function {scoring_func.__name__} has an unsupported" f" attribute. Metric with`y_pred` are the" f" only supported metrics is the only" f" supported." ) args_scoring_func = signature_scoring_func . bind ( * args , * * kwargs ) args_scoring_func . apply_defaults ( ) _score = scoring_func ( * args_scoring_func . args , * * args_scoring_func . kwargs ) if squared : _score = np . power ( _score , 2 ) signature_sens_spec = signature ( sensitivity_specificity_support ) params_sens_spec = set ( signature_sens_spec . parameters . keys ( ) ) common_params = params_sens_spec . intersection ( set ( args_scoring_func . arguments . keys ( ) ) ) args_sens_spec = { k : args_scoring_func . arguments [ k ] for k in common_params } if scoring_func . __name__ == "geometric_mean_score" : if "average" in args_sens_spec : if args_sens_spec [ "average" ] == "multiclass" : args_sens_spec [ "average" ] = "macro" elif ( scoring_func . __name__ == "accuracy_score" or scoring_func . __name__ == "jaccard_score" ) : # We do not support multilabel so the only average supported # is binary args_sens_spec [ "average" ] = "binary" sensitivity , specificity , _ = sensitivity_specificity_support ( * * args_sens_spec ) dominance = sensitivity - specificity return ( 1.0 + alpha * dominance ) * _score return compute_score return decorate