Python计算回归拟合各项指标

0、各项回归指标简介

  • Relative Root Mean Squared Error(RRMSE):The RRMSE normalizes the Root Mean Squared Error (RMSE) by the mean of observations. It goes from 0 to infinity. The lower the better the prediction performance.
  • The NRMSE(Normalized Root Mean Square Error) is calculated as the RMSE divided by the range of the observed values, expressed as a percentage. The range of the observed values is the difference between the maximum and minimum values of the observed data.
    Best possible score is 0.0, smaller value is better. Range = [0, +inf)
  • MAE (Mean absolute error) represents the difference between the original and predicted values extracted by averaged the absolute difference over the data set.
  • MSE (Mean Squared Error) represents the difference between the
    original and predicted values extracted by squared the average
    difference over the data set.
  • RMSE (Root Mean Squared Error) is the error rate by the square root
    of MSE.
  • R-squared (Coefficient of determination) represents the coefficient
    of how well the values fit compared to the original values. The value
    from 0 to 1 interpreted as percentages. The higher the value is, the
    better the model is.

1、Python计算回归拟合各项指标:包括RMSE # RRMSE # RSE # NSE # MAE # R # R2 # MAPE # ρ
在这里插入图片描述
图片来源:https://github.com/alifrmf/Evaluation-Metrics-for-Linear-Regression/blob/main/README.md

代码:

# RMSE
def rmse(y_true, y_pred):
    squared_diff = (y_true - y_pred) ** 2
    mean_squared_diff = np.mean(squared_diff)
    rmse_value = np.sqrt(mean_squared_diff)
    return rmse_value

RRMSE计算方式一:RMSE除以真实值的均值

# RRMSE(Relative Root Mean Squared Error )
def rrmse(y_true, y_pred):
    # Calculate the squared errors between the predicted and true values
    squared_errors = (y_true - y_pred) ** 2
    
    # Calculate the mean of the squared errors
    mean_squared_error = np.mean(squared_errors)
    
    # Take the square root of the mean squared error
    root_mean_squared_error = np.sqrt(mean_squared_error)
    
    # Calculate the relative error by dividing the root mean squared error by the mean of the true values
    relative_error = root_mean_squared_error / np.mean(y_true)
    
    # Return the RRMSE value
    return relative_error

RRMSE计算方式二:除以真实值最大值-真实值最小值

def rrmse(s, o):
    """
        Relative Root Mean Squared Error
        input:
            s: simulated
            o: observed
        output:
            relative root mean squared error
        """
    return 100*np.sqrt(np.mean((s-o)**2))/(o.max()-o.min())
# RSE
def root_squared_error(y_true, y_pred):
    """
    Calculate the Root Squared Error between two arrays (y_true and y_pred).
    
    Args:
        y_true (numpy.ndarray): Actual values.
        y_pred (numpy.ndarray): Predicted values.
        
    Returns:
        float: The Root Squared Error.
    """
    error = y_true - y_pred
    squared_error = np.square(error)
    mean_squared_error = np.mean(squared_error)
    root_squared_error = np.sqrt(mean_squared_error)
    
    return root_squared_error
# NSE
def nash_sutcliffe_efficiency(y_true, y_pred):
    """
    Calculate the Nash-Sutcliffe Efficiency (NSE) between two arrays (y_true and y_pred).
    
    Args:
        y_true (numpy.ndarray): Actual values.
        y_pred (numpy.ndarray): Predicted values.
        
    Returns:
        float: The Nash-Sutcliffe Efficiency.
    """
    numerator = np.sum(np.square(y_true - y_pred))
    denominator = np.sum(np.square(y_true - np.mean(y_true)))
    nse = 1 - (numerator / denominator)
    
    return nse
# MAE
def mean_absolute_error(y_true, y_pred):
    """
    Calculate the Mean Absolute Error (MAE) between two arrays (y_true and y_pred).
    
    Args:
        y_true (numpy.ndarray): Actual values.
        y_pred (numpy.ndarray): Predicted values.
        
    Returns:
        float: The Mean Absolute Error.
    """
    absolute_error = np.abs(y_true - y_pred)
    mae = np.mean(absolute_error)
    
    return mae
# R
def pearson_correlation_coefficient(y_true, y_pred):
    """
    Calculate the Pearson Correlation Coefficient (R) between two arrays (y_true and y_pred).
    
    Args:
        y_true (numpy.ndarray): Actual values.
        y_pred (numpy.ndarray): Predicted values.
        
    Returns:
        float: The Pearson Correlation Coefficient.
    """
    correlation_matrix = np.corrcoef(y_true, y_pred)
    r = correlation_matrix[0, 1]
    
    return r
