weixin_36382721 2016-10-12 19:07 采纳率: 0%
浏览 2305

matlab实现牛顿法逻辑回归

矢量形式的知道怎么写

clear all; close all; clc

x = load('ex4x.dat');
y = load('ex4y.dat');

[m, n] = size(x);

% Add intercept term to x
x = [ones(m, 1), x];

% Plot the training data
% Use different markers for positives and negatives
figure
pos = find(y); neg = find(y == 0);
plot(x(pos, 2), x(pos,3), '+')
hold on
plot(x(neg, 2), x(neg, 3), 'o')
hold on
xlabel('Exam 1 score')
ylabel('Exam 2 score')

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

% Define the sigmoid function
g = inline('1.0 ./ (1.0 + exp(-z))');

% Newton's method
MAX_ITR = 10;
J = zeros(MAX_ITR, 1);

for i = 1:MAX_ITR
% Calculate the hypothesis function
z = x * theta;
h = g(z);

grad = (1/m).*x' * (h-y);
H = (1/m).*x' * diag(h) * diag(1-h) * x;

% Calculate J (for testing convergence)
J(i) =(1/m)*sum(-y.*log(h) - (1-y).*log(1-h));

theta = theta - H\grad;

end
% Display theta
theta

% Calculate the probability that a student with
% Score 20 on exam 1 and score 80 on exam 2
% will not be admitted
prob = 1 - g([1, 20, 80]*theta)

% Plot Newton's method result
% Only need 2 points to define a line, so choose two endpoints
plot_x = [min(x(:,2)+2), max(x(:,2))+2];
% Calculate the decision boundary line
plot_y = (-1./theta(3)).*(theta(2).*plot_x +theta(1));
plot(plot_x, plot_y)
legend('Admitted', 'Not admitted', 'Decision Boundary')
hold off

% Plot J
figure
plot(0:MAX_ITR-1, J, 'o--', 'MarkerFaceColor', 'r', 'MarkerSize', 8)
xlabel('Iteration'); ylabel('J-cost')
求助非矢量型的怎么写 自己大概写的如下 是错的 因为不知道H的位置和表达方式
to=cputime();
x = load('ex4x.dat');
y = load('ex4y.dat');
m=length(y);
x = [ones(m, 1), x];
pos = find(y); neg = find(y == 0);
plot(x(pos, 2), x(pos,3), '*')
hold on
plot(x(neg, 2), x(neg, 3), 'o')
hold on
xlabel('Exam 1 score')
ylabel('Exam 2 score')
numfeature=size(x,2);
numtrainsam = size(x,1);
theta = zeros(numfeature, 1);
maxiterations=10;
errorperiteration=zeros(maxiterations,1);
g = inline('1.0 ./ (1.0 + exp(-z))');
prevtheta=theta;
for t=1:maxiterations
toterror=0;
for j=1:numfeature
totslope=0;
for i=1:m
z=0;

for jj=1:numfeature

          z=z+prevtheta(jj)*x(i,jj);
       end
       h=1.0/(1.0+exp(-z));
       H=H+(x(i,q)*h*(1-h)*x(i,jj));  

       totslope=(totslope+(h-y(i))*x(i,j));
       toterror=(toterror+-y(i)*log(h)-(1-y(i))*log(1-h));          
    end         

toterror=toterror/numtrainsam;
theta(j)= theta(j)-H\totslope;
end
prevtheta=theta;
errorperiteration(t)=toterror/j;
end
errorperiteration=zeros(maxiterations,1)
prevtheta=theta;
plot_x = [min(x(:,2)+2), max(x(:,2))+2];
% Calculate the decision boundary line
plot_y = (-1./theta(3)).*(theta(2).*plot_x +theta(1));
plot(plot_x, plot_y)
legend('Admitted', 'Not admitted', 'Decision Boundary')
hold off

  • 写回答

1条回答 默认 最新

  • devmiao 2016-10-13 02:01
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序