20200108

北城余情 提交于 2020-01-08 19:12:31

回顾

逗号为连续调用,顿号为终止输出,hold/close保持/关闭图像
plot magic 及矩阵直接定义
表达式/函数直接执行并输出ans

1.layout格式
2.*appender绑定输出类型与目标
3.setPattern绑定格式
4.Category绑定记录
5.set priority

一.Andrew ML

1.ex1单元梯度下降已实现

在这里插入图片描述

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.

h = X*theta;
J = (h-y)'*(h-y)/(2*m);

% =========================================================================
end

J的这种表达式暂时不懂数学原理

2.ex1:(可选)多元回归

目前看来a:b代表由a到b的数据
theta=(X’*X)\X’*y;%为常规方程法

(1).feature scaling

在这里插入图片描述
在这里插入图片描述

function [X_norm, mu, sigma] = featureNormalize(X)
%FEATURENORMALIZE Normalizes the features in X 
%   FEATURENORMALIZE(X) returns a normalized version of X where
%   the mean value of each feature is 0 and the standard deviation
%   is 1. This is often a good preprocessing step to do when
%   working with learning algorithms.

% You need to set these values correctly
X_norm = X;
mu = zeros(1, size(X, 2));% size(x,2)return colmns numbers of x
sigma = zeros(1, size(X, 2));
%mu与sigma均为行向量,列数为x列数,即xi的个数
% ====================== YOUR CODE HERE ======================
% Instructions: First, for each feature dimension, compute the mean
%               of the feature and subtract it from the dataset,
%               storing the mean value in mu. Next, compute the 
%               standard deviation of each feature and divide
%               each feature by it's standard deviation, storing
%               the standard deviation in sigma. 
%
%               Note that X is a matrix where each column is a 
%               feature and each row is an example. You need 
%               to perform the normalization separately for 
%               each feature. 
%
% Hint: You might find the 'mean' and 'std' functions useful.
%       
mu=mean(X);
sigma=std(X);
for i = 1:size(X,2)
    
X_norm(:,i) = (X(:,i) - mean(X(:,i)))/sigma(:,i);

end
% ============================================================
end

(2)梯度下降

alpha = 0.01;
num_iters = 400;
alpha2 = 0.03;
alpha3 = 0.003;
% Init Theta and Run Gradient Descent 
theta = zeros(3, 1);
[theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters);
[theta2, J_history2] = gradientDescentMulti(X, y, theta, alpha2, num_iters);
[theta3, J_history3] = gradientDescentMulti(X, y, theta, alpha3, num_iters);
function [theta, J_history] = gradientDescentMulti(X, y, theta, alpha, num_iters)
%GRADIENTDESCENTMULTI Performs gradient descent to learn theta
%   theta = GRADIENTDESCENTMULTI(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 (computeCostMulti) and gradient here.
    %
 theta = theta - alpha/m*X'*(X*theta-y);










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

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

end

end

(3)常规方程

function [theta] = normalEqn(X, y)
%NORMALEQN Computes the closed-form solution to linear regression 
%   NORMALEQN(X,y) computes the closed-form solution to linear 
%   regression using the normal equations.

theta = zeros(size(X, 2), 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the code to compute the closed form solution
%               to linear regression and put the result in theta.
%

% ---------------------- Sample Solution ----------------------

theta=(X'*X)\X'*y;


% -------------------------------------------------------------


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

end

中途问题

我的结果
在这里插入图片描述
正确结果
在这里插入图片描述
需要修改computeCostMulti,每次循环更新J值
在这里插入图片描述
ex1完成,用时2天

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!