python预测函数 计算均方误差代码

请使用Python语言自定义函数,满足下列要求。
已知数据存储在points数组中,数组的第一列表示x值,第二列表示y值,且给定预测值公式为“y_pre=w*x+b”,根据w、b参数计算损失误差(设置均方误差为损失误差)。

这就是简单的线性回归,可以参见 Python小白的数学建模课-23.数据拟合全集 https://blog.csdn.net/youcans/article/details/119332590

# mathmodel25_v1.py
# Demo25 of mathematical modeling algorithm
# Demo of curve fitting with Scipy
# Copyright 2021 YouCans, XUPT
# Crated:2021-08-03

# 1. 单变量线性拟合:最小二乘法 scipy.optimize.leastsq
import numpy as np
import matplotlib.pyplot as plt  # 导入 Matplotlib 工具包
from scipy.optimize import leastsq  # 导入 scipy 中的最小二乘法拟合工具
from scipy.stats import linregress  # 导入 scipy 中的线性回归工具

def fitfunc1(p, x):  # 定义拟合函数为直线
    p0, p1 = p  # 拟合函数的参数
    y = p0 + p1*x  # 拟合函数的表达式
    return y

def error1(p, x, y):  # 定义观测值与拟合函数值的误差函数
    err = fitfunc1(p,x) - y  # 误差
    return err

# 创建给定数据点集 (x,yObs)
p = [2.5, 1.5]  # y = p[0] + p[1] * x
x = np.array([0., 0.5, 1.5, 2.5, 4.5, 5.5, 7.5, 8.0, 8.5, 9.0, 10.0])
y = p[0] + p[1] * x  # 理论值 y
np.random.seed(1)
yObs = y + np.random.randn(x.shape[-1])  # 生成带有噪声的观测数据
# print(x.shape, y.shape, yObs.shape)

# 由给定数据点集 (x,y) 求拟合函数的参数 pFit
p0 = [1, 1]  # 设置拟合函数的参数初值
pFit, info = leastsq(error1, p0, args=(x,yObs))  # 最小二乘法求拟合参数
print("Data fitting with Scipy.optimize.leastsq")
print("y = p[0] + p[1] * x")
print("p[0] = {:.4f}\np[1] = {:.4f}".format(pFit[0], pFit[1]))

# 由拟合函数 fitfunc 计算拟合曲线在数据点的函数值
yFit = fitfunc1(pFit,x)

# 比较:线性回归,可以返回斜率,截距,r 值,p 值,标准误差
slope, intercept, r_value, p_value, std = linregress(x, yObs)
print("\nLinear regress with Scipy.stats.linregress")
print("y = p[0] + p[1] * x")
print("p[0] = {:.4f}".format(intercept))  # 输出截距 intercept
print("p[1] = {:.4f}".format(slope))  # 输出斜率 slope
print("r^2_value: {:.4f}".format(r_value**2))  # 输出 r^2 值
print("p_value: {:.4f}".format(p_value))  # 输出 p 值
print("std: {:.4f}".format(std))  # 输出标准差 std

# 绘图
fig, ax = plt.subplots(figsize=(8,6))
ax.text(8,3,"youcans-xupt",color='gainsboro')
ax.set_title("Data fitting with linear least squares")
plt.scatter(x, yObs, label="observed data")
plt.plot(x, y, 'r--', label="theoretical curve")
plt.plot(x, yFit, 'b-', label="fitting curve")
plt.legend(loc="best")
plt.show()


现在计算很简单了:
def calculateMSE(X,Y,m,b):
return sum([(y-m*x -b)**2 for x,y in zip(X,Y)])/len(X)

或者:
def mse_metric(actual, predicted):
sum_error = 0.0
# loop over all values
for i in range(len(actual)):
# the error is the sum of (actual - prediction)^2
prediction_error = actual[i] - predicted[i]
sum_error += (prediction_error ** 2)
# now normalize
mean_error = sum_error / float(len(actual))
return (mean_error)


# python 预测平方误差_python 计算平均平方误差(MSE)的实例
# 方法一
def calculateMSE(X, Y, m, b):
    in_bracket = []
    for i in range(len(X)):
        num = (Y[i] - m*X[i] - b)**2
        in_bracket.append(num)
    all_sum = sum(in_bracket)
    MSE = all_sum / len(X)
    return MSE

# 方法二
def calculateMSE(X, Y, m, b):
    return sum([(y-m*x - b)**2 for x, y in zip(X, Y)])/len(X)

print(calculateMSE(X, Y, m1, b1))



from sklearn.metrics import mean_squared_error # 均方误差
from sklearn.metrics import mean_absolute_error # 平方绝对误差
from sklearn.metrics import r2_score # R square
# 调整后的R square
def adj_r_squared(x_test,y_test,y_predict):
    SS_R = sum((y_test-y_predict)**2)
    SS_T = sum((y_test-np.mean(y_test))**2)
    r_squared = 1 - (float(SS_R))/SS_T
    adj_r_squared = 1 - (1-r_squared)*(len(y_test)-1)/(len(y_test)-x_test.shape[1]-1)
    return adj_r_squared
# 皮尔逊相关系数
from scipy.stats import pearsonr
#调用
mean_squared_error(y_test,y_predict)
mean_absolute_error(y_test,y_predict)
r2_score(y_test,y_predict)
adj_r_squared(x_test,y_test,y_predict)
pearsonr(y_test,y_predict)

这个直接用numpy库里面的矩阵函数即可实现

同意楼上,不过应该可以简化一点