1. The Basic Theory

Hypothesis: hθ(x)=θ0+θ1xh_\theta(x)=\theta_0+\theta_1xhθ​(x)=θ0​+θ1​x

Parameters: θ0,θ1\theta_0, \theta_1θ0​,θ1​

Cost Function: J(θ)=12m∑i=1m(hθ(x(i))−y(i))2J(\theta)=\frac{1}{2m}\sum\limits_{i=1}^m(h_\theta(x^{(i)})-y^{(i)})^2J(θ)=2m1​i=1∑m​(hθ​(x(i))−y(i))2

Objective Function: min⁡θ0,θ1J(θ)\min\limits_{\theta_0,\theta_1}J(\theta)θ0​,θ1​min​J(θ)

2. Gradient descent of linear regression

2.1 What is the gradient descent ?

We adopt the Gradient Descent Method to find the optimum of θ0,θ1\theta_0,\theta_1θ0​,θ1​

θj=θj−α∂∂θjJ(θ0,θ1)=θj−αm∑i=1m(hθ(x(i))−y(i))∂hθ(x)∂θj\theta_j=\theta_j-\alpha \frac{\partial}{\partial\theta_j}J(\theta_0,\theta_1) =\theta_j-\frac{\alpha}{m}\sum\limits_{i=1}^m(h_\theta(x^{(i)})-y^{(i)})\frac{\partial h_\theta(x)}{\partial\theta_j}θj​=θj​−α∂θj​∂​J(θ0​,θ1​)=θj​−mα​i=1∑m​(hθ​(x(i))−y(i))∂θj​∂hθ​(x)​

so, when j=0j=0j=0, θ0=θ0−αm∑i=1m(hθ(x(i))−y(i))\theta_0=\theta_0-\frac{\alpha}{m}\sum\limits_{i=1}^m(h_\theta(x^{(i)})-y^{(i)})θ0​=θ0​−mα​i=1∑m​(hθ​(x(i))−y(i)).

     j=1j=1j=1, θ1=θ1−αm∑i=1m[(hθ(x(i))−y(i))∗x(i)]\theta_1=\theta_1-\frac{\alpha}{m}\sum\limits_{i=1}^m[(h_\theta(x^{(i)})-y^{(i)})*x^{(i)}]θ1​=θ1​−mα​i=1∑m​[(hθ​(x(i))−y(i))∗x(i)].

We’d better to update the parameters simultaneously.

2.2 How to implement it?

STEP 1: Initialize the θ0,θ1\theta_0,\theta_1θ0​,θ1​

STEP 2: Calculate the hθ(x)h_\theta(x)hθ​(x)

STEP 3: Come up with the new θ0,θ1\theta_0,\theta_1θ0​,θ1​ under the condition of the hθ(x)h_\theta(x)hθ​(x) in STEP 2

STEP 4: Loop from STEP 2 - STEP 3

When to stop? We can set the times of iteration.

2.3 Matlab code for gradient descent

