1. Sigmoid function

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. Logistic Regression Cost &  Logistic Regression Gradient

首先可以将h(x)表示出来----sigmoid函数

然后对于gredient(j)来说,

可以现在草稿纸上把矩阵画出来,然后观察,用向量来解决;

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); for i=1:m,
J=J+1/m*(-y(i)*log(h(i))-(1-y(i))*log(1-h(i)));
endfor grad=1/m*X'*(h.-y); % ============================================================= end

3. Predict

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);
for i=1:m
if(p(i)>=0.5)p(i)=1;
else p(i)=0;
end
endfor % ========================================================================= end

4.Regularized Logistic Regression Cost & Regularized Logistic Regression Gradient

要注意的是:

Octave中,下标是从1开始的;

其次:

对于gradient(j)而言;

我们可以用X(:,j)的方式获取第j列的所有元素;

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 h=sigmoid(X*theta); for i=1:m
J=J+1/m*(-y(i)*log(h(i))-(1-y(i))*log(1-h(i)));
endfor for i=2:length(theta)
J=J+lambda/(2*m)*theta(i)^2;
endfor grad(1)=1/m*(h-y)'*X(:,1);
for i=2:length(theta)
grad(i)=1/m*(h-y)'*X(:,i)+lambda/m*theta(i);
endfor % ============================================================= end

Machine learning吴恩达第三周 Logistic Regression的更多相关文章

  1. Machine Learning——吴恩达机器学习笔记(酷

    [1] ML Introduction a. supervised learning & unsupervised learning 监督学习:从给定的训练数据集中学习出一个函数(模型参数), ...

  2. Machine learning吴恩达第二周coding作业(选做)

    1.Feature Normalization: 归一化的处理 function [X_norm, mu, sigma] = featureNormalize(X) %FEATURENORMALIZE ...

  3. Machine learning 吴恩达第二周coding作业(必做题)

    1.warmUpExercise: function A = warmUpExercise() %WARMUPEXERCISE Example function in octave % A = WAR ...

  4. 吴恩达+neural-networks-deep-learning+第二周作业

    Logistic Regression with a Neural Network mindset v4 简单用logistic实现了猫的识别,logistic可以被看做一个简单的神经网络结构,下面是 ...

  5. Deap Learning (吴恩达) 第一章深度学习概论 学习笔记

    Deap Learning(Ng) 学习笔记 author: 相忠良(Zhong-Liang Xiang) start from: Sep. 8st, 2017 1 深度学习概论 打字太麻烦了,索性在 ...

  6. 吴恩达机器学习笔记 —— 7 Logistic回归

    http://www.cnblogs.com/xing901022/p/9332529.html 本章主要讲解了逻辑回归相关的问题,比如什么是分类?逻辑回归如何定义损失函数?逻辑回归如何求最优解?如何 ...

  7. Github | 吴恩达新书《Machine Learning Yearning》完整中文版开源

    最近开源了周志华老师的西瓜书<机器学习>纯手推笔记: 博士笔记 | 周志华<机器学习>手推笔记第一章思维导图 [博士笔记 | 周志华<机器学习>手推笔记第二章&qu ...

  8. 我在 B 站学机器学习(Machine Learning)- 吴恩达(Andrew Ng)【中英双语】

    我在 B 站学机器学习(Machine Learning)- 吴恩达(Andrew Ng)[中英双语] 视频地址:https://www.bilibili.com/video/av9912938/ t ...

  9. Coursera课程《Machine Learning》吴恩达课堂笔记

    强烈安利吴恩达老师的<Machine Learning>课程,讲得非常好懂,基本上算是无基础就可以学习的课程. 课程地址 强烈建议在线学习,而不是把视频下载下来看.视频中间可能会有一些问题 ...

随机推荐

  1. DWR 3.0 入门示例教程

    DWR(Direct Web Remoting) DWR is a Java library that enables Java on the server and JavaScript in a b ...

  2. linux的mysql操作

    最近工作中经常需要使用到MySQL,有时候在WINXP,有时候在Linux中,而这次,需要在CentOS中配置一下,还需要用到phpmyadmin, 在网上搜了不少的资料. 无意中还找到了CentOS ...

  3. maven环境快速搭建(转)

    出处:http://www.cnblogs.com/fnng/archive/2011/12/02/2272610.html 最近,开发中要用到maven,所以对maven进行了简单的学习.因为有个m ...

  4. springboot+swagger集成

    一.swagger介绍 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一些常见问题.如果想深入分析项目源码,了解更多内容 ...

  5. 从零开始学习前端JAVASCRIPT — 11、JavaScript运动模型及轮播图效果、放大镜效果、自适应瀑布流

    未完待续...... 一.运动原理 通过连续不断的改变物体的位置,而发生移动变化. 使用setInterval实现. 匀速运动:速度值一直保持不变. 多物体同时运动:将定时器绑设置为对象的一个属性. ...

  6. Discuz核心函数的解析

    dz采用的是多入口的方式,在每个入口函数你能看到引用,启动核心类的语句(其余省略),如下: require './source/class/class_core.php'; C::app()-> ...

  7. 编写高质量代码改善C#程序的157个建议——建议94:区别对待override和new

    建议94:区别对待override和new override和new使类型体系应为继承而呈现出多态性.多态要求子类具有与基类同名的方法,override和new的作用就是: 如果子类中的方法前面带有n ...

  8. Reporting Service服务SharePoint集成模式安装配置(3、4、安装sharepoint 2010必备组件及产品)

    Reporting Service服务SharePoint集成模式安装配置 第三步和第四部 第三步 安装sharepoint 2010必备组件 1.安装SharePoint2010必备组件,执行Pre ...

  9. MyBatis 一级缓存避坑

    MyBatis 一级缓存(MyBaits 称其为 Local Cache)无法关闭,但是有两种级别可选: package org.apache.ibatis.session; /** * @autho ...

  10. asp.net—— 基础之截取字符串

    在实际开发中有时难免会遇到需要获取某个字符串中的某些字符串,这里我们可以用到字符串截取的办法. 截取字符串的方法很容易(暂不包含中文字符串),只要稍微有点.net基础的人看了都能看懂. /// < ...