【机器学习】【线性回归】梯度下降

文章目录

    • @[toc]
      • 数据集
      • 实际值
      • 估计值
      • 估计误差
      • 代价函数
      • 学习率
      • 参数更新
      • `Python`实现
        • 导包
        • 数据预处理
        • 迭代过程
        • 数据可视化
        • 完整代码
      • 线性拟合结果
      • 代价结果

因上努力

个人主页:丷从心

系列专栏:机器学习

果上随缘


数据集

( x ( i ) , y ( i ) ) , i = 1 , 2 , ⋯ , m \left(x^{(i)} , y^{(i)}\right) , i = 1 , 2 , \cdots , m (x(i),y(i)),i=1,2,,m


实际值

y ( i ) y^{(i)} y(i)


估计值

h θ ( x ( i ) ) = θ 0 + θ 1 x ( i ) h_{\theta}\left(x^{(i)}\right) = \theta_{0} + \theta_{1} x^{(i)} hθ(x(i))=θ0+θ1x(i)


估计误差

h θ ( x ( i ) ) − y ( i ) h_{\theta}\left(x^{(i)}\right) - y^{(i)} hθ(x(i))y(i)


代价函数

J ( θ ) = J ( θ 0 , θ 1 ) = 1 2 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) 2 = 1 2 m ∑ i = 1 m ( θ 0 + θ 1 x ( i ) − y ( i ) ) 2 J(\theta) = J(\theta_{0} , \theta_{1}) = \cfrac{1}{2m} \displaystyle\sum\limits_{i = 1}^{m}{\left(h_{\theta}\left(x^{(i)}\right) - y^{(i)}\right)^{2}} = \cfrac{1}{2m} \displaystyle\sum\limits_{i = 1}^{m}{\left(\theta_{0} + \theta_{1} x^{(i)} - y^{(i)}\right)^{2}} J(θ)=J(θ0,θ1)=2m1i=1m(hθ(x(i))y(i))2=2m1i=1m(θ0+θ1x(i)y(i))2


学习率

  • α \alpha α是学习率,一个大于 0 0 0的很小的经验值,决定代价函数下降的程度

参数更新

Δ θ j = ∂ ∂ θ j J ( θ 0 , θ 1 ) \Delta{\theta_{j}} = \cfrac{\partial}{\partial{\theta_{j}}} J(\theta_{0} , \theta_{1}) Δθj=θjJ(θ0,θ1)

θ j : = θ j − α Δ θ j = θ j − α ∂ ∂ θ j J ( θ 0 , θ 1 ) \theta_{j} := \theta_{j} - \alpha \Delta{\theta_{j}} = \theta_{j} - \alpha \cfrac{\partial}{\partial{\theta_{j}}} J(\theta_{0} , \theta_{1}) θj:=θjαΔθj=θjαθjJ(θ0,θ1)

[ θ 0 θ 1 ] : = [ θ 0 θ 1 ] − α [ ∂ J ( θ 0 , θ 1 ) ∂ θ 0 ∂ J ( θ 0 , θ 1 ) ∂ θ 1 ] \left[ \begin{matrix} \theta_{0} \\ \theta_{1} \end{matrix} \right] := \left[ \begin{matrix} \theta_{0} \\ \theta_{1} \end{matrix} \right] - \alpha \left[ \begin{matrix} \cfrac{\partial{J(\theta_{0} , \theta_{1})}}{\partial{\theta_{0}}} \\ \cfrac{\partial{J(\theta_{0} , \theta_{1})}}{\partial{\theta_{1}}} \end{matrix} \right] [θ0θ1]:=[θ0θ1]α θ0J(θ0,θ1)θ1J(θ0,θ1)

[ ∂ J ( θ 0 , θ 1 ) ∂ θ 0 ∂ J ( θ 0 , θ 1 ) ∂ θ 1 ] = [ 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x ( i ) ] = [ 1 m ∑ i = 1 m e ( i ) 1 m ∑ i = 1 m e ( i ) x ( i ) ] e ( i ) = h θ ( x ( i ) ) − y ( i ) \left[ \begin{matrix} \cfrac{\partial{J(\theta_{0} , \theta_{1})}}{\partial{\theta_{0}}} \\ \cfrac{\partial{J(\theta_{0} , \theta_{1})}}{\partial{\theta_{1}}} \end{matrix} \right] = \left[ \begin{matrix} \cfrac{1}{m} \displaystyle\sum\limits_{i = 1}^{m}{\left(h_{\theta}\left(x^{(i)}\right) - y^{(i)}\right)} \\ \cfrac{1}{m} \displaystyle\sum\limits_{i = 1}^{m}{\left(h_{\theta}\left(x^{(i)}\right) - y^{(i)}\right) x^{(i)}} \end{matrix} \right] = \left[ \begin{matrix} \cfrac{1}{m} \displaystyle\sum\limits_{i = 1}^{m}{e^{(i)}} \\ \cfrac{1}{m} \displaystyle\sum\limits_{i = 1}^{m}{e^{(i)} x^{(i)}} \end{matrix} \right] \kern{2em} e^{(i)} = h_{\theta}\left(x^{(i)}\right) - y^{(i)} θ0J(θ0,θ1)θ1J(θ0,θ1) = m1i=1m(hθ(x(i))y(i))m1i=1m(hθ(x(i))y(i))x(i) = m1i=1me(i)m1i=1me(i)x(i) e(i)=hθ(x(i))y(i)

