20200108

浩哥:摸,我疯狂地摸

回顾

逗号为连续调用,顿号为终止输出,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完成,用时3天

3 逻辑回归方程回顾

在这里插入图片描述

h(θx):假设函数/目标函数:结果为1的可能性
J(θ):代价函数:应该随着h的变化为凹/凸函数才能获得极值
在这里插入图片描述
在这里插入图片描述
个人理解,可能不对:
当样本结果为1时,h越接近0则惩罚越大,越接近1则惩罚越小
当样本结果为0时,h越接近1则惩罚越大,越接近0则惩罚越小

多与多分类问题,我们可以训练多个分类器,预测每一个分类器的输出值,以最大输出值对应的分类为它的预测值。

4 开始ex2

要执行的文件:

ex2.m
ex2_reg.m
要修改的文件:五个
在这里插入图片描述

1 Logistic Regression

1.1 Visualizing the data

pos = find(y==1); neg = find(y == 0);
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2,'MarkerSize', 7); 
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7);

1.2 Implementation
1.2.1 Warmup exercise: sigmoid function
在这里插入图片描述
可能要转战python作业了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值