机器学习——gradient descent

机器学习——梯度下降法
线性回归算法

本文章包含诸多错误,已作废,请勿参考

主要目的:

​ 通过线性回归算法,根据已知的数据集进行训练得出一条较吻合的曲线。

相关概念:

​ 回归曲线:可以直观呈现出数据集相关的关系,并可以进行预测。

​ 梯度下降法:

​ cost function: 回归算法是一种监督学习算法,会有响应的数据与预测数据进行比对,损失函数就是一种预测数据与实际数据偏差的表征。

实验步骤:

1:根据数据集画出对应的图

myplot


2:求出它的近似曲线:

​ 根据图片可以看出这是一个线性函数:我们可以先假设它为
h ( x ) = θ 0 + θ 1 x h(x) = θ_0 + θ_1x h(x)=θ0+θ1x

​ 而现在我们需要做的就是得出这两个参数,即θ0 和 θ1,那么如何去做呢?我们可以随意的假设两个参数的值,并且画出它的曲线,看看是否与图像吻合,由于我们有图像作为标准,即,我们知道答案,所以这是一种有监督学习。现在当我们输入某一对参数时,对训练集进行训练,那么我们可以得出一个损失函数:
c o s t = 1 2 M ∑ ( h θ ( x ( i ) ) − y ( i ) ) 2 cost = \frac{1}{2M} \sum (h_\theta(x^{(i)}) - y^{(i)})^2 cost=2M1(hθ(x(i))y(i))2
现在我们初始化 θ 1 \theta_1 θ1 θ 0 \theta_0 θ0

theta = [[0, 0]]
(这里有点小细节,请参考numpy.md)

可以预见到cost = ∑ y 2 2 × M \sum\frac{y^2}{2\times M} 2×My2 ,代码输出如下:

def computeCost(x, y, theta):
    mul = numpy.multiply(x, theta)
    mod = np.linalg.norm(mul)
    sub = numpy.subtract(y, mod)
    mod = np.linalg.norm(sub)
    cost = mod ** 2 / 2 * m
    print(cost)

image-20210601222833357

接下来给予 θ \theta θ -1 ,2

theta = [[-1, 2]]

image-20210601222937273

可以明确的是cost越小那么曲线就吻合的更好,此时我们就需要使用梯度下降法来进行选择更吻合数据的 θ \theta θ

Gradient Descend Algorithm

定义:
θ j : = θ j − α ∂ ∂ θ j J ( θ 0 , θ 1 ) \theta_j := \theta_j - \alpha\frac{\partial}{\partial\theta_j}J(\theta_0,\theta_1) θj:=θjαθjJ(θ0,θ1)
其中, α \alpha α是学习率(learning rate),可以人为进行调控 := 这个符号代表的是赋值,,而且我们可以求出对应的 θ \theta θ偏导公式
θ 0 : 1 M ∑ ( h θ ( x ( i ) ) − y ( i ) ) \theta_0 : \frac{1}{M} \sum(h_\theta(x^{(i)}) - y^{(i)}) θ0M1(hθ(x(i))y(i))

θ 1 : 1 M ∑ ( h θ ( x ( i ) ) − y ( i ) ) × x ( i ) \theta_1 : \frac{1}{M} \sum(h_\theta(x^{(i)}) - y^{(i)})\times x^{(i)} θ1M1(hθ(x(i))y(i))×x(i)

实现代码如下:

def updateTheta(rate, cost, profit, population, theta):
    # you must update theta 0 and theta 0 simultaneously
    theta0 = theta[0][0]
    theta1 = theta[1][0]
    length = len(cost)
    temp = np.sum((np.subtract(cost, profit))) / length
    temp1 = np.sum((np.dot(population, np.subtract(cost, profit)))) / length
    theta0 -= rate * temp
    theta1 -= rate * temp1
    theta = np.array([[theta0, theta1]]).T
    return theta

接下来我们进行循环更新:

while res > 0 | iteration < iterations:
        theta = updateTheta(alpha, cost, profit, population1, theta)
        cost = np.dot(population, theta)
        res = computeCost(population, profit, theta)
        iteration += 1
    print(theta)

输出结果如下:

image-20210603163258491

注意这是学习率为0.01的情况,当学习率为0.05时,则会有以下的结果

image-20210603163609947

这是跳跃步数太大的结果,会在临界点左右反复横跳


3.验证

​ 经过上述步骤得到一个比较理想的值,接下来我们进行验证看看是否符合样本数据

def test(theta):
    x = np.arange(5.0, 22.5, 0.5)
    y = [theta[0][0] + theta[1][0] * i for i in x]
    plt.figure()
    plt.plot(x, y, color='r', linestyle='-.')
    plt.show()

程序结果如下:

result

可以看的出来大致符合数据集。

完整代码请参考gitee地址:https://gitee.com/qingmoxuan/maching-learning.git

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值