线性回归问题 自写拟合函数
程序员文章站
2022-07-14 13:40:50
...
二次回归拟合
#生成数据点
import numpy as np
import matplotlib.pyplot as plt
xx = np.random.normal(0, 1, [1000])
dd = xx**2 + xx + np.random.normal(0, 0.3, [1000]) #产生样本
plt.scatter(xx, dd)
plt.show()
转换数据
xx = np.array(xx)
dd = np.array(dd)
xx = xx.reshape(-1,1)
dd = dd.reshape(-1,1)
一阶线性回归
from sklearn.linear_model import LinearRegression
LR1 = LinearRegression()
LR1.fit(xx,dd)
y_predict= LR1.predict(xx)
print(y_predict)
画图
ig_reslut = plt.figure(figsize = (8,5))
plt.scatter(xx,dd)
plt.plot(xx,y_predict,"r")
plt.show()
二次回归
from sklearn.preprocessing import PolynomialFeatures
quadratic_featurizer = PolynomialFeatures(degree=2)
X_train_quadratic = quadratic_featurizer.fit_transform(xx)
regressor_quadratic = LinearRegression()
regressor_quadratic.fit(X_train_quadratic, dd)
xx_l = np.linspace(-4, 4, 1000)#给出一些点,并画出线性回归的曲线
xx_quadratic = quadratic_featurizer.transform(xx_l.reshape(xx_l.shape[0], 1))
fig_reslut = plt.figure(figsize = (8,5))
plt.scatter(xx,dd)
plt.plot(xx_l,regressor_quadratic.predict(xx_quadratic),"r")#根据需求点预测结果点
plt.show()
自写拟合函数
import numpy as np
import matplotlib.pyplot as plt
xx = np.random.normal(0, 1, [1000])
dd = xx**2 + xx + np.random.normal(0, 0.3, [1000]) #产生样本作用,实际不知道
# x,y是数据,样本点
plt.scatter(xx, dd)
plt.show()
def model(x, a, b, c):
# 构建的模型
y = a * x ** 2 + b * x + c
return y
def gradL(x, d, a, b, c):
# dL/dy
y = model(x, a, b, c)
dldy = 2 * (y - d)
dlda = np.mean(dldy * x ** 2) # dl/dy * dy/da
dldb = np.mean(dldy * x)
dldc = np.mean(dldy * 1)
return dlda, dldb, dldc, np.mean((y-d)**2)
a, b, c = 0, 0, 0 # 初始值
eta = 0.1 # 学习率,步长
loss = []
batch_size = 100 #批学习
for step in range(100):
idx = np.random.randint(0, 1000, [batch_size])
inx = xx[idx]
ind = dd[idx]
ga, gb, gc, ls = gradL(inx, ind, a, b, c) # 计算梯度
loss.append(ls)
a = a - eta * ga
b = b - eta * gb
c = c - eta * gc
plt.plot(loss)
plt.show()
x_plot = np.linspace(-3, 3, 1000)
y_plot = model(x_plot, a, b, c)
plt.plot(x_plot, y_plot, lw=3, c="r")
plt.scatter(xx, dd)
plt.show()
1000)
y_plot = model(x_plot, a, b, c)
plt.plot(x_plot, y_plot, lw=3, c="r")
plt.scatter(xx, dd)
plt.show()
上一篇: zookeeper
下一篇: sklearn实战:房价预测(线性回归)