# R2
def r_squared(y_true, y_pred):
    """
    Calculate the R squared value between two arrays (y_true and y_pred).
    
    Args:
        y_true (numpy.ndarray): Actual values.
        y_pred (numpy.ndarray): Predicted values.
        
    Returns:
        float: The R squared value.
    """
    correlation_matrix = np.corrcoef(y_true, y_pred)
    correlation_xy = correlation_matrix[0,1]
    r_squared = correlation_xy**2
    
    return r_squared

# ρ (RRMSE / (1 + R))
def relative_rmse(y_true, y_pred):
    rmse = np.sqrt(metrics.mean_squared_error(y_true, y_pred))
    return rmse / (np.max(y_true) - np.min(y_true))

def pearson_correlation_coefficient(y_true, y_pred):
    correlation_matrix = np.corrcoef(y_true, y_pred)
    r = correlation_matrix[0, 1]
    return r

代码来源:https://github.com/alifrmf/Evaluation-Metrics-for-Linear-Regression/blob/main/Regression%20Metrics%20for%20Machine%20Learning.py

2、Python计算bias、rbias、mae、rmse等指标
代码来源:https://github.com/dsi-llc/scripts/blob/d4445ef02a971754fdaef901250b42b8394539fa/EEstatslib.py#L80

import numpy as np

# ------------------------------------------------------------------------------
# statistic functions
# ------------------------------------------------------------------------------

def drop_nan(df):
    """
        this function reads in dataframe after using 
        dffromdatfile function in dataFrameFromdatfiles.py
        then returns a dataframe without nan 
        """
    df_dropped = df.dropna()
    return df_dropped

def data_paired(df):
    """
        this function return the number of data paired
        after dropping nan values
        """
    return df.shape[0]

def bias(s, o):
    """
        Bias
        input:
            s: simulated
            o: observed
        output:
            bias
        """
    return np.mean(s-o)

def rbias(s, o):
    """
        Relative Bias
        input:
            s: simulated
            o: observed
        output:
            relative bias
        """
    return 100*(np.sum(s-o))/np.sum(o)
            

def mae(s, o):
    """
        Mean(Average) Absolute Error
        input:
            s: simulated
            o: observed
        output:
            mean absolute error
        """
    return np.mean(np.abs(s-o))

def rmse(s, o):
    """
        Root Mean Squared Error
        input:
            s: simulated
            o: observed
        output:
            root mean squared error
        """
    return np.sqrt(np.mean((s-o)**2))

def rrmse(s, o):
    """
        Relative Root Mean Squared Error
        input:
            s: simulated
            o: observed
        output:
            relative root mean squared error
        """
    return 100*np.sqrt(np.mean((s-o)**2))/(o.max()-o.min())

def correlation(s, o):
    """
        Correlation Coefficient
        input:
            s: simulated
            o: observed
        output:
            correlation coefficient
        """
    return np.corrcoef(o, s)[0, 1]

def r_sqr(s, o):
    """
        R Squared (Square of Correlation Coefficient)
        input:
            s: simulated
            o: observed
        output:
            R Squared
        """    
    return correlation(s, o)**2

def nsi(s, o):
    """
        Nash-Sutcliffe Index of Efficiency
        input:
            s: simulated
            o: observed
        output:
            nash-sutcliffe index of efficiency
        """
    return 1-np.sum((s-o)**2)/np.sum((o-np.mean(o))**2)

def coe(s, o):
    """
        Coefficient of Efficiency
        input:
            s: simulated
            o: observed
        output:
            coefficient of efficiency
        """
    return 1 - np.sum(np.abs(s-o))/np.sum(np.abs(o-np.mean(o)))

def ioa(s, o):
    """
        Index of Agreement
        input:
            s: simulated
            o: observed
        output:
            index of agreement
        """
    return 1 - (np.sum((o-s)**2))/\
               (np.sum((np.abs(s-np.mean(o))+np.abs(o-np.mean(o)))**2))

def kge(s, o):
    """
        Kling-Gupta Efficiency
        input:
            s: simulated
            o: observed
        output:
            kgef: kling-gupta efficiency
            cc: correlation
            alpha: ratio of the standard deviation
            beta: ratio of the mean
        """
    cc = correlation(s, o)
    alpha = np.std(s)/np.std(o)
    beta = np.sum(s)/np.sum(o)
    kgef = 1 - np.sqrt((cc-1)**2 + (alpha-1)**2 + (beta-1)**2)
    return kgef, cc, alpha, beta

