zoukankan      html  css  js  c++  java
  • 斯坦福机器学习课程 Exercise 习题二

    Exercise 2: Linear Regression

    话说LaTex用起来好爽

    Matlab代码

    迭代并且画出拟合曲线

    Linear regression 公式如下

    hθ(x)=θTx=i=0nθixi

    (i是代表x的个数)

    batch gradient descent update rule

    θj:=θjα1mi=1m(h(i)θy(i))x(i)j(for all j)
    α=0.07
    x = load('L:\MachineLearning2016\ex2x.dat');
    y = load('L:\MachineLearning2016\ex2y.dat');
    
    m = length(y);
    x = [ones(m, 1), x];
    theta=[0,0];%row vector
    
    figure % open a new figure window
    plot(x(:,2), y, 'o');
    ylabel('Height in meters')
    xlabel('Age in years')
    
    hold on % Plot new data without clearing old plot
    plot(x(:,2), x*transpose(theta), '-')
    legend('Training data', 'Linear regression')
    
    %迭代方式1
     newTheta1 =theta(1,1) - transpose(x(:,1)) * (x*transpose(theta) -y) *0.07 * 0.02;
     newTheta2 =theta(1,2) - transpose(x(:,2)) * (x*transpose(theta) -y) *0.07 * 0.02;
     theta=[newTheta1,newTheta2];
    
     %迭代方式2
     for ii = 1:1500
         theta  = theta -  transpose( x*transpose(theta) -y  ) * x * 0.07 * 0.02;
        if rem(ii,100) == 0
        hold on % Plot new data without clearing old plot
        plot(x(:,2), x*transpose(theta), '-')
    
        end
     end
    
      hold on % 打印最后一条拟合曲线
      plot(x(:,2), x*transpose(theta), '+')

    画出J(θ)的图像

    Understanding J(θ)

    J(θ)=12mi=1m(h(i)θy(i))2(i means the ith of sample)
    J_vals = zeros(100, 100);   % initialize Jvals to 100x100 matrix of 0's
    theta0_vals = linspace(-3, 3, 100);
    theta1_vals = linspace(-1, 1, 100);
    for i = 1:length(theta0_vals)
          for j = 1:length(theta1_vals)
          t = [theta0_vals(i); theta1_vals(j)];%column vector
          J_vals(i,j) = sum( (x*t' -y).^2  ) * 0.01;
        end
    end
    
    % Plot the surface plot
    % 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';
    figure;
    surf(theta0_vals, theta1_vals, J_vals)
    xlabel('	heta_0'); ylabel('	heta_1')
  • 相关阅读:
    Mysql数据库的使用总结之ERROR 1146 (42S02)
    正在连接 cn.archive.ubuntu.com (91.189.91.39)] [正在连接 security.ubuntu.co.....问题的解决
    如何设定用F12进入bios
    thinkphp volist用法
    array_flip() 函数,一维数组,键名和键值交换..
    shell 备份数据库
    shell 备份数据库,并移动到备份数据库
    shell脚本实现取当前时间
    获取某日是否是工作日
    showModalDialog is not defined 的解决方案
  • 原文地址:https://www.cnblogs.com/slankka/p/9158538.html
Copyright © 2011-2022 走看看