sigmoid.m文件

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

g = zeros(size(z));  初始化g ,z可以是一个数,一个向量或者一个矩阵

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the sigmoid of each value of z (z can be a matrix, vector or scalar)
Ones = ones(size(z));
g = Ones./(Ones + exp((-1).*z));  计算,g(z)的值域在[0,1]之间,符合概率的分布.

当z=0时,g=0.5; 当z<0时,g<0.5;当z>0时,g>0.5;

当z->-∞时,g->0; 当z->+∞时,g->1

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));  %grad的维数与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
%

J(θ)的表达式      

grad的表达式

J = 1/m*(-1*y'*log(sigmoid(X*theta)) - (ones(1,m)-y')*log(ones(m,1)-sigmoid(X*theta)));    %logM是对矩阵的每个元素都是求log, exp(M)同样是表示对矩阵的每                                                                                                                                        个元素求e的底

调用的函数参见上述函数sigmoid.m

grad = 1/m * (X' * (sigmoid(X*theta) - y));,

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

end

%% ============ 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);  %求x矩阵的维数

% Add intercept term to x and X_test
X = [ones(m, 1) X];  %X矩阵左侧加一列1,用来匹配常数量

% Initialize fitting parameters
initial_theta = zeros(n + 1, 1);

% Compute and display initial cost and gradient
[cost, grad] = costFunction(initial_theta, X, y);   %参见上述文件costFunction.m

fprintf('Cost at initial theta (zeros): %f\n', cost);
fprintf('Gradient at initial theta (zeros): \n');
fprintf(' %f \n', grad);

fprintf('\nProgram paused. Press enter to continue.\n');
pause;

matlab(3) Logistic Regression: 求cost 和gradient \ 求sigmoid的值的更多相关文章

  1. matlab(4) Logistic regression:求θ的值使用fminunc / 画decision boundary(直线)plotDecisionBoundary

    画decision boundary(直线) %% ============= Part 3: Optimizing using fminunc =============% In this exer ...

  2. matlab(2) Logistic Regression: 画出样本数据点plotData

    画出data数据 data数据 34.62365962451697,78.0246928153624,030.28671076822607,43.89499752400101,035.84740876 ...

  3. SVM: 相对于logistic regression而言SVM的 cost function与hypothesis

    很多学习算法的性能都差不多,关键不是使用哪种学习算法,而是你能得到多少数据量和应用这些学习算法的技巧(如选择什么特征向量,如何选择正则化参数等) SVM在解决非线性问题上提供了强大的方法. logis ...

  4. Stanford机器学习---第三讲. 逻辑回归和过拟合问题的解决 logistic Regression & Regularization

    原文:http://blog.csdn.net/abcjennifer/article/details/7716281 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...

  5. 机器学习-- Logistic回归 Logistic Regression

    转载自:http://blog.csdn.net/linuxcumt/article/details/8572746 1.假设随Tumor Size变化,预测病人的肿瘤是恶性(malignant)还是 ...

  6. 编程作业2.2:Regularized Logistic regression

    题目 在本部分的练习中,您将使用正则化的Logistic回归模型来预测一个制造工厂的微芯片是否通过质量保证(QA),在QA过程中,每个芯片都会经过各种测试来保证它可以正常运行.假设你是这个工厂的产品经 ...

  7. (原创)Stanford Machine Learning (by Andrew NG) --- (week 3) Logistic Regression & Regularization

    coursera上面Andrew NG的Machine learning课程地址为:https://www.coursera.org/course/ml 我曾经使用Logistic Regressio ...

  8. Logistic Regression 笔记与理解

    Logistic Regression 笔记与理解 Logistic Regression Hypothesis 记为 H(theta) H(theta)=g(z) 当中g(z),是一个叫做Logis ...

  9. machine learning(10) -- classification:logistic regression cost function 和 使用 gradient descent to minimize cost function

    logistic regression cost function(single example) 图像分布 logistic regression cost function(m examples) ...

随机推荐

  1. flask 编码问题

    在我们的flask项目中,通过表单提交对数据库进行更新的时候,数据提交不成功,提示以下内容: sqlalchemy.exc.InternalError: (pymysql.err.InternalEr ...

  2. [bzoj4345][POI2016]Korale_堆_贪心_线段树_dfs

    bzoj4345 POI2016 Korale 题目链接:https://lydsy.com/JudgeOnline/problem.php?id=4345 数据范围:略. 题解: 由于$k$的范围问 ...

  3. LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

    145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...

  4. 剑指offer44:翻转单词顺序列

    1 题目描述 牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思.例如,“stude ...

  5. Python-19-元类

    一.定义 元类就是类的类 type是python的一个内建元类 类的两种定义方式 # 定义类的两种方式 class Foo(object): def __init__(self, name, age) ...

  6. 机器学习-EM算法-GMM模型笔记

    GMM即高斯混合模型,下面根据EM模型从理论公式推导GMM: 随机变量X是有K个高斯分布混合而成,取各个高斯分布的概率为φ1,φ2,... ,φK,第i个高斯分布的均值为μi,方差为Σi.若观测到随机 ...

  7. (转) 嵌入式 Linux 利用 udev 实现自动检测挂载U盘

    本文链接:https://blog.csdn.net/cfl927096306/article/details/95180940 udev配置文件是/etc/udev/udev.conf,也许长这样: ...

  8. SringMVC笔记

    SpringMvc主要是三个Servlet:HttpServletBean,FramwworkServlet,DispatcherServlet,它们是依次继承的关系,其处理过程大致功能如下: 1.H ...

  9. VBA数组(十四)

    我们都知道,一个变量是一个存储值的容器. 有时,开发人员希望一次可以在一个变量中保存多个值. 当一系列值存储在单个变量中时,则称为数组变量. 数组声明 数组声明的方式与声明变量相同,只是数组变量的声明 ...

  10. mysql中常用的数据类型

    MySQL是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性.MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语 ...