Professional Documents
Culture Documents
Logistic 1
Logistic 1
Logistic 1
1
% define initial seed
params0 = zeros(1,size(X,2));
% define bounds
paramslb = []; % [] means no bounds
paramsub = [];
% define cost function (negative-log-likelihood)
% here, the eps is a hack to ensure that the log returns a finite
number.
costfun = @(pp) -sum((y .* log(logisticfun(X*pp')+eps)) + ...
((1-y) .* log(1-logisticfun(X*pp')+eps)));
% fit the logistic regression model by finding
% parameter values that minimize the cost function
[params,resnorm,residual,exitflag,output] = ...
lsqnonlin(costfun,params0,paramslb,paramsub,options);
First-Order
Norm of
Iteration Func-count Residual optimality Lambda
step
0 4 1201.13 416 0.01
1 8 277.244 56 0.001
1.78489
2 12 148.623 27.9 0.0001
3.8386
3 22 132.374 12.2 100
0.320084
4 26 128.393 5.39 10
0.162361
5 31 127.375 3.18 100
0.0812274
6 35 127.017 2.88 10
0.046166
7 39 126.662 7.23 1
0.328918
8 45 125.814 3.97 100
0.0750915
9 49 125.552 2.49 10
0.0399111
10 53 125.487 4.43 1
0.271921
11 59 124.925 2.61 100
0.0594392
12 63 124.708 2.19 10
0.0360605
13 68 124.598 1.87 100
0.0249691
14 72 124.526 1.64 10
0.0197256
15 76 124.173 2.27 1
0.171948
2
16 82 124.075 1.55 100
0.0238948
17 86 124.017 1.14 10
0.0178019
18 90 123.814 1.9 1
0.148335
19 96 123.727 1.55 100
0.022521
20 100 123.678 1.3 10
0.0165903
21 104 123.576 2.11 1
0.133349
22 110 123.487 1.43 100
0.0229012
23 114 123.442 1.03 10
0.0160575
24 118 123.404 1.78 1
0.123001
25 124 123.319 1.39 100
0.0223498
26 128 123.276 1.12 10
0.0157401
27 133 123.251 0.934 100
0.0117664
28 137 123.235 0.808 10
0.00950573
29 141 123.157 0.986 1
0.0828874
30 147 123.133 0.726 100
0.0115103
31 151 123.119 0.561 10
0.00889812
32 155 123.069 1.01 1
0.074771
33 161 123.047 0.813 100
0.011244
34 165 123.034 0.68 10
0.00848042
35 169 123.005 0.961 1
0.0689362
36 175 122.983 0.691 100
0.0113644
37 179 122.971 0.518 10
0.00826277
38 183 122.957 0.951 1
0.0646201
39 189 122.935 0.74 100
0.0113329
40 193 122.923 0.598 10
0.00813652
41 197 122.922 0.96 1
0.0618885
42 203 122.899 0.677 100
0.0116673
3
43 207 122.887 0.494 10
0.0081493
44 212 122.881 0.375 100
0.00600049
45 216 122.877 0.297 10
0.00475153
46 220 122.86 0.542 1
0.0406697
47 226 122.854 0.44 100
0.00587151
48 230 122.85 0.371 10
0.00452326
49 234 122.84 0.483 1
0.037564
50 240 122.834 0.357 100
0.00586685
51 244 122.83 0.274 10
0.00437758
52 248 122.824 0.508 1
0.0351516
53 254 122.818 0.4 100
0.00585419
54 258 122.815 0.327 10
0.00428393
55 262 122.812 0.482 1
0.0334166
56 268 122.806 0.349 100
0.00595175
57 272 122.803 0.26 10
0.00425006
58 276 122.803 0.488 1
0.0321513
4
ax = axis;
xvals = linspace(ax(1),ax(2),100);
yvals = linspace(ax(3),ax(4),100);
[xx,yy] = meshgrid(xvals,yvals);
% construct regressor matrix
X = [xx(:) yy(:)];
X(:,end+1) = 1;
% evaluate model at the points (but don't perform the final
thresholding)
outputimage = reshape(logisticfun(X*params'),[length(yvals)
length(xvals)]);
% visualize the image (the default coordinate system for images
% is 1:N where N is the number of pixels along each dimension.
% we have to move the image to the proper position; we
% accomplish this by setting XData and YData.)
h3 = imagesc(outputimage,[0 1]); % the range of the logistic function
is 0 to 1
set(h3,'XData',xvals,'YData',yvals);
colormap(hot);
colorbar;
% visualize the decision boundary associated with the model
% by computing the 0.5-contour of the image
[c4,h4] = contour(xvals,yvals,outputimage,[.5 .5]);
set(h4,'LineWidth',3,'LineColor',[0 0 1]); % make the line thick and
blue
% send the image to the bottom so that we can see the data points
uistack(h3,'bottom');
% send the contour to the top
uistack(h4,'top');
% restore the original axis range
axis(ax);
% report the accuracy of the model in the title
title(sprintf('Classification accuracy is %.1f%%',pctcorrect));
5
Published with MATLAB® R2018b