• 利用sklearn计算决定系数R2


    决定系数R2

    sklearn.metrics中r2_score

    格式

    sklearn.metrics.r2_score(y_true, y_pred, sample_weight=None, multioutput=’uniform_average’)

    R^2 (coefficient of determination) regression score function.

    R2可以是负值(因为模型可以任意差)。如果一个常数模型总是预测y的期望值,而忽略输入特性,则r^2的分数将为0.0。

    Best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a R^2 score of 0.0.

    Parameters:
    y_true array-like of shape = (n_samples) or (n_samples, n_outputs)

    Ground truth (correct) target values.

    y_pred array-like of shape = (n_samples) or (n_samples, n_outputs)

    Estimated target values.

    sample_weight array-like of shape = (n_samples), optional

    Sample weights.

    multioutput string in [‘raw_values’, ‘uniform_average’, ‘variance_weighted’] or None or array-like of shape (n_outputs)

    Defines aggregating of multiple output scores. Array-like value defines weights used to average scores. Default is “uniform_average”.

    ‘raw_values’ :

    Returns a full set of scores in case of multioutput input.

    ‘uniform_average’ :

    Scores of all outputs are averaged with uniform weight.

    ‘variance_weighted’ :

    Scores of all outputs are averaged, weighted by the variances of each individual output.

    Returns:
    z float or ndarray of floats

    The R^2 score or ndarray of scores if ‘multioutput’ is ‘raw_values’.

    注意:

    This is not a symmetric function. (R2是非对称函数!注意输入顺序。)

    Unlike most other scores, R^2 score may be negative (it need not actually be the square of a quantity R).(R2可以是负值,它不需要是R的平方!)

     

    from sklearn.metrics import r2_score
     y_true = y_true = [3, -0.5, 2, 7]
     y_pred = [2.5, 0.0, 2, 8]
     r2_score(y_true, y_pred)
     # 结果:0.9486081370449679
     r2_score(y_true, y_pred, multioutput= 'uniform_average')
     # 结果:0.9486081370449679
     y_true = [[0.5, 1], [-1, 1], [7, -6]]
     y_pred = [[0, 2], [-1, 2], [8, -5]]
     r2_score(y_true, y_pred, multioutput='variance_weighted')
     # 结果:0.9382566585956417
     y_true = [1, 2, 3]
     y_pred = [1, 2, 3]
     r2_score(y_true, y_pred)
     # 结果: 1.0
     y_true = [1, 2, 3]
     y_pred = [2, 2, 2]
     r2_score(y_true, y_pred)
     # 结果:0.0
      y_true = [1, 2, 3] # bar{y} = (1+2+3)/ 3 = 2
      y_pred = [3, 2, 1] # y - hat{y}(即y_true - y_pred) = [-2, 0, 2]
      r2_score(y_true, y_pred)
      # 结果:-3.0
      y_true = [[0.5, 1], [-1, 1], [7, -6]]
      y_pred = [[0, 2], [-1, 2], [8, -5]]
      r2_score(y_true, y_pred, multioutput='raw_values')
      # 结果:array([0.96543779, 0.90816327])

     

  • 相关阅读:
    UOJ#310. 【UNR #2】黎明前的巧克力(FWT)
    cf24D. Broken robot(高斯消元)
    loj#2483. 「CEOI2017」Building Bridges(dp cdq 凸包)
    给博客园加一个会动的小人-spig.js
    loj#6033. 「雅礼集训 2017 Day2」棋盘游戏(二分图博弈)
    loj#6032. 「雅礼集训 2017 Day2」水箱(并查集 贪心 扫描线)
    洛谷P4103 [HEOI2014]大工程(虚树 树形dp)
    Oracle DB SQL 性能分析器
    ORA-000845 与 /dev/shm(tempfs)
    ID3DXMesh接口 创建自己的立方体网格
  • 原文地址:https://www.cnblogs.com/jiangkejie/p/10677858.html
Copyright © 2020-2023  润新知