[ ∂ J ( θ 0 , θ 1 ) ∂ θ 0 ∂ J ( θ 0 , θ 1 ) ∂ θ 1 ] = [ 1 m ∑ i = 1 m e ( i ) 1 m ∑ i = 1 m e ( i ) x ( i ) ] = [ 1 m ( e ( 1 ) + e ( 2 ) + ⋯ + e ( m ) ) 1 m ( e ( 1 ) + e ( 2 ) + ⋯ + e ( m ) ) x ( i ) ] = 1 m [ 1 1 ⋯ 1 x ( 1 ) x ( 2 ) ⋯ x ( m ) ] [ e ( 1 ) e ( 2 ) ⋮ e ( m ) ] = 1 m X T e = 1 m X T ( X θ − y ) \begin{aligned} \left[ \begin{matrix} \cfrac{\partial{J(\theta_{0} , \theta_{1})}}{\partial{\theta_{0}}} \\ \cfrac{\partial{J(\theta_{0} , \theta_{1})}}{\partial{\theta_{1}}} \end{matrix} \right] &= \left[ \begin{matrix} \cfrac{1}{m} \displaystyle\sum\limits_{i = 1}^{m}{e^{(i)}} \\ \cfrac{1}{m} \displaystyle\sum\limits_{i = 1}^{m}{e^{(i)} x^{(i)}} \end{matrix} \right] = \left[ \begin{matrix} \cfrac{1}{m} \left(e^{(1)} + e^{(2)} + \cdots + e^{(m)}\right) \\ \cfrac{1}{m} \left(e^{(1)} + e^{(2)} + \cdots + e^{(m)}\right) x^{(i)} \end{matrix} \right] \\ &= \cfrac{1}{m} \left[ \begin{matrix} 1 & 1 & \cdots & 1 \\ x^{(1)} & x^{(2)} & \cdots & x^{(m)} \end{matrix} \right] \left[ \begin{matrix} e^{(1)} \\ e^{(2)} \\ \vdots \\ e^{(m)} \end{matrix} \right] = \cfrac{1}{m} X^{T} e = \cfrac{1}{m} X^{T} (X \theta - y) \end{aligned} θ0J(θ0,θ1)θ1J(θ0,θ1) = m1i=1me(i)m1i=1me(i)x(i) = m1(e(1)+e(2)++e(m))m1(e(1)+e(2)++e(m))x(i) =m1[1x(1)1x(2)1x(m)] e(1)e(2)e(m) =m1XTe=m1XT(y)

  • 由上述推导得

Δ θ = 1 m X T e \Delta{\theta} = \cfrac{1}{m} X^{T} e Δθ=m1XTe

θ : = θ − α Δ θ = θ − α 1 m X T e \theta := \theta - \alpha \Delta{\theta} = \theta - \alpha \cfrac{1}{m} X^{T} e θ:=θαΔθ=θαm1XTe


Python实现

