Python编程:方差、标准差、均方差、均方根值、均方误差、均方根误差

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: Python编程:方差、标准差、均方差、均方根值、均方误差、均方根误差

image.png

# -*- coding: utf-8 -*-
import math
def get_average(records):
    """
    平均值
    """
    return sum(records) / len(records)
def get_variance(records):
    """
    方差 反映一个数据集的离散程度
    """
    average = get_average(records)
    return sum([(x - average) ** 2 for x in records]) / len(records)
def get_standard_deviation(records):
    """
    标准差 == 均方差 反映一个数据集的离散程度
    """
    variance = get_variance(records)
    return math.sqrt(variance)
def get_rms(records):
    """
    均方根值 反映的是有效值而不是平均值
    """
    return math.sqrt(sum([x ** 2 for x in records]) / len(records))
def get_mse(records_real, records_predict):
    """
    均方误差 估计值与真值 偏差
    """
    if len(records_real) == len(records_predict):
        return sum([(x - y) ** 2 for x, y in zip(records_real, records_predict)]) / len(records_real)
    else:
        return None
def get_rmse(records_real, records_predict):
    """
    均方根误差:是均方误差的算术平方根
    """
    mse = get_mse(records_real, records_predict)
    if mse:
        return math.sqrt(mse)
    else:
        return None
def get_mae(records_real, records_predict):
    """
    平均绝对误差
    """
    if len(records_real) == len(records_predict):
        return sum([abs(x - y) for x, y in zip(records_real, records_predict)]) / len(records_real)
    else:
        return None
if __name__ == '__main__':
    records1 = [3, 4, 5]
    records2 = [2, 4, 6]
    # 平均值
    average1 = get_average(records1)  # 4.0
    average2 = get_average(records2)  # 4.0
    # 方差
    variance1 = get_variance(records1)  # 0.66
    variance2 = get_variance(records2)  # 2.66
    # 标准差
    std_deviation1 = get_standard_deviation(records1)  # 0.81
    std_deviation2 = get_standard_deviation(records2)  # 1.63
    # 均方根
    rms1 = get_rms(records1)  # 4.08
    rms2 = get_rms(records2)  # 4.32
    # 均方误差
    mse = get_mse(records1, records2)  # 0.66
    # 均方根误差
    rmse = get_rmse(records1, records2)  # 0.81
    # 平均绝对误差
    mae = get_mae(records1, records2)  # 0.66
相关实践学习
基于MSE实现微服务的全链路灰度
通过本场景的实验操作,您将了解并实现在线业务的微服务全链路灰度能力。
相关文章
|
7天前
|
存储 人工智能 数据处理
Python:编程的艺术与科学的完美交融
Python:编程的艺术与科学的完美交融
13 1
|
3天前
|
测试技术 调度 索引
python编程中常见的问题
【4月更文挑战第23天】
14 2
|
3天前
|
网络协议 算法 网络架构
Python网络编程之udp编程、黏包以及解决方案、tcpserver
Python网络编程之udp编程、黏包以及解决方案、tcpserver
|
4天前
|
机器学习/深度学习 数据挖掘 算法框架/工具
Python:编程的艺术与魅力
Python:编程的艺术与魅力
14 3
|
4天前
|
机器学习/深度学习 数据可视化 数据挖掘
实用技巧:提高 Python 编程效率的五个方法
本文介绍了五个提高 Python 编程效率的实用技巧,包括使用虚拟环境管理依赖、掌握列表推导式、使用生成器提升性能、利用装饰器简化代码结构以及使用 Jupyter Notebook 进行交互式开发。通过掌握这些技巧,可以让你的 Python 编程更加高效。
|
4天前
|
算法 Python
Python面向对象oop编程(二)
Python面向对象oop编程(二)
|
7天前
|
机器学习/深度学习 数据挖掘 API
pymc,一个灵活的的 Python 概率编程库!
pymc,一个灵活的的 Python 概率编程库!
15 1
|
7天前
|
人工智能 算法 调度
uvloop,一个强大的 Python 异步IO编程库!
uvloop,一个强大的 Python 异步IO编程库!
19 2
|
7天前
|
机器学习/深度学习 人工智能 数据可视化
Python:探索编程之美
Python:探索编程之美
9 0
|
8天前
|
机器学习/深度学习 人工智能 数据处理
Python编程的魅力与实践
Python编程的魅力与实践