梯度下降法曲线拟合

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))






  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值