导包
import numpy as np
import matplotlib.pyplot as plt
数据预处理
x = np.array([4, 3, 3, 4, 2, 2, 0, 1, 2, 5, 1, 2, 5, 1, 3])
y = np.array([8, 6, 6, 7, 4, 4, 2, 4, 5, 9, 3, 4, 8, 3, 6])m = len(x)x = np.c_[np.ones([m, 1]), x]
y = y.reshape(m, 1)
theta = np.zeros([2, 1])
迭代过程
alpha = 0.01
iter_cnt = 1000  # 迭代次数
cost = np.zeros([iter_cnt])  # 代价数据for i in range(iter_cnt):h = x.dot(theta)  # 估计值error = h - y  # 误差值cost[i] = 1 / (2 * m) * error.T.dot(error)  # 代价值# 更新参数delta_theta = 1 / m * x.T.dot(error)theta -= alpha * delta_theta
数据可视化
# 回归结果
plt.scatter(x[:, 1], y, c='blue')
plt.plot(x[:, 1], h, 'r-')
plt.show()# 代价结果
plt.plot(cost)
plt.show()
完整代码
import numpy as np
import matplotlib.pyplot as pltx = np.array([4, 3, 3, 4, 2, 2, 0, 1, 2, 5, 1, 2, 5, 1, 3])
y = np.array([8, 6, 6, 7, 4, 4, 2, 4, 5, 9, 3, 4, 8, 3, 6])m = len(x)x = np.c_[np.ones([m, 1]), x]
y = y.reshape(m, 1)
theta = np.zeros([2, 1])alpha = 0.01
iter_cnt = 1000  # 迭代次数
cost = np.zeros([iter_cnt])  # 代价数据for i in range(iter_cnt):h = x.dot(theta)  # 估计值error = h - y  # 误差值cost[i] = 1 / (2 * m) * error.T.dot(error)  # 代价值# 更新参数delta_theta = 1 / m * x.T.dot(error)theta -= alpha * delta_theta# 线性拟合结果
plt.scatter(x[:, 1], y, c='blue')
plt.plot(x[:, 1], h, 'r-')
plt.show()# 代价结果
plt.plot(cost)
plt.show()

线性拟合结果

1


代价结果

2


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/352512.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python api是什么_Python/C API

Python/C API Python/C API可能是被最广泛使用的方法。它不仅简单,而且可以在C代码中操作你的Python对象。 这种方法需要以特定的方式来编写C代码以供Python去调用它。所有的Python对象都被表示为一种叫做PyObject的结构体,并且Python.h头文件中提供了各…

Sublime Text插件列表

本文由伯乐在线 - 艾凌风 翻译,黄利民 校稿。英文出处:ipestov.com。欢迎加入翻译组。本文收录了作者辛苦收集的Sublime Text最佳插件,很全。 最佳的Sublime Text 插件 朋友们你们好!我尝试着收集了最佳的ST插件,这些插…

C语言sendto()函数:经socket传送数据

相关函数&#xff1a;send, sendmsg, recv, recvfrom, socket头文件&#xff1a;#include <sys/types.h> #include <sys/socket.h>定义函数&#xff1a;int sendto(int s, const void * msg, int len, unsigned int flags, const struct sockaddr * to, int tole…

javaone_JavaOne 2012:向上,向上和向外:使用Akka扩展软件

javaone在晚些时候的社区主题演讲之后&#xff0c;我前往希尔顿金门大桥3/4/5观看了维克多巴生 &#xff08; Viktor Klang &#xff09;的&#xff08; Typesafe &#xff09;“向上&#xff0c;向上和向外&#xff1a;Akka”演讲。 巴生&#xff08;Klang&#xff09;是Akka的…

Windows平台下动态链接库的总结

1、 动态链接库与静态连接库 静态连接库与动态链接库都是经过编译器编译之后的&#xff0c;在计算机上可以直接运行的二进制目标文件&#xff0c;就像exe文件一样&#xff0c;但不同于exe文件的是静态链接库和动态链接库不可以独立运行&#xff0c;一般而言&#xff0c;动态链接…

python建模分析实操_城市公交站点设置优化模型-基于Python

城市公交站点设置的优化分析 一、模型应用 进入21世纪以来&#xff0c;我国城市公共交通飞速发展&#xff0c;然而随着经济社会发展&#xff0c;城市不断升级以及人民生活品质越来越好&#xff0c;城市交通拥堵、出行不便等问题日益突出&#xff0c;严重损坏了市民日常的生活体…

【递归:把目录下所有文件的绝对路径给输出在控制台】

