Logistic Regression in MATLAB
p = 1./(1+exp(-1*(b(1)+b(2)*xvals))); gives us the probability of the xvals belonging to class 1
The output is between 0 and 1. It is usual to classify the input as Y = 0 for output lesser than 0.5 and Y = 1 for output greater than 0.5.
Code:
%%
clc
clear all
close all
%%
g=[22 0
25 0
47 1
52 0
56 1
55 0
60 1
62 1
61 1
28 0
27 0
29 0
49 1
55 1
25 1
58 1
19 0
21 0
26 0
40 1
45 1
50 1
54 1];
contr=g(:,1);
detection=g(:,2);
%%
b=glmfit(contr, detection, 'binomial');
%%
xvals=1:100; % x axis values
p = 1./(1+exp(-1*(b(1)+b(2)*xvals)));
%%
a=[];
y=detection';
for i=1:length(contr)
a=[a ; contr(i) 1];
end
c =a\y';
yR = c(1)*contr + c(2); % the fitted line
plot(contr,yR)
hold on
plot(contr,detection,'ro');
plot(xvals,p);
legend('Linear Regression','Original Dataset','Logistic Regression Model');
join us on Telegram: https://t.me/matlabirawen
Join us on Facebook Group: https://www.facebook.com/groups/matlabcodes
Code:
%%
clc
clear all
close all
%%
g=[22 0
25 0
47 1
52 0
56 1
55 0
60 1
62 1
61 1
28 0
27 0
29 0
49 1
55 1
25 1
58 1
19 0
21 0
26 0
40 1
45 1
50 1
54 1];
contr=g(:,1);
detection=g(:,2);
%%
b=glmfit(contr, detection, 'binomial');
%%
xvals=1:100; % x axis values
p = 1./(1+exp(-1*(b(1)+b(2)*xvals)));
%%
a=[];
y=detection';
for i=1:length(contr)
a=[a ; contr(i) 1];
end
c =a\y';
yR = c(1)*contr + c(2); % the fitted line
plot(contr,yR)
hold on
plot(contr,detection,'ro');
plot(xvals,p);
legend('Linear Regression','Original Dataset','Logistic Regression Model');
join us on Telegram: https://t.me/matlabirawen
Join us on Facebook Group: https://www.facebook.com/groups/matlabcodes
No comments