加载中...
拟合
发表于:2021-11-08 | 分类: 机器学习课程(魏)
1
2
3
4
5
import numpy as np
import matplotlib.pyplot as plt
import numpy.linalg as lg
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures as pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
x=np.linspace(-20,20,50)
y=2*x+0.2*x**2+1+np.random.uniform(-5,5,x.shape)
plt.figure
plt.plot(x,y,'k*')

A=np.c_[x**2,x,np.ones(x.shape)]
B=y.reshape(y.shape[0],1)

eta=0.00001
n=10000
m=x.shape[0]
arf=np.random.randn(3,1)*2
for i in range(n):
gradients=2/m*A.T.dot(A.dot(arf)-B)
arf=arf-eta*gradients

w=lg.inv(A.T.dot(A)).dot(A.T).dot(y)

plt.plot(x,x**2*arf[0]+x*arf[1]+arf[2])
plt.show()


png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
t=np.arange(1,17,1)
y=np.array([4,6.4,8,8.8,9.22,9.5,9.7,9.86,10,10.20,10.32,10.42,10.5,10.55,10.58,10.6
])
plt.figure()
plt.plot(t,y,'k*')

# y=at^2+bt+c

A=np.c_[t**2,t,np.ones(t.shape)]

w=lg.inv(A.T.dot(A)).dot(A.T).dot(y)

plt.plot(t,w[0]*t**2+w[1]*t+w[2])
plt.show()


png

1
2
3
4
5
6
7
8
9
10
11
12
13
t=np.arange(1,17,1)
t=t.reshape(-1,1)
y=np.array([4,6.4,8,8.8,9.22,9.5,9.7,9.86,10,10.20,10.32,10.42,10.5,10.55,10.58,10.6
])
plt.figure()
plt.plot(t,y,'k*')
# y=at^2+bt+c

lin_reg=LinearRegression()
lin_reg.fit(t,y)
plt.plot(t,lin_reg.predict(t).reshape(-1,1))
plt.show()


png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
t=np.arange(1,17,1)
t=t.reshape(-1,1)
y=np.array([4,6.4,8,8.8,9.22,9.5,9.7,9.86,10,10.20,10.32,10.42,10.5,10.55,10.58,10.6])
plt.figure()
plt.plot(t,y,'k*')


# y=at^2+bt+c

poly_features=pl(degree=2,include_bias=False)
x_poly=poly_features.fit_transform(t)
lin_reg=LinearRegression()
lin_reg.fit(x_poly,y)
plt.plot(t,lin_reg.coef_[1]*t**2+lin_reg.coef_[0]*t+lin_reg.intercept_)
plt.show()


png

1

上一篇:
决策树
下一篇:
朴素贝叶斯
本文目录
本文目录