def stats_summary(df, sim_column_idx=0, obs_column_idx=1, decimals=3):
    """
        Statistics Summary, output all statistics number in dictionary
        input:
            df: dataframe from EE.dat file 
                (default just two columns, model and data)
            sim_column_idx: column index for simulated values (default 0)
            obs_column_idx: column index for observed values (default 1)
            decimals: round all statistics to the given number of decimals (default 3)
        output:
            statsummary: dictionary with all statistics number
        """
            
            
    df_drop = drop_nan(df)
    
    simulated = df_drop.iloc[:, sim_column_idx]
    observed = df_drop.iloc[:, obs_column_idx]
    statsummary = {'Data Paired': data_paired(df_drop),
                   'Bias': np.round(bias(simulated, observed), decimals),
                   'Percent Bias': np.round(rbias(simulated, observed), decimals),
                   'Mean Absolute Error': np.round(mae(simulated, observed), decimals),
                   'RMSE': np.round(rmse(simulated, observed), decimals),
                   'RRMSE': np.round(rrmse(simulated, observed), decimals),
                   'R': np.round(correlation(simulated, observed), decimals),
                   'R-Sqr': np.round(r_sqr(simulated, observed), decimals),
                   'Nash-Sutcliffe Efficiency': np.round(nsi(simulated, observed), decimals),
                   'Coefficient of Efficiency': np.round(coe(simulated, observed),decimals),
                   'Index of Agreement': np.round(ioa(simulated, observed), decimals),
                   'Kling-Gupta Efficiency': np.round(list(kge(simulated, observed))[0], decimals)}
    return statsummary

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/651040.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

电脑不能远程桌面连接不上,电脑无法建立远程桌面连接如何解决?

电脑无法建立远程桌面连接的问题,通常涉及到多个层面的因素,包括但不限于网络设置、系统配置、防火墙设置以及服务状态等。以下是一些专业性的解决方案,以帮助您解决这一问题。 首先,我们需要检查网络连接。远程桌面连接需要稳定的…

Day08:CSS 高级

目标:掌握定位的作用及特点;掌握 CSS 高级技巧 一、定位 作用:灵活的改变盒子在网页中的位置 实现: 1.定位模式:position 2.边偏移:设置盒子的位置 leftrighttopbottom 水平方向偏移:left、…

电子商务网站(网上商店PetShop)

PetShop是一个范例,微软用它来展示.Net企业系统开发的能力。PetShop随着版本的不断更新,至现在基于.Net2.0的PetShop 4.0为止,整个设计逐渐变得成熟而优雅,有很多可以借鉴之处。PetShop是一个小型的项目,系统架构与代码…

【软件设计师】——7.软件工程基础

目录 7.1 软件工程概述 7.2 需求分析 7.3 软件设计 7.4 软件开发方法及模型 7.4.1 软件开发方法 7.4.2 软件开发模型 7.5 软件测试 7.6 软件维护 7.7 软件质量保证 7.7.1 软件质量特性 7.7.2 程序质量评审 7.7.3 设计质量评审 7.8 软件过程改进 7.9 项目管理 7.1 …

【制作100个unity游戏之26】unity2d横版卷轴动作类游13(附带项目源码)

最终效果 系列导航 文章目录 最终效果系列导航前言存储点灯光后处理存储位置信息存储更多数据存储场景信息持久化存储数据引入Unity 的可序列化字典类调用 游戏结束源码完结 前言 欢迎来到【制作100个Unity游戏】系列!本系列将引导您一步步学习如何使用Unity开发各…

Appium系列(2)元素定位工具appium-inspector

背景 如实现移动端自动化,依赖任何工具时,都需要针对于页面中的元素进行识别,通过识别到指定的元素,对元素进行事件操作。 识别元素的工具为appium官网提供的appium-inspector。 appium-inspector下载地址 我这里是mac电脑需要下…

使用numpy手写一个神经网络

本文主要包含以下内容: 推导神经网络的误差反向传播过程使用numpy编写简单的神经网络,并使用iris数据集和california_housing数据集分别进行分类和回归任务,最终将训练过程可视化。 1. BP算法的推导过程 1.1 导入 前向传播和反向传播的总体…

编程-辅助工具-Git下载

文章目录 1、前言2、Git官网地址3、迅雷下载 1、前言 采用Git能下载github上的代码,其下载是采用官网下载的,但是下载速度比较慢,网上也推荐了镜像的方式,但是有些链接失效了,突然有一天想起用迅雷是不是合适&#xf…

