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