吴恩达老师机器学习课程课后作业ex2随笔

随笔嘛,所以会有各种奇怪的笔记混进去,比如英语单词,以及各种答案搬运这些。

  1. outline(n.)草稿,轮廓;(v.)打草稿,概述,画轮廓
  2. plotData.m:
    1 .题目大意就是给出一个训练集,每个样例有三个元素,分别是测试1的分数、测试2的分数以及是否录取,然后让我们画出一个坐标轴是测试分数,坐标点是录取结果的图。
    2 .答案代码(ex2.pdf中已经给出来了):
% Find Indices of Positive and Negative Examples
pos = find(y == 1); neg = find(y == 0);
% Plot Examples
plot(X(pos, 1), X(pos, 2), 'k+','LineWidth', 2, 'MarkerSize', 7);
plot(X(neg, 1), X(neg, 2), 'ko', 'MarkerFaceColor', 'y','MarkerSize', 7);

3.recall:(v.)回忆
4.构造sigmoid函数:

function g = sigmoid(z)
%SIGMOID Compute sigmoid functoon
%   J = SIGMOID(z) computes the sigmoid of z.

% You need to return the following variables correctly 
g = zeros(size(z));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix,
%               vector or scalar).
g = 1 ./ ( 1 + exp(-z) ) ;
% =============================================================

end


这是取z=randn(1000,1)时的图像

5 .costFunction.m:

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
%               You should set J to the cost.
%               Compute the partial derivatives and set grad to the partial
%               derivatives of the cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
%


J=-1/m*sum((y.*log(sigmoid(X*theta)))+(1-y).*log(1-sigmoid(X*theta)));
grad=1/m*(X'*(sigmoid(X*theta)-y));

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

做这个题的时候发现几个以前没注意到的点:

  1. 虽然octave中对矩阵的加减以及点加点减没有严格区分,但是对于矩阵乘法和点乘还是有严格区分的。
  2. 如果所求值是一个数,那么我们多会用到sum,点加减乘这些;如果所求值是一个矩阵或者向量,那么我么多会用到矩阵乘法,因为矩阵乘法本身就包含了连加操作。

6 .optimal(adj)最佳的,最优的
7 .constraint(n.)约束,强制
8 .snip(v)剪
9 .snippet [ˈsnɪpɪt] (n.)小片,片段,不知天高地厚的年轻人
10 .决策边界绘制函数:

function plotDecisionBoundary(theta, X, y)
%PLOTDECISIONBOUNDARY Plots the data points X and y into a new figure with
%the decision boundary defined by theta
%   PLOTDECISIONBOUNDARY(theta, X,y) plots the data points with + for the 
%   positive examples and o for the negative examples. X is assumed to be 
%   a either 
%   1) Mx3 matrix, where the first column is an all-ones column for the 
%      intercept.
%   2) MxN, N>3 matrix, where the first column is all-ones

% Plot Data
plotData(X(:,2:3), y);
hold on

if size(X, 2) <= 3
    % Only need 2 points to define a line, so choose two endpoints
    plot_x = [min(X(:,2))-2,  max(X(:,2))+2];

    % Calculate the decision boundary line
    plot_y = (-1./theta(3)).*(theta(2).*plot_x + theta(1));

    % Plot, and adjust axes for better viewing
    plot(plot_x, plot_y)
    
    % Legend, specific for the exercise
    legend('Admitted', 'Not admitted', 'Decision Boundary')
    %下面这条axis指令用于取横纵轴的范围,四个参数分别表示:x_min,x_max;y_min,y_max
    axis([30, 100, 30, 100])
    
else
    % Here is the grid range
    u = linspace(-1, 1.5, 50);
    v = linspace(-1, 1.5, 50);

    z = zeros(length(u), length(v));
    % Evaluate z = theta*x over the grid
    for i = 1:length(u)
        for j = 1:length(v)
            z(i,j) = mapFeature(u(i), v(j))*theta;
        end
    end
    z = z'; % important to transpose z before calling contour

    % Plot z = 0
    % Notice you need to specify the range [0, 0]
    contour(u, v, z, [0, 0], 'LineWidth', 2)
end
hold off

end

11 .个人觉得plot_x的两个最最值点不需要加减2,下面左图是没有加减2画出的决策边界,右图是加减2后画出的决策边界,可以发现二者并没有太大区别:
在这里插入图片描述
12.计算决策边界(即计算plot_y)的依据是公式 θ \theta θ0+ θ \theta θ1X1+ θ \theta θ2X2=0,然后plot_y存的是X2的两个值

13 .predict.m:

function p = predict(theta, X)
%PREDICT Predict whether the label is 0 or 1 using learned logistic 
%regression parameters theta
%   p = PREDICT(theta, X) computes the predictions for X using a 
%   threshold at 0.5 (i.e., if sigmoid(theta'*x) >= 0.5, predict 1)

m = size(X, 1); % Number of training examples

% You need to return the following variables correctly
p = zeros(m, 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Complete the following code to make predictions using
%               your learned logistic regression parameters. 
%               You should set p to a vector of 0's and 1's
%

for i=1:m,
	if   X(i,:)*theta>=0,
		p(i)=1;
	else
		p(i)=0;
	end
end

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


end

关于这个题,还有一个答案是这样写的:

k = find(sigmoid( X * theta) >= 0.5 );
p(k)= 1;

通过这个题可以知道,有时候循环可以直接用find指令代替

14 .fabrication plant(n.)加工厂
15 .microchip(n.)微芯片
16 .QA:quality assurance质量保证
17 .susceptible 英[səˈseptəbl] (adj)可接受的,易受影响的
18 .expressive(adj.)有表现力的
19 .combat(vt)与…战斗,减少 (n.)战斗,搏斗
20 .evenly(adv)均匀地
evenly spaced grid等间距网格

21 .costFunctionReg.m:

function [J, grad] = costFunctionReg(theta, X, y, lambda)
%COSTFUNCTIONREG Compute cost and gradient for logistic regression with regularization
%   J = COSTFUNCTIONREG(theta, X, y, lambda) computes the cost of using
%   theta as the parameter for regularized logistic regression and the
%   gradient of the cost w.r.t. to the parameters. 

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

% You need to return the following variables correctly 
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
%               You should set J to the cost.
%               Compute the partial derivatives and set grad to the partial
%               derivatives of the cost w.r.t. each parameter in theta


%theta_1=[0;theta(2:end)];    % 先把theta(1)拿掉,不参与正则化
%J= -1 * sum( y .* log( sigmoid(X*theta) ) + (1 - y ) .* log( (1 - sigmoid(X*theta)) ) ) / m  + lambda/(2*m) * theta_1' * theta_1 ;
%grad = ( X' * (sigmoid(X*theta) - y ) )/ m + lambda/m * theta_1 ;
      
J=-1/m*sum((y.*log(sigmoid(X*theta)))+(1-y).*log(1-sigmoid(X*theta)))+lambda/(2*m)*sum(theta.^2);
grad=1/m*(X'*(sigmoid(X*theta)-y));
n=size(theta);
for i=2:n,
	grad(i)+=lambda/m*theta(i);
end



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

end

这道题再次证明了向量化的重要性,看来以后真的得多用向量化的思想了,不然学了也没用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值