看了很多的线性回归代码,感觉都没有写出算法的核心来,因此重新写了线性回归代码。下面的代码中缺少迭代退出机制,也就是 abs(ypredict - yobserver) < eps, 如果满足这个条件,就是求出了最优的theta值,就不需要继续迭代了。

还有问题,就是如何理解等高线的梯度下降?这个欢迎大家讨论。

代码见下:

% Exercise 2 Linear Regression

% Data is roughly based on 2000 CDC growth figures
% for boys
%
% x refers to a boy's age
% y is a boy's height in meters
%

clear all; close all; clc
x = load('ex2x.dat'); y = load('ex2y.dat');

m = length(y); % number of training examples

% Plot the training data
figure; % open a new figure window
plot(x, y, 'o');
ylabel('Height in meters')
xlabel('Age in years')

% Gradient descent
x = [ones(m, 1) x]; % Add a column of ones to x
theta = zeros(size(x(1,:)))'; % initialize fitting parameters
MAX_ITR = 1500;
alpha = 0.07;

for num_iterations = 1:MAX_ITR
%     This is a vectorized version of the
%     gradient descent update formula
%     It's also fine to use the summation formula from the videos
   
    %Here is the gradient
%    grad = (1/m).* x' * ((x * theta) - y);
%    
%    % Here is the actual update
%    theta = theta - alpha .* grad;
 %theta
   
    % Sequential update: The wrong way to do gradient descent
      grad1 = (1/m).* x(:,1)' * ((x * theta) - y);
%      theta(1) = theta(1) + alpha*grad1;
     grad2 = (1/m).* x(:,2)' * ((x * theta) - y);
%      theta(2) = theta(2) + alpha*grad2;
     grad=[grad1,grad2]';
     theta = theta - alpha .* grad;
end
% print theta to screen
theta

% Plot the linear fit
hold on; % keep previous plot visible
plot(x(:,2), x*theta, '-')
legend('Training data', 'Linear regression')
hold off % don't overlay any more plots on this figure

% Closed form solution for reference
% You will learn about this method in future videos
exact_theta = (x' * x)\x' * y

% Predict values for age 3.5 and 7
predict1 = [1, 3.5] *theta
predict2 = [1, 7] * theta

% Calculate J matrix

% Grid over which we will calculate J
theta0_vals = linspace(-3, 3, 100);
theta1_vals = linspace(-1, 1, 100);

% initialize J_vals to a matrix of 0's
J_vals = zeros(length(theta0_vals), length(theta1_vals));

for i = 1:length(theta0_vals)
   for j = 1:length(theta1_vals)
   t = [theta0_vals(i); theta1_vals(j)];   
   J_vals(i,j) = (0.5/m) .* (x * t - y)' * (x * t - y);
    end
end

% Because of the way meshgrids work in the surf command, we need to
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';
% Surface plot
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1');

% Contour plot
figure;
% Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100
contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 2, 15))
xlabel('\theta_0'); ylabel('\theta_1');