%% ================== Data Generation ===================
x = normrnd(0, 0.6, 100, 1);
noise = normrnd(0, 0.03, 100, 1);
y = 0.43 + 0.22 * x + noise;%% ================== Linear Regression ===================
m = length(y);
X = [ones(m,1),x];
% initialize the theta
theta = [0;0];theta_length = length(theta);
itera = 5000;
alpha = 0.01;
costFunc = zeros(m,1);
theta_itera = zeros(m,2);for j = 1:iteratheta_itera(j,:) = theta';  % record all the theta values during the processhypothesis = X * theta;costFunc(j) = (1/2*m) * sum(((hypothesis - y) .^ 2)); % cost function% using the gradienet descent algorithm to find the optimumfor i = 1:theta_lengththeta(i) = theta(i) - (alpha/m) * ((hypothesis - y)'* X(:,i));  end
end%% ================== Visualization ===================
%% plot the dataset and the fit line
figure;
for j = 1:10:iteraplot(x,y,'bx');hold onh2 = plot(X(:,2),X*theta_itera(j,:)','r');pause(0.5);delete(h2);
end%% plot the iterate curve
figure;
plot3(theta_itera(:,1),theta_itera(:,2),costFunc,'r');
xlabel('\theta_0'); ylabel('\theta_1');
hold on%% plot the costFunction
% Grid over which we will calculate J
theta0_vals = linspace(-1, 1, 100);
theta1_vals = linspace(-1, 1, 100);
plot_costFunc = zeros(length(theta0_vals), length(theta1_vals));
% Fill
for i = 1:length(theta0_vals)for j = 1:length(theta1_vals)t = [theta0_vals(i); theta1_vals(j)];plot_hypothesis = X * t;plot_costFunc(i,j) = (1/2*m) * sum(((plot_hypothesis - y) .^ 2));end
end% Surface plot
%   In this case, the vertices of the surface patches are the triples
%   (x(j), y(i), Z(i,j)). Note that x corresponds to the columns of Z
%   and y corresponds to the rows.
plot_costFunc = plot_costFunc';
surf(theta0_vals, theta1_vals,plot_costFunc)
xlabel('\theta_0'); ylabel('\theta_1');%% Contour map of the costFunction
figure;
contour(theta0_vals, theta1_vals,  plot_costFunc, logspace(-2, 3, 20))
xlabel('\theta_0'); ylabel('\theta_1');
hold on;
plot(theta_itera(:,1), theta_itera(:,2), 'r');


ML Notes: Week 1 - Univariate Linear Regression相关推荐

  1. ML Notes: Week 2 - Multivariate Linear Regression

    1. The basic theory of the multivariate linear regression Hypothesis: hθ(x)=θ0x0+θ1x1+-+θnxn=θTXh_\t ...

  2. 吴恩达机器学习笔记(2)——单变量线性回归(Univariate linear regression)

    一.模型描述 上一章已经通过卖房价格的模型简单介绍了什么是回归:我们尝试将变量映射到某一个连续函数上. 这章我们将这个问题简单地量化为单变量线性回归模型(Univariate linear regre ...

  3. machine learning (2)-linear regression with one variable

    machine learning- linear regression with one variable(2) Linear regression with one variable = univa ...

  4. ML - 线性回归(Linear Regression)

    文章目录 关于线性回归 线性回归特点 和 kNN 图示的区别 简单线性回归 算法原理 如何求解机器学习算法? 编程实现简单线性回归 向量化运算 封装线性回归类 评估方法 向量化运算的性能测试 线性回归 ...

  5. 李宏毅 2020 ML Homework 1: Linear Regression

    (注:本文转载自GitHub:https://github.com/Iallen520/lhy_DL_Hw) (所有作业的数据,链接:https://pan.baidu.com/s/1m1QbhrzK ...

  6. Coursera公开课笔记: 斯坦福大学机器学习第四课“多变量线性回归(Linear Regression with Multiple Variables)”

    Coursera公开课笔记: 斯坦福大学机器学习第四课"多变量线性回归(Linear Regression with Multiple Variables)" 斯坦福大学机器学习第 ...

  7. Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable

    本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归.Octave Tutorial.Logistic Regression.Regularization.神经网络.机器学 ...

  8. Stanford机器学习---第一讲. Linear Regression with one variable

    本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归.Octave Tutorial.Logistic Regression.Regularization.神经网络.机器学 ...

  9. Linear Regression Logistic Regression

    Linear Regression & Logistic Regression Keywords List keywords you may encounter when exploring ...

最新文章

  1. 8148和8127中的ezsdk和dvrsdk
  2. java常用类总结_java——常用类的总结
  3. 静态方法+工厂方法(静态方法用途)
  4. Drools:基于PHREAK堆栈的评估和向后链接
  5. python如何计算个人gpa_【Python】计算GPA | 学步园
  6. SVM(三),支持向量机,线性不可分和核函数
  7. adb工具包_如何使用命令刷机 ADB与FASTBOOT工具使用教程
  8. vue src动态加载请求获取的图片
  9. 【MicroPython ESP32】ssd1306 0.96“OLED+网络时钟
  10. 对接payjs的个人支付之微信扫码支付接口
  11. 下载优酷视频 基于python2
  12. 设计师浅谈标志设计经验
  13. Win10电脑开机进不去系统怎么修复
  14. Elementui el-select创建条目的多选下拉框 自定义校验 新增条目时字符长度限制
  15. java List去除重复数据的五种方式
  16. 2022年最新云开发去水印小程序源码
  17. Java读取word模板,并动态生成word
  18. bp验证码爆破插件二改
  19. 二进制bit0是什么意思_什么是ip地址
  20. Java编程基础小总结

热门文章

  1. Modbus转Profinet网关连接海利普变频器配置方法
  2. JDBC连接数据库详细讲解及代码演示
  3. pymysql使用_使用pymysql的AWS Lambda与RDS
  4. 清除知乎动态(点赞)
  5. mt6762添加gpio按键方法与问题调试
  6. 【19调剂】杭州师范大学阿里巴巴商学院2019年硕士研究生预调剂信息
  7. 角色 提问 回答 小马识途营销顾问分享问答营销实战三大要点
  8. 第五届“传智杯”全国大学生计算机大赛(练习赛)传智杯 #5 练习赛] 平等的交易
  9. Goldbach's Conjecture
  10. C# TCP如何限制单个客户端的访问流量