吴恩达机器学习-习题及答案ex1 - Regression

Linear Regression & Multivariate Regression

 

百度云链接:链接:https://pan.baidu.com/s/1Fk1G67CASqg_ivdWCK2JZg 
提取码:suqv 
 

作业是用Octave做两个习题:

第一个,打开ex1 是做一个人口和经济利益最大化的开店方式

Step1: 读取数据

 

data = load('ex1data1.txt');
X = data(:, 1); y = data(:, 2);
m = length(y)

 X[m,1]   y[m,1]   

m : num of train examples

还有什么没有做?先思考,step3会说

Step2: 数据可视化

plotData(X, y);

Step3: 处理数据

X = [ones(m, 1), data(:,1)];  % Add a column of ones to x
theta = zeros(2, 1); % initialize fitting parameters

% Some gradient descent settings
iterations = 1500;
alpha = 0.01;

添加一行ones全是1给参数theta(1)

Step4: 计算cost function

J = computeCost(X, y, theta);

%%函数:

function J = computeCost(X, y, theta)
%COMPUTECOST Compute cost for linear regression
%   J = COMPUTECOST(X, y, theta) computes the cost of using theta as the
%   parameter for linear regression to fit the data points in X and y

% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly 
J = 0;

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta
%               You should set J to the cost.

A = (X*theta-y).^2(:);
J = sum(A)/(2*m);



% =========================================================================

end

Step5: Loop : gradient descent

theta = gradientDescent(X, y, theta, alpha, iterations);

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
%GRADIENTDESCENT Performs gradient descent to learn theta
%   theta = GRADIENTDESCENT(X, y, theta, alpha, num_iters) updates theta by 
%   taking num_iters gradient steps with learning rate alpha

% Initialize some useful values
m = length(y); % number of training examples
J_history = zeros(num_iters, 1);

for iter = 1:num_iters

    % ====================== YOUR CODE HERE ======================
    % Instructions: Perform a single gradient step on the parameter vector
    %               theta. 
    %
    % Hint: While debugging, it can be useful to print out the values
    %       of the cost function (computeCost) and gradient here.
    %
    A = (X*theta - y)';
    B = (A*X)';

    theta = theta - alpha/m*(B);






    % ============================================================

    % Save the cost J in every iteration    
    J_history(iter) = computeCost(X, y, theta);

end

end

 

 

第二个,打开ex1_multi 是做一个根据size 和 bedroom数量来预测房价

基本做法和第一个一样,不再赘述

mu=mean(X_norm);

sigma=std(X_norm);

X_norm=(X_norm-ones(size(X,1),1)*mu)./(ones(size(X,1),1)*sigma);

记住这个是Mean Normalization处理,对于数据特征范围相差太大时,非常有用

 

第三个内容涵盖在第二个中: Normal Equation 正则方程求解

 

 

你可能感兴趣的:(吴恩达机器学习-习题及答案ex1 - Regression)