

def explained_variance_score(y_true, y_pred, sample_weight=None, multioutput='uniform_average'):
    "Explained variance regression score function\n\n    Best possible score is 1.0, lower values are worse.\n\n    Read more in the :ref:`User Guide <explained_variance_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 array-like of shape (n_outputs)\n        Defines aggregating of multiple output scores.\n        Array-like value defines weights used to average scores.\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    score : float or ndarray of floats\n        The explained variance or ndarray if 'multioutput' is 'raw_values'.\n\n    Notes\n    -----\n    This is not a symmetric function.\n\n    Examples\n    --------\n    >>> from sklearn.metrics import explained_variance_score\n    >>> y_true = [3, -0.5, 2, 7]\n    >>> y_pred = [2.5, 0.0, 2, 8]\n    >>> explained_variance_score(y_true, y_pred)  # doctest: +ELLIPSIS\n    0.957...\n    >>> y_true = [[0.5, 1], [-1, 1], [7, -6]]\n    >>> y_pred = [[0, 2], [-1, 2], [8, -5]]\n    >>> explained_variance_score(y_true, y_pred, multioutput='uniform_average')\n    ... # doctest: +ELLIPSIS\n    0.983...\n\n    "
    (y_type, y_true, y_pred, multioutput) = _check_reg_targets(y_true, y_pred, multioutput)
    y_diff_avg = np.average((y_true - y_pred), weights=sample_weight, axis=0)
    numerator = np.average((((y_true - y_pred) - y_diff_avg) ** 2), weights=sample_weight, axis=0)
    y_true_avg = np.average(y_true, weights=sample_weight, axis=0)
    denominator = np.average(((y_true - y_true_avg) ** 2), weights=sample_weight, axis=0)
    nonzero_numerator = (numerator != 0)
    nonzero_denominator = (denominator != 0)
    valid_score = (nonzero_numerator & nonzero_denominator)
    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 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
    else:
        avg_weights = multioutput
    return np.average(output_scores, weights=avg_weights)

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