vs2015 网站开发教程,陕西省住房和城乡建设厅网官网,网站推广协议,光谷网站开发使用numpy写一个线性回归算法#xff0c; 方程#xff08;模型#xff09;为 y a x b yaxb yaxb。要求自己设计训练部分并且收敛到满意效果。 以下是数据产生代码#xff1a;
import matplotlib.pyplot as plt
import numpy as npclass DataGenerator: 方程模型为 y a x b yaxb yaxb。要求自己设计训练部分并且收敛到满意效果。 以下是数据产生代码
import matplotlib.pyplot as plt
import numpy as npclass DataGenerator:线性回归数据产生器 方程y ax bdef __init__(self, a, b):self.a aself.b bdef __call__(self, data_len):xx np.random.uniform(-50, 50, data_len) # 生成 x 点集yy self.a * xx self.b # 生成 y 点集# 加随机误差noise np.random.normal(0, 20, data_len)yy noisereturn xx, yya, b 3.5, 7.1
xx, yy DataGenerator(a, b)(1000)# 可视化
fig plt.figure()
ax fig.add_subplot()
ax.scatter(xx, yy)
plt.show()
现在把方程变换成 y a x 2 b y ax^2b yax2b
class DataGeneratorSD:线性回归数据产生器 方程y a*x*x bdef __init__(self, a, b):self.a aself.b bdef __call__(self, data_len):xx np.random.uniform(-50, 50, data_len) # 生成 x 点集yy self.a * (xx**2) self.b # 生成 y 点集# 加随机误差noise np.random.normal(0, 20, data_len) * 50yy noisereturn xx, yy