package com.bornsoft.test.capitalpool.tyc;import java.io.File;/*** author shusheng* description* Email shushengyiji.com* date 2018/10/16 10:26*/ public class DiGuiDemo2 {/***需求&#xff1a;请大家把目录下所有文件的绝对路径给输出在控制台*分析&#xff1a;*A:…

UDP sendto和recvfrom使用详解

在网络编程中&#xff0c;UDP运用非常广泛。很多网络协议是基于UDP来实现的&#xff0c;如SNMP等。大家常常用到的局域网文件传输软件飞鸽传书也是基于UDP实现的。 本篇文章跟大家分享linux下UDP的使用和实现&#xff0c;主要介绍下sendto()和recvfrom()两个函数的使用&#xf…

SpringOne Platform 2016回顾

我最近结束了在拉斯维加斯参加SpringOne Platform会议的总结。 这是我第一次参加SpringOne。 这是聆听演讲并与软件开发领域的一些顶级专家进行对话的一种体验。 如果您没有参加SpringOne&#xff0c;那么您肯定会想要阅读这篇文章。 我们将介绍这四个主题&#xff0c;以及如何…

欧姆龙cp1hum读保护解密步骤_欧姆龙PLC的NJ系列NJ产品功能介绍

欧姆龙PLC的NJ 系列NJ运动、逻辑和视觉集于一体欧姆龙PLC的NJ 系列NJ特点One Machine Control运动、逻辑和视觉集于一体将组成机械所需的各种控制设备汇集于一体&#xff0c;使用一个软件即可进行控制。 这就是Sysmac自动化平台的努力目标。 我们的新型机器自动化控制器NJ系列通…

关于CUDA和CuDNN配置的小问题

为了方便组员操作&#xff0c;简单写一下CUDA的配置啦。 首先你需要一台电脑&#xff0c;有NVDIA显卡的那种&#xff08;就那个煤气灶&#xff0c;你懂我意思吧&#xff09;&#xff0c;然后就继续往下走吧&#xff0c;如果没有的话可以找一下右上角的红叉了&#xff0c;这篇文…

PyMongo--非关系型数据库mongodb入门(一步一步 版)

PyMongo--非关系型数据库mongodb入门&#xff08;一步一步 版&#xff09; 本文主要内容&#xff1a; 1.简要介绍mongodb 2.Pymongo 3.mongo shell 4.我的mongodb入门之旅 1.简要介绍mongodb MongoDB是一个基于分布式文件存储的数据库。由C语言编写。旨在为WEB应用提供可扩展的…

python画图颜色种类_Python可视化|matplotlib07-python colormap(颜色映射)(三)

本篇详细介绍matplotlib内置的颜色条Colormap使用。 本文将学到什么&#xff1f; 1、colormap名称 2、colormap可视化 3、colormap使用方法 4、参考资料 更好的阅读体验请戳&#xff1a; 1、colormap名称 colormap颜色通过matplotlib的cm模块调用&#xff0c;print(dir(cm))即可…

element table 组件内容换行方案

element table 组件内容换行方案 white-space的值&#xff1a; normal 默认。空白会被浏览器忽略。pre 空白会被浏览器保留。其行为方式类似 HTML 中的<pre> 标签。nowrap 文本不会换行&#xff0c;文本会在在同一行上继续&#xff0c;直到遇到 <br>标签为止。pre-…

UDP Socket编程 C/C++实现 (Windows Platform SDK)

挺无聊一小程序&#xff0c;俩SB一人说一句&#xff0c;据说聊天程序最好用UDP写。 Server&#xff1a; [csharp] view plaincopy #pragma comment (lib,"ws2_32.lib") #include <Winsock2.h> #include <stdio.h> void main() {…

关于Mongodb的全面总结,学习mongodb的人,可以从这里开始!

MongoDB的内部构造《MongoDB The Definitive Guide》 MongoDB的官方文档基本是how to do的介绍&#xff0c;而关于how it worked却少之又少&#xff0c;本人也刚买了《MongoDB TheDefinitive Guide》的影印版&#xff0c;还没来得及看&#xff0c;本文原作者将其书中一些关于M…

Spring Kafka生产者/消费者样本

我的目的是演示Spring Kafka如何为原始Kafka Producer和Consumer API提供一种易于使用且对具有Spring背景的人熟悉的抽象。 示例场景 示例场景是一个简单的场景&#xff0c;我有一个系统&#xff0c;该系统生成一条消息&#xff0c;另一个系统对其进行处理 使用Raw Kafka Pr…

homelede软路由设置方法_斐讯无线路由器怎么设置 斐讯无线路由器设置方法【详解】...

无线路由器的品类在不断增加&#xff0c;人们的生活也逐渐无法离开WIFI&#xff0c;各类无线路由器的设置方法大同小异&#xff0c;为了方便不为人知的小白更方便的设置无线路由器&#xff0c;这里详细介绍一下斐讯(phicomm)无线路由器怎么设置。配置前准备&#xff1a;A、请保…

Angular 个人深究(四)【生命周期钩子】

Angular 个人深究&#xff08;四&#xff09;【生命周期钩子】 定义&#xff1a; 每个组件都有一个被 Angular 管理的生命周期。 Angular 创建它&#xff0c;渲染它&#xff0c;创建并渲染它的子组件&#xff0c;在它被绑定的属性发生变化时检查它&#xff0c;并在它从 DOM 中被…

BITMAPINFO结构

BITMAPINFO结构定义了Windows设备无关位图&#xff08;DIB&#xff09;的度量和颜色信息。 一、BITMAPINFO结构形式&#xff1a; typedef struct tagBITMAPINFO { BITMAPINFOHEADER bmiHeader; RGBQUAD bmiColors[1]; } BITMAPINFO; 二、BITMAPINFO成员 bmi…