一、Logistic回归——分类

对于分类问题,采用线性回归是不合理的。

1.假设函数(logistic函数/Sigmoid函数):

注:假设函数 h 的值,看作结果为y=1的概率估计。决策界限可以看作是 h=0.5 的线。

2.代价函数

3.高级优化 fminunc

在上文优化过程中需要提供α值,而高级优化α是自动选择。

优化结果

二、Logistic回归——多元分类(一对多种类别)

三、编程作业

1.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

2.plotDate.m 数据可视化

function plotData(X, y)
%PLOTDATA Plots the data points X and y into a new figure
%   PLOTDATA(x,y) plots the data points with + for the positive examples
%   and o for the negative examples. X is assumed to be a Mx2 matrix.% Create New Figure
figure; hold on;% ====================== YOUR CODE HERE ======================
% Instructions: Plot the positive and negative examples on a
%               2D plot, using the option 'k+' for the positive
%               examples and 'ko' for the negative examples.
%
axis([30 100 30 100]);
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);

3.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
%h =sigmoid(X*theta);
costfun = y.*log(h)+(1-y).*log(1-h);
J = -1/m*sum(costfun);
grad = X'*(h-y)/m;% =============================================================end

4.fminunc高级优化

命令行:

% 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);  

5.predict.m

对每个样本预测分类结果(根据假设函数),将分类结果存到向量 v 中,与实际的分类结果 y 比较,得到正确率。

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

转载于:https://www.cnblogs.com/sunxiaoshu/p/10557726.html

监督学习-逻辑回归及编程作业(一)相关推荐

  1. EduCoder 机器学习 逻辑回归

    逻辑回归是属于机器学习里面的监督学习,它是以回归的思想来解决分类问题的一种非常经典的二分类分类器.由于其训练后的参数有较强的可解释性,在诸多领域中,逻辑回归通常用作 baseline 模型,以方便后期 ...

  2. stanford coursera 机器学习编程作业 exercise 3(逻辑回归实现多分类问题)

    本作业使用逻辑回归(logistic regression)和神经网络(neural networks)识别手写的阿拉伯数字(0-9) 关于逻辑回归的一个编程练习,可参考:http://www.cnb ...

  3. 吴恩达《神经网络和深度学习》第二周编程作业—用神经网络思想实现逻辑回归

    吴恩达<神经网络和深度学习>-用神经网络思想实现逻辑回归 1 安装包 2 问题概述 3 学习算法的一般架构 4 构建算法的各个部分 4.1 激活函数 4.2 初始化参数 4.3 前向和后向 ...

  4. python 线性回归与逻辑回归区别(有监督学习【分类、回归】、无监督学习【聚类、强化学习】、损失函数、梯度下降、学习率、过拟合、欠拟合、正则化)

    引用文章1 https://blog.csdn.net/viewcode/article/details/8794401 引用文章2:一.线性回归和逻辑回归 一.什么是机器学习 利用大量的数据样本,使 ...

  5. 监督分类空白处也被分类了_监督学习(2)|本质是分类的“逻辑回归”

    引言 机器学习,绕不开预测问题,预测绕不开回归和分类.本篇介绍最常用的二分类算法:逻辑回归(Logistics Regression),当然随着算法的发展,它也可用于多分类问题.每一个算法都是许许多多 ...

  6. 吴恩达作业1:逻辑回归实现猫的分类

    思路:输入样本X与随机初始权重W相乘,利用sigmoid激活函数输出值,对于二分类问题,用交叉熵损失函数来计算损失值,通过交叉熵损失函数利用链式法则求出W和b的偏导,梯度下降更新W和b即可,(梯度下降 ...

  7. 机器学习速成课程 | 练习 | Google Development——编程练习:逻辑回归

    逻辑回归 学习目标: 将(在之前的练习中构建的)房屋价值中位数预测模型重新构建为二元分类模型 比较逻辑回归与线性回归解决二元分类问题的有效性 与在之前的练习中一样,我们将使用加利福尼亚州住房数据集,但 ...

  8. 吴恩达机器学习作业二——逻辑回归

    有了作业一的铺垫,作业二的代码更容易理解了. 逻辑回归 题目描述:在训练的初始阶段,我们将要构建一个逻辑回归模型来预测,某个学生是否被大学录取.设想你是大学相关部分的管理者,想通过申请学生两次测试的评 ...

  9. 吴恩达机器学习 逻辑回归 作业3(手写数字分类) Python实现 代码详细解释

    整个项目的github:https://github.com/RobinLuoNanjing/MachineLearning_Ng_Python 里面可以下载进行代码实现的数据集 题目介绍: In t ...

最新文章

  1. 解决非controller使用@Autowired注解注入报错为java.lang.NullPointerException问题
  2. OpenCV图像或视频显示在VC对话框中的方法
  3. debugging Auto Layout:Logical Errors
  4. 最全的CSS浏览器兼容问题(转至http://68design.net/Web-Guide/HTMLCSS/37154-1.html)
  5. CentOS部署Harbor镜像仓库(1),java技术栈自我理解面试题通俗解说
  6. python最新技术开锁工具_Python 自动化库介绍 PySimpleGUI
  7. Matlab均值滤波去噪
  8. 扩展欧几里得算法求逆元c语言,利用扩展欧几里得算法编程求逆元
  9. 计算机网络考研408计算机134分笔记!超级详细!23最新考纲
  10. ubuntu 安装 mujoco-py
  11. TCP/IP网络编程 学习笔记_9 --域名系统(DNS)
  12. web安全之SQL注入(三)
  13. QQ群怎么快速封群,如何举报骗子QQ群可以使之封群?
  14. 计算机在中医学有哪些最新应用,计算机与中医学的现代应用
  15. rocketMQ报错:No accessKey is configured
  16. 好女人是一所学校- -
  17. 云讯健身管理系统-9
  18. Html中锚点的使用
  19. android手势控制动画,轻松实现Android,iOS的一个手势动画效果
  20. android生成apk流程,Android Studio生成APK的基本流程

热门文章

  1. debian7更换gcc版本的二种方法分享
  2. please wait while windows configures microsoft visual studio professional 2013
  3. LeetCode 解题报告索引
  4. 使用GPS实时记录运动路线
  5. CMMI过程域-MA度量和分析
  6. FFMPEG命令行处理视频进阶——高阶必读
  7. 漫谈词向量之基于Softmax与Sampling的方法
  8. 高级驾驶辅助系统ADAS
  9. Bilateral Filtering(双边滤波) for SSAO
  10. Socket通信总结(附C++实现)