import numpy as np
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
X,label = make_blobs(1000,2,centers=2)
x ,y = np.array([X[:,0]]).T,np.array([X[:,1]]).T
# 初始参数
learning_rate = 0.01
theta = np.random.random((2,1))# np.array([[1],[2]])#
x_ = x
ones_x = np.hstack((np.ones_like(x_),x_))
Y=y
def SGD(ones_x, theta, Y):
def loss_function(theta,X, Y):
x_theta_Y = np.dot(ones_x, theta) - Y
H_x = np.dot(x_theta_Y.T, x_theta_Y)/X.shape[0]*0.5
return H_x
def gradiant_function(ones_x, theta, Y):
x_theta_Y = np.dot(ones_x, theta) - Y
return np.dot(ones_x.T, x_theta_Y)
flag = True
loss = []
while flag == True:
delta_theta = gradiant_function(ones_x, theta, Y) / ones_x.shape[0]
theta = theta - learning_rate * delta_theta
flag = np.all(np.abs(delta_theta) > 1e-7)#当梯度变化很小时就可以停止了
loss_ = loss_function(theta, X, Y)
loss .append(np.array(loss_).flatten())
return loss, theta
loss, theta = SGD(ones_x,theta,Y)
plt.subplot(2,1,1)
plt.plot(loss,label = 'Loss')
plt.legend()
plt.subplot(2,1,2)
x_ = np.linspace(ones_x[:,1].min(),ones_x[:,1].max(),100)
y_ = x_*theta[1]+theta[0]*np.ones_like(theta[1])
plt.plot(x_,y_,color='orange')
plt.scatter(x,y,s=30,edgecolor='black',color= 'yellow')
plt.show()
print(min(loss))
梯度下降法曲线拟合
最新推荐文章于 2023-04-02 17:22:49 发布