吴恩达机器学习练习2——Logistic回归

Logistic回归

代价函数

Logistic回归是分类算法,它的输出值在0和1之间。
h(x)的作用是,对于给定的输入变量,根据选择的参数计算输出变量等于1的可能性(estimated probablity)即h(x)=P(y=1|x;θ)

梯度下降

练习2

数据集

x1,x2 : score on two exams
y : admissions decision  

可视化数据集

function plotData(X, y)
	figure; hold on;
	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);
	hold off;
end
find()函数的基本功能是返回向量或者矩阵中不为0的元素的位置索引。
如果需要找到其中满足一定条件的元素,find(X==4)找到矩阵X中值为4的数据的索引

在这里插入图片描述

sigmoid函数

function g = sigmoid(z)
	g = zeros(size(z));
	g = 1./(1+exp.^(-z));
end

代价函数及梯度下降

function [J, grad] = costFunction(theta, X, y)
	m = length(y); 
	J = 0;
	grad = zeros(size(theta));
	h = sigmoid(X*theta);
	J = (1/m)*sum((-y)*log(h)-(1-y)*log(1-h));
	for j = 1 : size(X,2)
		grad(j) = (1/m)*sum((h-y).*X(:,j));
end

在这里插入图片描述

使用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);

评估

function p = predict(theta, X)
	m = size(X, 1); 
	p = zeros(m, 1);
	p = sigmoid(X*theta)>=0.5;
end

matlab之find()函数
matlab之sum()函数

猜你喜欢

转载自blog.csdn.net/cherry1307/article/details/83214733
今日推荐