Skip to content

Commit 8f1dddb

Browse files
authored
Merge pull request #29 from anesh612/master
Added Logistic Regression
2 parents cfbc905 + b7eeae5 commit 8f1dddb

File tree

7 files changed

+293
-0
lines changed

7 files changed

+293
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function P = Predict(Theta, X)
2+
3+
n = size(X, 1);
4+
5+
P = zeros(n, 1);
6+
7+
for i = 1 : n,
8+
9+
if Sigmoid(X * Theta)(i) >= 0.5
10+
11+
P(i) = 1;
12+
13+
end
14+
15+
end
16+
17+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function G = Sigmoid(Z)
2+
3+
G = zeros(size(Z));
4+
5+
a = size(Z);
6+
7+
for i = 1 : a(1, 1),
8+
9+
for j = 1 : a(1, 2),
10+
11+
G(i, j) = 1 / (1 + e.^-Z(i, j));
12+
13+
end
14+
15+
end
16+
17+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function [j, Grad] = costfunction(Theta, X, Y)
2+
3+
n = length(Y);
4+
5+
j = 0;
6+
7+
j = (1/n)*(-Y'* log(Sigmoid(X * Theta)) - (1 - Y)'* log(1 - Sigmoid(X * Theta))); % See the sigmoid function.
8+
9+
Grad = zeros(size(Theta));
10+
11+
Grad_ = (1 / n) * ((Sigmoid(X * Theta) - Y)' * X);
12+
13+
Grad = Grad_';
14+
15+
end
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
34.62365962451697,78.0246928153624,0
2+
30.28671076822607,43.89499752400101,0
3+
35.84740876993872,72.90219802708364,0
4+
60.18259938620976,86.30855209546826,1
5+
79.0327360507101,75.3443764369103,1
6+
45.08327747668339,56.3163717815305,0
7+
61.10666453684766,96.51142588489624,1
8+
75.02474556738889,46.55401354116538,1
9+
76.09878670226257,87.42056971926803,1
10+
84.43281996120035,43.53339331072109,1
11+
95.86155507093572,38.22527805795094,0
12+
75.01365838958247,30.60326323428011,0
13+
82.30705337399482,76.48196330235604,1
14+
69.36458875970939,97.71869196188608,1
15+
39.53833914367223,76.03681085115882,0
16+
53.9710521485623,89.20735013750205,1
17+
69.07014406283025,52.74046973016765,1
18+
67.94685547711617,46.67857410673128,0
19+
70.66150955499435,92.92713789364831,1
20+
76.97878372747498,47.57596364975532,1
21+
67.37202754570876,42.83843832029179,0
22+
89.67677575072079,65.79936592745237,1
23+
50.534788289883,48.85581152764205,0
24+
34.21206097786789,44.20952859866288,0
25+
77.9240914545704,68.9723599933059,1
26+
62.27101367004632,69.95445795447587,1
27+
80.1901807509566,44.82162893218353,1
28+
93.114388797442,38.80067033713209,0
29+
61.83020602312595,50.25610789244621,0
30+
38.78580379679423,64.99568095539578,0
31+
61.379289447425,72.80788731317097,1
32+
85.40451939411645,57.05198397627122,1
33+
52.10797973193984,63.12762376881715,0
34+
52.04540476831827,69.43286012045222,1
35+
40.23689373545111,71.16774802184875,0
36+
54.63510555424817,52.21388588061123,0
37+
33.91550010906887,98.86943574220611,0
38+
64.17698887494485,80.90806058670817,1
39+
74.78925295941542,41.57341522824434,0
40+
34.1836400264419,75.2377203360134,0
41+
83.90239366249155,56.30804621605327,1
42+
51.54772026906181,46.85629026349976,0
43+
94.44336776917852,65.56892160559052,1
44+
82.36875375713919,40.61825515970618,0
45+
51.04775177128865,45.82270145776001,0
46+
62.22267576120188,52.06099194836679,0
47+
77.19303492601364,70.45820000180959,1
48+
97.77159928000232,86.7278223300282,1
49+
62.07306379667647,96.76882412413983,1
50+
91.56497449807442,88.69629254546599,1
51+
79.94481794066932,74.16311935043758,1
52+
99.2725269292572,60.99903099844988,1
53+
90.54671411399852,43.39060180650027,1
54+
34.52451385320009,60.39634245837173,0
55+
50.2864961189907,49.80453881323059,0
56+
49.58667721632031,59.80895099453265,0
57+
97.64563396007767,68.86157272420604,1
58+
32.57720016809309,95.59854761387875,0
59+
74.24869136721598,69.82457122657193,1
60+
71.79646205863379,78.45356224515052,1
61+
75.3956114656803,85.75993667331619,1
62+
35.28611281526193,47.02051394723416,0
63+
56.25381749711624,39.26147251058019,0
64+
30.05882244669796,49.59297386723685,0
65+
44.66826172480893,66.45008614558913,0
66+
66.56089447242954,41.09209807936973,0
67+
40.45755098375164,97.53518548909936,1
68+
49.07256321908844,51.88321182073966,0
69+
80.27957401466998,92.11606081344084,1
70+
66.74671856944039,60.99139402740988,1
71+
32.72283304060323,43.30717306430063,0
72+
64.0393204150601,78.03168802018232,1
73+
72.34649422579923,96.22759296761404,1
74+
60.45788573918959,73.09499809758037,1
75+
58.84095621726802,75.85844831279042,1
76+
99.82785779692128,72.36925193383885,1
77+
47.26426910848174,88.47586499559782,1
78+
50.45815980285988,75.80985952982456,1
79+
60.45555629271532,42.50840943572217,0
80+
82.22666157785568,42.71987853716458,0
81+
88.9138964166533,69.80378889835472,1
82+
94.83450672430196,45.69430680250754,1
83+
67.31925746917527,66.58935317747915,1
84+
57.23870631569862,59.51428198012956,1
85+
80.36675600171273,90.96014789746954,1
86+
68.46852178591112,85.59430710452014,1
87+
42.0754545384731,78.84478600148043,0
88+
75.47770200533905,90.42453899753964,1
89+
78.63542434898018,96.64742716885644,1
90+
52.34800398794107,60.76950525602592,0
91+
94.09433112516793,77.15910509073893,1
92+
90.44855097096364,87.50879176484702,1
93+
55.48216114069585,35.57070347228866,0
94+
74.49269241843041,84.84513684930135,1
95+
89.84580670720979,45.35828361091658,1
96+
83.48916274498238,48.38028579728175,1
97+
42.2617008099817,87.10385094025457,1
98+
99.31500880510394,68.77540947206617,1
99+
55.34001756003703,64.9319380069486,1
100+
74.77589300092767,89.52981289513276,1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function plotdata(x, Y)
2+
3+
figure;
4+
5+
hold on;
6+
7+
Pos = find(Y == 1);
8+
9+
Neg = find(Y == 0);
10+
11+
plot(x(Pos, 1), x(Pos, 2), 'k+','LineWidth', 2, ...
12+
'MarkerSize', 7);
13+
14+
plot(x(Neg, 1), x(Neg, 2), 'ko', 'MarkerFaceColor', 'y', ...
15+
'MarkerSize', 7);
16+
17+
hold off;
18+
19+
end
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function plotdecisionboundary(Theta, X, Y)
2+
3+
plotdata(X(:,2:3), Y);
4+
5+
hold on;
6+
7+
if size(X, 2) <= 3
8+
9+
plot_X = [min(X(:,2))-2, max(X(:,2))+2];
10+
11+
plot_Y = (-1./Theta(3)).*(Theta(2).*plot_X + Theta(1));
12+
13+
plot(plot_X, plot_Y);
14+
15+
legend('Admitted', 'Not admitted', 'Decision Boundary');
16+
17+
axis([30, 100, 30, 100]);
18+
19+
else
20+
21+
U = linspace(-1, 1.5, 50);
22+
23+
V = linspace(-1, 1.5, 50);
24+
25+
Z = zeros(length(U), length(V));
26+
27+
for i = 1:length(U)
28+
29+
for j = 1:length(V)
30+
31+
Z(i,j) = mapFeature(U(i), V(j))*Theta;
32+
33+
end
34+
35+
end
36+
37+
Z = Z';
38+
39+
end
40+
41+
hold off;
42+
43+
end
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
clear; close all; clc;
2+
3+
Data = load('data.txt');
4+
x = Data(:, [1, 2]); Y = Data(:, 3);
5+
6+
fprintf(['Plotting data with + indicating (Y = 1) examples and o ' ...
7+
'indicating (Y = 0) examples.\n']);
8+
9+
plotdata(x, Y);
10+
11+
hold on;
12+
13+
xlabel('Exam 1 score');
14+
ylabel('Exam 2 score');
15+
16+
legend('Admitted', 'Not admitted');
17+
hold off;
18+
19+
fprintf('\nProgram paused, press enter to continue.\n');
20+
pause;
21+
22+
[m, n] = size(x);
23+
24+
X = [ones(m, 1) x];
25+
26+
Initial_Theta = zeros(n + 1, 1);
27+
28+
[Cost, Grad] = costfunction(Initial_Theta, X, Y);
29+
30+
fprintf('Cost at initial theta (zeros): %f\n', Cost);
31+
fprintf('Expected cost (approx): 0.693\n');
32+
fprintf('Gradient at initial theta (zeros): \n');
33+
fprintf(' %f \n', Grad);
34+
fprintf('Expected gradients (approx):\n -0.1000\n -12.0092\n -11.2628\n');
35+
36+
Test_Theta = [-24; 0.2; 0.2];
37+
[Cost, Grad] = costfunction(Test_Theta, X, Y);
38+
39+
fprintf('\nCost at test theta: %f\n', Cost);
40+
fprintf('Expected cost (approx): 0.218\n');
41+
fprintf('Gradient at test theta: \n');
42+
fprintf(' %f \n', Grad);
43+
fprintf('Expected gradients (approx):\n 0.043\n 2.566\n 2.647\n');
44+
45+
fprintf('\nProgram paused, press enter to continue.\n');
46+
pause;
47+
48+
Options = optimset('GradObj', 'on', 'MaxIter', 400);
49+
50+
[Theta, Cost] = ...
51+
fminunc(@(t)(costfunction(t, X, Y)), Initial_Theta, Options);
52+
53+
fprintf('Cost at theta found by fminunc: %f\n', Cost);
54+
fprintf('Expected cost (approx): 0.203\n');
55+
fprintf('theta: \n');
56+
fprintf(' %f \n', Theta);
57+
fprintf('Expected theta (approx):\n');
58+
fprintf(' -25.161\n 0.206\n 0.201\n');
59+
60+
plotdecisionboundary(Theta, X, Y);
61+
62+
hold on;
63+
64+
xlabel('Exam 1 score');
65+
ylabel('Exam 2 score');
66+
67+
legend('Admitted', 'Not admitted');
68+
hold off;
69+
70+
fprintf('\nProgram paused, press enter to continue.\n');
71+
pause;
72+
73+
Prob = Sigmoid([1 45 85] * Theta);
74+
fprintf(['For a student with scores 45 and 85, we predict an admission ' ...
75+
'probability of %f\n'], Prob);
76+
fprintf('Expected value: 0.775 +/- 0.002\n\n');
77+
78+
P = Predict(Theta, X);
79+
80+
fprintf('Train accuracy: %f\n', mean(double(P == Y)) * 100);
81+
fprintf('Expected accuracy (approx): 89.0\n');
82+
fprintf('\n');

0 commit comments

Comments
 (0)