close all,clear,clc
x = load('ex4x.dat');
y = load('ex4y.dat');
% find returns the indices of the
% rows meeting the specified condition
pos = find(y == 1); %得到被录取学生的下标序列
neg = find(y == 0); %得到没有录取学生的下标序列
% Assume the features are in the 2nd and 3rd
% columns of x
m = length(y); %样本个数
x=[ones(m,1),x];
plot(x(pos, 2), x(pos,3), '+'); %x1作为横坐标;x2作为纵坐标
hold on
plot(x(neg, 2), x(neg, 3), 'o')
ylabel('Exam 2 score');
xlabel('Exam 1 score');
legend('Admitted','Not admitted')
theta0=0;
theta1=0;
theta2=0;
theta=[theta0;theta0;theta0];
iter = 15;
J = [];
for t=1:iter
grad = [0,0,0]';
H =zeros(3);
tmp = 0;
for i=1:m
fun = 1/(1+exp(-(theta'*x(i,:)'))) ; %Logistic函数
grad = grad + (1/m)*(fun-y(i,:))*x(i,:)'; %牛顿迭代中的梯度
H = H + (1/m)*fun*(1-fun)*x(i,:)'*x(i,:); %hession
tmp = tmp + (1/m)*(-y(i,:)*log(fun) - (1-y(i,:))*log(1-fun))close all,clear,clc
x = load('ex4x.dat');
y =