前言

学以致用,以学促用,通过笔记总结,巩固学习成果,复习新学的概念。

目录

文章目录

  • 前言
  • 目录
  • 正文
    • 模型引入
    • 决策边界
    • 误差函数
    • 多分类问题
  • 作业答案

正文

本节学习内容主要为逻辑回归-分类。

模型引入

问题引入,收到一封邮件后,电脑如何自动判断将其归类为垃圾邮件,节约我们看邮件的时间。
例子,根据肿瘤尺寸对癌症的良性和恶性进行分类,假设计算的值》=0.5,则认为肿瘤是恶性的。
因为,我们想要0<y(x)<1,因此,我们选择了sigmoid函数作为映射函数,它的函数图像如图所示。
对于理论输出结果的解释,多少概率是这个结果。

决策边界

逻辑回归模型详解,对应于y=1时的原始x值,以及中间输出值z的大小。

决策边界,即是分类超平面,是模型空间里正负两类的分界线。
分类便捷不一定是条直线,对于非线性问题它也可能是一条曲线。

误差函数

为了选择一个合适的参数,我们需要一个合适的误差函数,而且这个误差函数是凸函数。

直观演示逻辑回归函数的误差函数1。
直观演示逻辑回归函数的误差函数2。
误差函数组合,最终形式。

## 梯度下降的实现流程
这个程序的优化算法

多分类问题

多分类的分类边界
多分类问题的实现方式,通过n个单分类器。

作业答案

ex2.m

%% Machine Learning Online Class - Exercise 2: Logistic Regression
%
%  Instructions
%  ------------
%
%  This file contains code that helps you get started on the logistic
%  regression exercise. You will need to complete the following functions
%  in this exericse:
%
%     sigmoid.m
%     costFunction.m
%     predict.m
%     costFunctionReg.m
%
%  For this exercise, you will not need to change any code in this file,
%  or any other files other than those mentioned above.
%%% Initialization
clear ; close all; clc%% Load Data
%  The first two columns contains the exam scores and the third column
%  contains the label.data = load('ex2data1.txt');
X = data(:, [1, 2]); y = data(:, 3);%% ==================== Part 1: Plotting ====================
%  We start the exercise by first plotting the data to understand the
%  the problem we are working with.fprintf(['Plotting data with + indicating (y = 1) examples and o ' ...'indicating (y = 0) examples.\n']);plotData(X, y);% Put some labels
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')% Specified in plot order
legend('Admitted', 'Not admitted')
hold off;fprintf('\nProgram paused. Press enter to continue.\n');
pause;%% ============ Part 2: Compute Cost and Gradient ============
%  In this part of the exercise, you will implement the cost and gradient
%  for logistic regression. You neeed to complete the code in
%  costFunction.m%  Setup the data matrix appropriately, and add ones for the intercept term
[m, n] = size(X);% Add intercept term to x and X_test
X = [ones(m, 1) X];% Initialize fitting parameters
initial_theta = zeros(n + 1, 1);% Compute and display initial cost and gradient
[cost, grad] = costFunction(initial_theta, X, y);fprintf('Cost at initial theta (zeros): %f\n', cost);
fprintf('Expected cost (approx): 0.693\n');
fprintf('Gradient at initial theta (zeros): \n');
fprintf(' %f \n', grad);
fprintf('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n');% Compute and display cost and gradient with non-zero theta
test_theta = [-24; 0.2; 0.2];
[cost, grad] = costFunction(test_theta, X, y);fprintf('\nCost at test theta: %f\n', cost);
fprintf('Expected cost (approx): 0.218\n');
fprintf('Gradient at test theta: \n');
fprintf(' %f \n', grad);
fprintf('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n');fprintf('\nProgram paused. Press enter to continue.\n');
pause;%% ============= Part 3: Optimizing using fminunc  =============
%  In this exercise, you will use a built-in function (fminunc) to find the
%  optimal parameters theta.%  Set options for fminunc
options = optimset('GradObj', 'on', 'MaxIter', 400);%  Run fminunc to obtain the optimal theta
%  This function will return theta and the cost
[theta, cost] = ...fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);% Print theta to screen
fprintf('Cost at theta found by fminunc: %f\n', cost);
fprintf('Expected cost (approx): 0.203\n');
fprintf('theta: \n');
fprintf(' %f \n', theta);
fprintf('Expected theta (approx):\n');
fprintf(' -25.161\n 0.206\n 0.201\n');% Plot Boundary
plotDecisionBoundary(theta, X, y);% Put some labels
hold on;
% Labels and Legend
xlabel('Exam 1 score')
ylabel('Exam 2 score')% Specified in plot order
legend('Admitted', 'Not admitted')
hold off;fprintf('\nProgram paused. Press enter to continue.\n');
pause;%% ============== Part 4: Predict and Accuracies ==============
%  After learning the parameters, you'll like to use it to predict the outcomes
%  on unseen data. In this part, you will use the logistic regression model
%  to predict the probability that a student with score 45 on exam 1 and
%  score 85 on exam 2 will be admitted.
%
%  Furthermore, you will compute the training and test set accuracies of
%  our model.
%
%  Your task is to complete the code in predict.m%  Predict probability for a student with score 45 on exam 1
%  and score 85 on exam 2 prob = sigmoid([1 45 85] * theta);
fprintf(['For a student with scores 45 and 85, we predict an admission ' ...'probability of %f\n'], prob);
fprintf('Expected value: 0.775 +/- 0.002\n\n');% Compute accuracy on our training set
p = predict(theta, X);fprintf('Train Accuracy: %f\n', mean(double(p == y)) * 100);
fprintf('Expected accuracy (approx): 89.0\n');
fprintf('\n');

sigmoid.m