在通过跨网文件交换时,如何保障科研结构核心研究数据?

当今科研领域,数据如同生命线,支撑着每一个突破性发现的诞生。随着国际合作的加深,跨网文件交换成了常态,但这也为科研机构的核心研究数据安全带来了一系列挑战。想象一下,那些精心搜集和分析的宝贵数据,在…

OpenBayes 教程上新 |全球首个开源的文生视频 DiT 模型!对标 Sora,保姆级 Latte 文生视频使用指南

小朋友不爱背诗怎么办?《千秋诗颂》试试看。 2 月 26 日,中国首部文生视频 AI 系列动画《千秋诗颂》于 CCTV-1 频道正式播出,这部动画由上海人工智能实验室和「央妈」(中央广播电视总台)强强联手,借助「央视…

Discuz!X3.4论坛网站公安备案号怎样放到网站底部?

Discuz!网站的工信部备案号都知道在后台——全局——站点信息——网站备案信息代码填写,那公安备案号要添加在哪里呢?并没有看到公安备案号填写栏,今天驰网飞飞和你分享 1)工信部备案号和公安备案号统一填写到网站备案…

安装appium自动化测试环境,我自己的版本信息

教程来自:Appium原理与安装 - 白月黑羽 我的软件的版本: 安装是选择为自己安装而不是选all user pip install appium-python-client命令在项目根目录下安装appium-python-client sdk的话最简单的安装方式就是去Android官网下一个android studio然后在…

强化学习_06_pytorch-PPO2实践(Humanoid-v4)

一、PPO优化 PPO的简介和实践可以看笔者之前的文章 强化学习_06_pytorch-PPO实践(Pendulum-v1) 针对之前的PPO做了主要以下优化: -笔者-PPO笔者-PPO2refdata collectone episodeseveral episode(one batch)activationReLUTanhadv-compute-compute adv as one seri…

鸿蒙ArkTS声明式开发:跨平台支持列表【按键事件】

按键事件 按键事件指组件与键盘、遥控器等按键设备交互时触发的事件,适用于所有可获焦组件,例如Button。对于Text,Image等默认不可获焦的组件,可以设置focusable属性为true后使用按键事件。 说明: 开发前请熟悉鸿蒙开…

【STM32踩坑】HAL固件库版本过高导致烧录后无法运行问题

问题引入 目前STM32CUBEMX已经更新到了6.11版本,对应的固件库也一直在更新; 以STM32F1库为例,目前最新的库对应版本为1.8.5 但是我们会发现,如果直接使用1.8.5版本的固件库生成HAL源码后,烧录是可以烧录,但…

【IC】良率模型-yield model

缺陷密度Default Density(D0),表示单位面积的缺陷数D。 单位面积有M个部件,一个部件的平均失效率为: 一个面积为A的系统(芯片)良率: Possion模型: 当M趋于无穷时,系统良率为possion模型。 Murphy模型:(D~对称三角分布) 大芯片或大系统possion模型预估良率过于…

Python自动化测试进阶:性能测试与持续集成实践

Python自动化测试进阶包括性能测试和持续集成实践两个关键方面。以下是对这两个领域的简要介绍,并附带一些示例代码。 性能测试 性能测试是评估软件在特定条件下的性能表现的过程。对于Python应用程序,可以使用一些工具来进行性能测试,例如ps…

linux centos tomcat启动内存泄漏

tomcat启动内存泄漏 经过与开发沟通,结果是开发写了死循环,导致内存泄漏,上一次是开发少打了一个jar包,让开发查代码就行,重新更新代码

C++学习/复习6---内存管理(数据的位置/大小)/new、delete/内存相关面试题(malloc与new/内存泄漏)

一、内存中区域 1.不同数据的存储位置 2.数据占用内存大小 二、动态内存函数 三、new与delete 1.操作内置类型 2.操作自定义类型 四、operator new与operator delete 1.底层源码(malloc/free) 2.内置/自定义与构造和析构 3.举例 五、定位new表达式 1.举…

TiDB学习3:TiKV

目录 1. TiKV架构和作用 2. RocksDB 2.1 写入 2.2 查询 2.3 Column Families列簇 3. 分布式事务 3.1 事务流程 3.2 分布式事务流程 3.3 MVCC 4. Raft与Multi Raft 4.1 Raft日志复制 4.2 Raft Leader选举 5. TiKV- 读写 5.1 数据的写入 5.2 数据的读取ReadIndex …