吴恩达机器学习-习题及答案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 正则方程求解

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值