function g = sigmoid(z)
%SIGMOID Compute sigmoid function
%   g = 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

costfunction.m

function [J, grad] = costFunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
%   J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
%   parameter for 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
%
% Note: grad should have the same dimensions as theta
%
error=0;
for i=1:m
error=error-y(i)*log(sigmoid(X(i,:)*theta))-(1-y(i))*log(1-sigmoid(X(i,:)*theta));
end
J=error/m;
for j=1:length(theta)factor=0;for i=1:mfactor=factor+(sigmoid(X(i,:)*theta)-y(i))*X(i,j);endgrad(j)=factor/m;
end% =============================================================end

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
%p= sigmoid(X*theta)>0.5;

吴恩达 coursera ML 第五课总结+作业答案相关推荐

  1. 吴恩达 coursera ML 第十七课总结+作业答案

    前言 吴恩达的课程堪称经典,有必要总结一下. 学以致用,以学促用,通过笔记总结,巩固学习成果,复习新学的概念. 目录 文章目录 前言 目录 正文 正文 文字字符识别问题 工作流程 工作流水线 文本检测 ...

  2. 吴恩达 coursera ML 第十三课总结+作业答案

    前言 吴恩达的课程堪称经典,有必要总结一下. 学以致用,以学促用,通过笔记总结,巩固学习成果,复习新学的概念. 目录 文章目录 前言 目录 正文 动机一数据压缩 动机二数据可视化 降维方法:PCA 数 ...

  3. 吴恩达 coursera ML 第十一课总结+作业答案

    前言 吴恩达的课程堪称经典,有必要总结一下. 学以致用,以学促用,通过笔记总结,巩固学习成果,复习新学的概念. 目录 文章目录 前言 目录 正文 大分割界限的启示 核函数二 使用svm解决问题的 正文 ...

  4. 吴恩达 coursera ML 第十课总结+作业答案

    前言 吴恩达的课程堪称经典,有必要总结一下. 学以致用,以学促用,通过笔记总结,巩固学习成果,复习新学的概念. 目录 文章目录 前言 目录 正文 误差分析 不均匀数据的评估方法 准确度和召回率的取舍 ...

  5. 吴恩达 coursera ML 第八课总结+作业答案

    前言 吴恩达的课程堪称经典,有必要总结一下. 学以致用,以学促用,通过笔记总结,巩固学习成果,复习新学的概念. 目录 文章目录 前言 目录 正文 反向传播算法 前向传播过程 反向传播算法图解 实现技巧 ...

  6. 吴恩达 coursera ML 第七课总结+作业答案

    前言 学以致用,以学促用,通过笔记总结,巩固学习成果,复习新学的概念. 目录 文章目录 前言 目录 正文 模型引入 神经网络 模型表示 模型表示2 例子和图示 例子与图示2 作业答案 正文 本节主要讨 ...

  7. 吴恩达 coursera ML 第六课总结+作业答案

    前言 学以致用,以学促用,通过笔记总结,巩固学习成果,复习新学的概念. 目录 文章目录 前言 目录 正文 问题引入 正则化 正则化逻辑回归 作业答案 正文 本节主要探讨过拟合以及如何使用l2正则化抑制 ...

  8. 吴恩达 coursera ML 第四课总结+作业答案

    前言 学以致用,以学促用,通过笔记总结,巩固学习成果,复习新学的概念. 目录 文章 文章目录 前言 文章 正文 模型引入 多元梯度下降 技巧1 特征压缩 梯度下降2:学习率 多项式回归 补充内容 作业 ...

  9. 吴恩达 coursera AI 第四课总结+作业答案

    前言 吴恩达的课程堪称经典,有必要总结一下. 学以致用,以学促用,通过笔记总结,巩固学习成果,复习新学的概念. 目录 文章目录 前言 目录 正文 正文 深度神经网络 n层神经网络的信息流图. 深度网络 ...

最新文章

  1. Windows Xp Oracle 10g的安装
  2. 06-机器学习(Haar+Adaboost实现人脸、人眼检测)
  3. 学成在线--5.CMS页面管理开发(修改页面)
  4. MySQL 聚合函数 和 分组查询(初级)
  5. iOS 如何在一个应用程序中调用另一个应用程序
  6. Javascript数据类型检测
  7. YOLOv5数据集标注工具
  8. 判断conime.exe是不是病毒!
  9. springboot+vue+elementUI 公司财务固定资产管理系统#毕业设计
  10. 产品商业需求文档_【器】我的产品需求文档心法
  11. 优思学院|注册六西格玛绿带考试试题及答案详解
  12. 完全平方公式用c语言表达式,完全平方公式教案
  13. 第三方qq登录(获取头像和昵称)
  14. 美国AMC数学竞赛的含金量如何?
  15. arcgis api for js地图符号的使用(使用Draw绘制图形)
  16. 安利10个让你爽到爆的IDEA必备插件
  17. php主板主要是支持,主板影响电脑什么性能?
  18. SegNet: A Deep Convolutional Encoder-Decoder Architecture for Image Segmentation
  19. Python图片查找轮廓、多边形拟合、最小外接矩形操作实例
  20. imageReady 图片预加载

热门文章

  1. pond and pool
  2. 浅谈PHP的Public、Protected、Private三种方法的区别
  3. JVM技术周报第2期
  4. WebGIS--ArcGIS系列开发五:SDE for Oracle
  5. leetcode 264. Ugly Number II
  6. android: 播放音频
  7. 大型网站技术架构文摘
  8. 25美元 Linux PC 'Raspberry Pi' 一月上市
  9. Windows Forms DataGridView 中合并单元格
  10. bzoj4551: [Tjoi2016Heoi2016]树(树链剖分)