

def r2_score(y_true, y_pred, sample_weight=None, multioutput=None):
    "R^2 (coefficient of determination) regression score function.\n\n    Best possible score is 1.0 and it can be negative (because the\n    model can be arbitrarily worse). A constant model that always\n    predicts the expected value of y, disregarding the input features,\n    would get a R^2 score of 0.0.\n\n    Read more in the :ref:`User Guide <r2_score>`.\n\n    Parameters\n    ----------\n    y_true : array-like of shape = (n_samples) or (n_samples, n_outputs)\n        Ground truth (correct) target values.\n\n    y_pred : array-like of shape = (n_samples) or (n_samples, n_outputs)\n        Estimated target values.\n\n    sample_weight : array-like of shape = (n_samples), optional\n        Sample weights.\n\n    multioutput : string in ['raw_values', 'uniform_average', 'variance_weighted'] or None or array-like of shape (n_outputs)\n\n        Defines aggregating of multiple output scores.\n        Array-like value defines weights used to average scores.\n        Default value corresponds to 'variance_weighted', this behaviour is\n        deprecated since version 0.17 and will be changed to 'uniform_average'\n        starting from 0.19.\n\n        'raw_values' :\n            Returns a full set of scores in case of multioutput input.\n\n        'uniform_average' :\n            Scores of all outputs are averaged with uniform weight.\n\n        'variance_weighted' :\n            Scores of all outputs are averaged, weighted by the variances\n            of each individual output.\n\n    Returns\n    -------\n    z : float or ndarray of floats\n        The R^2 score or ndarray of scores if 'multioutput' is\n        'raw_values'.\n\n    Notes\n    -----\n    This is not a symmetric function.\n\n    Unlike most other scores, R^2 score may be negative (it need not actually\n    be the square of a quantity R).\n\n    References\n    ----------\n    .. [1] `Wikipedia entry on the Coefficient of determination\n            <https://en.wikipedia.org/wiki/Coefficient_of_determination>`_\n\n    Examples\n    --------\n    >>> from sklearn.metrics import r2_score\n    >>> y_true = [3, -0.5, 2, 7]\n    >>> y_pred = [2.5, 0.0, 2, 8]\n    >>> r2_score(y_true, y_pred)  # doctest: +ELLIPSIS\n    0.948...\n    >>> y_true = [[0.5, 1], [-1, 1], [7, -6]]\n    >>> y_pred = [[0, 2], [-1, 2], [8, -5]]\n    >>> r2_score(y_true, y_pred, multioutput='variance_weighted')  # doctest: +ELLIPSIS\n    0.938...\n\n    "
    (y_type, y_true, y_pred, multioutput) = _check_reg_targets(y_true, y_pred, multioutput)
    if (sample_weight is not None):
        sample_weight = column_or_1d(sample_weight)
        weight = sample_weight[:, np.newaxis]
    else:
        weight = 1.0
    numerator = (weight * ((y_true - y_pred) ** 2)).sum(axis=0, dtype=np.float64)
    denominator = (weight * ((y_true - np.average(y_true, axis=0, weights=sample_weight)) ** 2)).sum(axis=0, dtype=np.float64)
    nonzero_denominator = (denominator != 0)
    nonzero_numerator = (numerator != 0)
    valid_score = (nonzero_denominator & nonzero_numerator)
    output_scores = np.ones([y_true.shape[1]])
    output_scores[valid_score] = (1 - (numerator[valid_score] / denominator[valid_score]))
    output_scores[(nonzero_numerator & (~ nonzero_denominator))] = 0.0
    if ((multioutput is None) and (y_true.shape[1] != 1)):
        warnings.warn("Default 'multioutput' behavior now corresponds to 'variance_weighted' value which is deprecated since 0.17, it will be changed to 'uniform_average' starting from 0.19.", DeprecationWarning)
        multioutput = 'variance_weighted'
    if isinstance(multioutput, string_types):
        if (multioutput == 'raw_values'):
            return output_scores
        elif (multioutput == 'uniform_average'):
            avg_weights = None
        elif (multioutput == 'variance_weighted'):
            avg_weights = denominator
            if (not np.any(nonzero_denominator)):
                if (not np.any(nonzero_numerator)):
                    return 1.0
                else:
                    return 0.0
    else:
        avg_weights = multioutput
    return np.average(output_scores, weights=avg_weights)

dataset/ETHPy150Open scikit-learn/scikit-learn/sklearn/metrics/regression.py r2_score/original