• 技术文章 >Python技术 >Python高级

    python如何实现均方误差和均方根误差?

    宋雪维宋雪维2021-02-21 16:22:57原创7279

    一、python实现均方误差

    均方误差是各数据偏离真实值的距离平方和的平均数,也即误差平方和的平均数。

    用法:一般被用在机器学习的预测值与真实值之间的距离。均方误差对应的是最小二乘法。

    # -*- coding: utf-8 -*-
    import math
    
    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

    二、python实现均方根误差

    均方根误差亦称标准误差,是均方误差的算术平方根。

    # -*- coding: utf-8 -*-
    import math
    
    def get_rmse(records_real, records_predict):
        """
        均方根误差
        """
        mse = get_mse(records_real, records_predict)
        if mse:
            return math.sqrt(mse)
        else:
            return None
    专题推荐:python基础
    品易云
    上一篇:python中卡方分布如何使用? 下一篇:python中不同的CSV功能和使用

    相关文章推荐

    • python如何对电脑进行清屏• python入门:方差和标准差的区别

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网