关于machinelearning第二周的作业详细解析

来源:互联网 发布:php学生选课管理系统 编辑:程序博客网 时间:2024/06/05 01:57

这周来谈下关于第二周的作业的解析

这周本来准备花三天来结束一下第二周的课程,因为觉得第一周还是便简单的,但是第二周的作业确实难到了我。也不能说是难到吧,提出了一个小小的问题可以说。

回顾一下第一周,第一周我们提到了machinelearning的最简单的算法,单系数的线性回归,在第二周的时候前几节课稍微扩展了一下线性回归将其变为多系数,当然多系数也没有特别大的变化。之后便留下了一个作业,用matlab来实现一下线性回归。这个作业还是非常的有想法的,去coursera下下来你就知道了,包括一整个的模版以及教学pdf,引导你一步一步的做完。不过由于对matlab语法与运用的不熟悉,导致我还是花了大约一小时才写完的。下面来详细解析一下。首先让我们定义一个5*5的特征矩阵,下面的代码是他所原来就有的,warmUpExercise是我们需要自己写的。

clear ; close all; clc% Complete warmUpExercise.m fprintf('Running warmUpExercise ... \n');fprintf('5x5 Identity Matrix: \n');warmUpExercise()fprintf('Program paused. Press enter to continue.\n');pause;
function A = warmUpExercise()A = [];% ============= YOUR CODE HERE ==============A = eye(5);% ===========================================end

也是分为模块化,它给你的和自己写的,自己写的当然在your code here里。这部分太简单我们直接下一部分。

%% ======================= Part 2: Plotting =======================fprintf('Plotting Data ...\n')data = load('ex1data1.txt');X = data(:, 1); y = data(:, 2);m = length(y); % number of training examplesplotData(X, y);fprintf('Program paused. Press enter to continue.\n');pause;

再同样补全一下plotData中的东西

function plotData(x, y)% ====================== YOUR CODE HERE ======================figure; plot(x,y,'rx','MarkerSize',10);ylabel('Profit in $10,000s');xlabel('Population of City in 10,000s');% ============================================================end

很简单的我们所有训练样本都会在一张图中显示出来,有兴趣大家可以试一下。接下来是第三部分。

%% =================== Part 3: Gradient descent ===================fprintf('Running Gradient Descent ...\n')X = [ones(m, 1), data(:,1)]; % Add a column of ones to xtheta = zeros(2, 1); % initialize fitting parameters% Some gradient descent settingsiterations = 1500;alpha = 0.01;% compute and display initial costcomputeCost(X, y, theta)% run gradient descenttheta = gradientDescent(X, y, theta, alpha, iterations);% print theta to screenfprintf('Theta found by gradient descent: ');fprintf('%f %f \n', theta(1), theta(2));% Plot the linear fitplotData(X(:,2),y);hold on; % keep previous plot visibleplot(X(:,2), X*theta, '-')legend('Training data', 'Linear regression')hold off % don't overlay any more plots on this figure% Predict values for population sizes of 35,000 and 70,000predict1 = [1, 3.5] *theta;fprintf('For population = 35,000, we predict a profit of %f\n',...    predict1*10000);predict2 = [1, 7] * theta;fprintf('For population = 70,000, we predict a profit of %f\n',...    predict2*10000);fprintf('Program paused. Press enter to continue.\n');pause;

核心部分,做线性回归的。它给的模块几乎所有的内容都给到你了,要补全的函数有两个,一个是计算j的,一个是通过梯度来计算theta的值。这部分函数中的矩阵运算及表达与一般的高级语言不太一样,你时刻要注意你的变量中有几个是矩阵所以要特别的注意其中的运算以及语法。还有就是。。。千万别玩了matlab中for和if后面是要接end的!我刚开始因为之前在写python完全忽略了这一点导致前几次都没有做对。贴一下代码。

function J = computeCost(X, y, theta)m = length(y); J = 0;% ====================== YOUR CODE HERE ======================% Instructions: Compute the cost of a particular choice of theta%               You should set J to the cost.for i=1:m    J = J + (theta(1,1)+theta(2,1) * X(i,1) - y(i,1))^2;end %刚开始就是少了这个!还有上面的分号没打会导致整个赋值失败。。。也是写完python的后遗症J = J / (2*m);% =========================================================================endfunction [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)m = length(y); % number of training examplesJ_history = zeros(num_iters, 1);for iter = 1:num_iters    % ====================== YOUR CODE HERE ======================    temp1 = 0;    temp2 = 0;    for mter = 1:m        temp1 = temp1 + (theta(1,1) * X(mter,1) + theta(2,1) * X(mter,2) - y(mter,1)) * X(mter,1);        temp2 = temp2 + (theta(1,1) * X(mter,1) + theta(2,1) * X(mter,2) - y(mter,1)) * X(mter,2);    end    theta(1,1) = theta(1,1) - (alpha / m) * temp1;    theta(2,1) = theta(2,1) - (alpha / m) * temp2;    % ============================================================    J_history(iter) = computeCost(X, y, theta);endend

可能复制过来有一点乱,part3的输出部分它给的又一点小问题,我稍作修改才能在一个图中把回归线和原来的样本都输出。第四部分是验证单调性,代码都不用补,还是小贴一下。

%% ============= Part 4: Visualizing J(theta_0, theta_1) =============fprintf('Visualizing J(theta_0, theta_1) ...\n')% Grid over which we will calculate Jtheta0_vals = linspace(-10, 10, 100);theta1_vals = linspace(-1, 4, 100);% initialize J_vals to a matrix of 0'sJ_vals = zeros(length(theta0_vals), length(theta1_vals));% Fill out J_valsfor i = 1:length(theta0_vals)    for j = 1:length(theta1_vals)      t = [theta0_vals(i); theta1_vals(j)];          J_vals(i,j) = computeCost(X, y, t);    endend% 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 flippedJ_vals = J_vals';% Surface plotfigure;surf(theta0_vals, theta1_vals, J_vals)xlabel('\theta_0'); ylabel('\theta_1');% Contour plotfigure;% Plot J_vals as 15 contours spaced logarithmically between 0.01 and 100contour(theta0_vals, theta1_vals, J_vals, logspace(-2, 3, 20))xlabel('\theta_0'); ylabel('\theta_1');hold on;plot(theta(1), theta(2), 'rx', 'MarkerSize', 10, 'LineWidth', 2);

这个其实做完了之后看看还是觉得挺简单的,所以这次实验的主要目的还是熟悉一下matlab。四部分加起来就是上周提到的最最简单的线性回归的实现了。它还有另一个用矩阵乘法的形式完成的,这个就留给大家自己研究去啦。

YC你学到了吗

1 0
原创粉丝点击