线性回归代码matlab相关推荐

  1. Matlab回归算法,线性回归算法Matlab实现

    编辑推荐: 本文来自于csdn,本文将通过以OCR(光学字符识别)的场景来介绍深度学习在计算机视觉中的应用. 一,单变量线性回归: 1. 数据分布,x轴是属性城市人口,y轴是标签值盈利: 2. 目的: ...

  2. python编程代码示例_python编程线性回归代码示例

    用python进行线性回归分析非常方便,有现成的库可以使用比如:numpy.linalog.lstsq例子.scipy.stats.linregress例子.pandas.ols例子等. 不过本文使用 ...

  3. 高斯投影法正反算代码MATLAB版本

    高斯投影法正反算代码MATLAB版本 说明 高斯投影正算 高斯投影反算 说明 之前一个帖子给出了正反算的C语言代码 链接如下: link 我把它移植到了matlab中 支持向量输入 高斯投影正算 高斯 ...

  4. 示例代码-Matlab绘制高斯分布曲面图(1)

    高斯分布 \quad在数据建模时,经常会用到高斯分布模型[1,2],下面我们就使用Matlab来绘制高斯分布曲面. \quad另一种画法可参考:示例代码-Matlab绘制高斯分布曲面图(2). 1. ...

  5. 人工神经网络matlab代码,matlab神经网络30例代码

    如何在matlab中建立多层bp神经网络 当你用newff的时候,里面有一个参数可以控制层数,比如说:P = [0 1 2 3 4 5 6 7 8 9 10];T = [0 1 2 3 4 3 2 1 ...

  6. 机器学习之MATLAB代码--MATLAB量子粒子群优化LSTM超参数负荷预测(十三)

    机器学习之MATLAB代码--MATLAB量子粒子群优化LSTM超参数负荷预测(十三) 代码 数据 结果 代码 代码按照下列顺序依次: 1. function result(true_value,pr ...

  7. matlab lbm 代码,Matlab实现格子玻尔兹曼方法(Lattice Boltzmann Method,LBM)模拟

    %LBM的matlab代码 %Matlab实现格子玻尔兹曼方法(Lattice Boltzmann Method,LBM)模拟 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % c ...

  8. matlab lsa,omlsa cohen大神的OM-LSA仿真代码 MATLAB soft 联合开发网 - pudn.com

    omlsa 所属分类:其他 开发工具:matlab 文件大小:4KB 下载次数:22 上传日期:2019-01-16 10:01:04 上 传 者:wraII 说明:  cohen大神的OM-LSA仿 ...

  9. ISOMAP函数官方代码-matlab

    ISOMAP函数官方代码-matlab function [Y, R, E] = Isomap(D, n_fcn, n_size, options); % ISOMAP Computes Isomap ...

最新文章

  1. 人工智能学习--知识图谱的关键技术及其智能应用
  2. [转]使用Ant进行ssh和scp操作
  3. Nginx学习笔记(三) Nginx基本数据结构
  4. c语言t0中断方式编程,PIC C语言编程_PICC中断函数的实现
  5. 阶段5 3.微服务项目【学成在线】_day03 CMS页面管理开发_08-新增页面-前端-Api调用...
  6. 关于HRESULT判断的宏(SUCCEEDED/FAILED)
  7. pid温度控制c语言程序,51单片机PID温度控制程序
  8. 粗识计算机--Java学习Day01
  9. Spark开发:Spark大数据开发编程示例
  10. 单片机和嵌入式的区别
  11. cocos2d-html5教程之重要概念
  12. NIKE ZOOM ASTORIA SKY HI ACG (MUSTARD/TAN)
  13. python3中使用pip3错误syn_python-pip3错误-'_NamespacePath'对象没有属性'sort'
  14. lightOJ 1278
  15. (2022牛客多校五)H-Cutting Papers(签到)
  16. 打开Word提示:Office已阻止访问以下嵌入对象,以便保护你的安全解决方法
  17. 计算机运行加减乘除哪个最慢,计算机算加减乘除的时间对比
  18. MySQL数据库项目式教程思维导图
  19. 手把手使用Android自带SQLite数据库(1)—— 建立核心文件
  20. 如何将下载的影像变换为西安80坐标系

热门文章

  1. php中register_global,PHP安全之register_globals的on和off的区别
  2. python创建数组并运行,python中Numpy的数组创建
  3. original_keras_version = f.attrs[‘keras_version‘].decode(‘utf8‘)
  4. linux中的nm命令
  5. Python量化(八)下影线选股法
  6. 二分图带权最大匹配费用流_一文掌握阻抗匹配
  7. python programming training(四):动态规划
  8. 文巾解题 203. 移除链表元素
  9. 数据中台实战(四):商品分析(产品设计篇)
  10. 为什么通常牛顿法比梯度下降法能更快的收敛