-
Notifications
You must be signed in to change notification settings - Fork 1
/
5.1 Logistic Regression.html
605 lines (534 loc) · 32.1 KB
/
5.1 Logistic Regression.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>Logistic Regression — Data Science Notes</title>
<link href="_static/css/theme.css" rel="stylesheet">
<link href="_static/css/index.ff1ffe594081f20da1ef19478df9384b.css" rel="stylesheet">
<link rel="stylesheet"
href="_static/vendor/fontawesome/5.13.0/css/all.min.css">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="_static/vendor/fontawesome/5.13.0/webfonts/fa-solid-900.woff2">
<link rel="preload" as="font" type="font/woff2" crossorigin
href="_static/vendor/fontawesome/5.13.0/webfonts/fa-brands-400.woff2">
<link rel="stylesheet" type="text/css" href="_static/pygments.css" />
<link rel="stylesheet" type="text/css" href="_static/sphinx-book-theme.css?digest=c3fdc42140077d1ad13ad2f1588a4309" />
<link rel="stylesheet" type="text/css" href="_static/togglebutton.css" />
<link rel="stylesheet" type="text/css" href="_static/copybutton.css" />
<link rel="stylesheet" type="text/css" href="_static/mystnb.css" />
<link rel="stylesheet" type="text/css" href="_static/sphinx-thebe.css" />
<link rel="stylesheet" type="text/css" href="_static/panels-main.c949a650a448cc0ae9fd3441c0e17fb0.css" />
<link rel="stylesheet" type="text/css" href="_static/panels-variables.06eb56fa6e07937060861dad626602ad.css" />
<link rel="preload" as="script" href="_static/js/index.be7d3bbb2ef33a8344ce.js">
<script data-url_root="./" id="documentation_options" src="_static/documentation_options.js"></script>
<script src="_static/jquery.js"></script>
<script src="_static/underscore.js"></script>
<script src="_static/doctools.js"></script>
<script src="_static/togglebutton.js"></script>
<script src="_static/clipboard.min.js"></script>
<script src="_static/copybutton.js"></script>
<script>var togglebuttonSelector = '.toggle, .admonition.dropdown, .tag_hide_input div.cell_input, .tag_hide-input div.cell_input, .tag_hide_output div.cell_output, .tag_hide-output div.cell_output, .tag_hide_cell.cell, .tag_hide-cell.cell';</script>
<script src="_static/sphinx-book-theme.12a9622fbb08dcb3a2a40b2c02b83a57.js"></script>
<script defer="defer" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
<script>window.MathJax = {"options": {"processHtmlClass": "tex2jax_process|mathjax_process|math|output_area"}}</script>
<script async="async" src="https://unpkg.com/[email protected]/lib/index.js"></script>
<script>
const thebe_selector = ".thebe"
const thebe_selector_input = "pre"
const thebe_selector_output = ".output"
</script>
<script async="async" src="_static/sphinx-thebe.js"></script>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Logistic Regression MLE & Implementation" href="5.2%20Maximum%20Likelihood%20Estimation%20and%20Implementation.html" />
<link rel="prev" title="Gradient Descent" href="4.%20Gradient%20Descent.html" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="None">
<!-- Google Analytics -->
</head>
<body data-spy="scroll" data-target="#bd-toc-nav" data-offset="80">
<div class="container-fluid" id="banner"></div>
<div class="container-xl">
<div class="row">
<div class="col-12 col-md-3 bd-sidebar site-navigation show" id="site-navigation">
<div class="navbar-brand-box">
<a class="navbar-brand text-wrap" href="index.html">
<!-- `logo` is deprecated in Sphinx 4.0, so remove this when we stop supporting 3 -->
<img src="_static/logo.svg" class="logo" alt="logo">
<h1 class="site-logo" id="site-title">Data Science Notes</h1>
</a>
</div><form class="bd-search d-flex align-items-center" action="search.html" method="get">
<i class="icon fas fa-search"></i>
<input type="search" class="form-control" name="q" id="search-input" placeholder="Search this book..." aria-label="Search this book..." autocomplete="off" >
</form><nav class="bd-links" id="bd-docs-nav" aria-label="Main">
<div class="bd-toc-item active">
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="intro.html">
Introduction
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
Machine Learning
</span>
</p>
<ul class="current nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="1.1%20Introduction%20to%20Numpy.html">
Numpy
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="1.2%20Introduction%20to%20Matplotlib.html">
Matplotlib: Visualization with Python
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="1.3%20Introduction%20to%20Pandas.html">
Pandas
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="2.%20KNN.html">
K - Nearest Neighbour
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="3.1%20Linear%20Regression.html">
Linear Regression
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="3.2%20Multi-Variate%20Regression.html">
Multi Variable Regression
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="3.3%20MLE%20-%20Linear%20Regression.html">
MLE - Linear Regression
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="3.4%20GLM%20-%20Linear%20Regression.html">
Generalised linear model-Linear Regression
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="4.%20Gradient%20Descent.html">
Gradient Descent
</a>
</li>
<li class="toctree-l1 current active">
<a class="current reference internal" href="#">
Logistic Regression
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="5.2%20Maximum%20Likelihood%20Estimation%20and%20Implementation.html">
Logistic Regression MLE & Implementation
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="6.%20Decision%20Trees.html">
Decision Tree Algorithm
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="7.%20Ensemble.html">
Ensemble Learning
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="9.1%20Naive%20Bayes.html">
Naive Bayes Algorithm
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="9.2%20Multinomial%20Naive%20Bayes.html">
Multinomial Naive Bayes
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="11.%20Imbalanced%20Dataset.html">
Imbalanced Dataset
</a>
</li>
<li class="toctree-l1">
<a class="reference internal" href="12.%20PCA.html">
Principal Component Analysis
</a>
</li>
</ul>
<p aria-level="2" class="caption" role="heading">
<span class="caption-text">
About
</span>
</p>
<ul class="nav bd-sidenav">
<li class="toctree-l1">
<a class="reference internal" href="About%20the%20Authors.html">
Acknowledgement
</a>
</li>
</ul>
</div>
</nav> <!-- To handle the deprecated key -->
<div class="navbar_extra_footer">
Powered by <a href="https://jupyterbook.org">Jupyter Book</a>
</div>
</div>
<main class="col py-md-3 pl-md-4 bd-content overflow-auto" role="main">
<div class="topbar container-xl fixed-top">
<div class="topbar-contents row">
<div class="col-12 col-md-3 bd-topbar-whitespace site-navigation show"></div>
<div class="col pl-md-4 topbar-main">
<button id="navbar-toggler" class="navbar-toggler ml-0" type="button" data-toggle="collapse"
data-toggle="tooltip" data-placement="bottom" data-target=".site-navigation" aria-controls="navbar-menu"
aria-expanded="true" aria-label="Toggle navigation" aria-controls="site-navigation"
title="Toggle navigation" data-toggle="tooltip" data-placement="left">
<i class="fas fa-bars"></i>
<i class="fas fa-arrow-left"></i>
<i class="fas fa-arrow-up"></i>
</button>
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn" aria-label="Download this page"><i
class="fas fa-download"></i></button>
<div class="dropdown-buttons">
<!-- ipynb file if we had a myst markdown file -->
<!-- Download raw file -->
<a class="dropdown-buttons" href="_sources/5.1 Logistic Regression.ipynb"><button type="button"
class="btn btn-secondary topbarbtn" title="Download source file" data-toggle="tooltip"
data-placement="left">.ipynb</button></a>
<!-- Download PDF via print -->
<button type="button" id="download-print" class="btn btn-secondary topbarbtn" title="Print to PDF"
onClick="window.print()" data-toggle="tooltip" data-placement="left">.pdf</button>
</div>
</div>
<!-- Source interaction buttons -->
<!-- Full screen (wrap in <a> to have style consistency -->
<a class="full-screen-button"><button type="button" class="btn btn-secondary topbarbtn" data-toggle="tooltip"
data-placement="bottom" onclick="toggleFullScreen()" aria-label="Fullscreen mode"
title="Fullscreen mode"><i
class="fas fa-expand"></i></button></a>
<!-- Launch buttons -->
<div class="dropdown-buttons-trigger">
<button id="dropdown-buttons-trigger" class="btn btn-secondary topbarbtn"
aria-label="Launch interactive content"><i class="fas fa-rocket"></i></button>
<div class="dropdown-buttons">
<a class="binder-button" href="https://mybinder.org/v2/gh/executablebooks/jupyter-book/master?urlpath=tree/5.1 Logistic Regression.ipynb"><button type="button"
class="btn btn-secondary topbarbtn" title="Launch Binder" data-toggle="tooltip"
data-placement="left"><img class="binder-button-logo"
src="_static/images/logo_binder.svg"
alt="Interact on binder">Binder</button></a>
</div>
</div>
</div>
<!-- Table of contents -->
<div class="d-none d-md-block col-md-2 bd-toc show">
<div class="tocsection onthispage pt-5 pb-3">
<i class="fas fa-list"></i> Contents
</div>
<nav id="bd-toc-nav" aria-label="Page">
<ul class="visible nav section-nav flex-column">
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#introduction">
Introduction
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#bernoulli-distribution">
Bernoulli distribution
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#generalized-linear-model">
Generalized linear model
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#sigmoid-function">
Sigmoid Function
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#observations">
Observations
</a>
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#visualizing-decision-boundary">
Visualizing Decision Boundary
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#further-readings">
Further Readings
</a>
</li>
</ul>
</nav>
</div>
</div>
</div>
<div id="main-content" class="row">
<div class="col-12 col-md-9 pl-md-3 pr-md-0">
<div>
<section class="tex2jax_ignore mathjax_ignore" id="logistic-regression">
<h1>Logistic Regression<a class="headerlink" href="#logistic-regression" title="Permalink to this headline">¶</a></h1>
<section id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>Let us now look at a classification algorithm we have already seen KNN, in this section we will look at another classification algorithm called <strong>Logistic Regression.</strong></p>
<p>Logistic Regression is used when the dependent variable(target) is categorical and the independent variables are continuous.</p>
<p>For example,</p>
<ul class="simple">
<li><p>To predict whether an email is spam (1) or not spam(0)</p></li>
<li><p>Whether the tumor is malignant (1) or not(0)</p></li>
</ul>
<p>In logistic regression we try to make a <strong>decision boundary</strong> seprating the two classes. In case where there is two features a line will be representing the decision boundary. And for increase in the dimension of data, decision boundary’s dimension will also increase. following diagram helpls us to visualize the following-</p>
<p><img alt="" src="_images/lor1.png" /></p>
<p>As can been easily seen above the decision boundary is line until now we know that linear models can be used to make a straight line for the data.But observing carefully it can be understood that in linear regression line was drawn to suit the data most here we need to divide in two <a class="reference external" href="http://parts.As">parts.As</a> told in linear regression model the line is made to suit data by using it’s loss function so a new loss function will help us to do the required.</p>
<p>But,before that we we have one more problem in using the linear model, which is that in linear model our <span class="math notranslate nohighlight">\(Y_{true}\)</span> was following a <strong>Normal distribution</strong> and was continuous therefore <em>prediction was also continuous in case of linear regression</em>.
Here the <span class="math notranslate nohighlight">\(Y_{true}\)</span> and <span class="math notranslate nohighlight">\(Y_{pred}\)</span> <em>both will be discrete values</em> i.e. eiether 0 or 1 symbolizing any one of the given class.</p>
<p>To overcome this problem we have recently studied about <strong>Generalized Linear model</strong> which we can use to apply linear model on any distribution and the <em>Link function derived from it will give us the final prediction</em>.Therefore to use GLM we have to know the distribution of the data’s prediction <em>In case of binary classification it follows bernoulli distribution</em>.</p>
</section>
<section id="bernoulli-distribution">
<h2>Bernoulli distribution<a class="headerlink" href="#bernoulli-distribution" title="Permalink to this headline">¶</a></h2>
<p>The Bernoulli distribution is a discrete distribution having two possible outcomes labelled by n=0 and n=1 in which n=1 (“success”) occurs with probability <strong>p</strong> and n=0 (“failure”) occurs with probability <strong>q=1-p</strong>, where 0<p<1. It therefore has probability density function</p>
<p><img alt="image.png" src="_images/lor21.png" /></p>
<p>which can also be written</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\large{P(n)=p^n \times (1-p)^{(1-n)}}\)</span>.</p>
</div></blockquote>
<p><strong>The corresponding distribution function is-</strong></p>
<p><img alt="image-2.png" src="_images/lor22.png" /></p>
<p>Bernoulli distribution can be represented on graph something like this-</p>
<p><img alt="" src="_images/lor2.png" /></p>
<hr class="docutils" />
<blockquote>
<div><p>You can read more about bernoulli distribution from here - ( <a class="reference external" href="https://en.wikipedia.org/wiki/Bernoulli_distribution">https://en.wikipedia.org/wiki/Bernoulli_distribution</a> )</p>
</div></blockquote>
<hr class="docutils" />
<p>Now let’s calculate the Expected value of <span class="math notranslate nohighlight">\(Y\)</span>, Y here represents the class to which corresponding data will belong as Y belongs to a bernoullli distribution and possible value of Y can be 0 or 1.</p>
<p>Therefore,</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\normalsize{E[Y]=probability(Y=0)\times0+probability(y=1)\times1}\)</span></p>
</div></blockquote>
<p>using the constrainsts of bernoulli distribution where</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\normalsize{Probability(Y=1)=p}\)</span> and,</p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\normalsize{Probability(Y=0)=1-p}\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\normalsize{E[Y]=(1-p)\times0+p\times1}\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\normalsize{\therefore E[Y]=p}\)</span></p>
</div></blockquote>
<p><strong>Now as now we have understood the need of GLM in case of Logistic regression and derived some expression we also need to consider that will a bernoulli distribution work in for GLM or not in other sense will a linear model can be used in case of bernoulli distribution.</strong></p>
</section>
<section id="generalized-linear-model">
<h2>Generalized linear model<a class="headerlink" href="#generalized-linear-model" title="Permalink to this headline">¶</a></h2>
<p>To proof that a GLM will work on a given probability function we have to show that given probability distribution belongs to a exponential family and can be written in terms of general equation of GLM.</p>
<p>Equation of bernoulli distrinution-</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\large{P(y)=p^y\times(1-p)^{(1-y)}}\)</span>.</p>
</div></blockquote>
<p><strong>Taking log on both the sides-</strong></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\normalsize{\log(P(Y))=\log(p^Y\times(1-p)^{(1-Y)})}\)</span></p>
</div></blockquote>
<p><strong>Using the property of log-</strong></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\normalsize\log({P(Y))=Y\times\log(p)+(1-Y)\times\log(1-p)}\)</span></p>
</div></blockquote>
<p><strong>Taking Anti-log on both sides-</strong></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\normalsize{P(Y)=e^{\large{Y\times\log(p)+(1-Y)\times\log(1-p)}}}\)</span></p>
</div></blockquote>
<p>As, above equation shows bernoulli distribution belongs to exponential family thus now we can use GLM on it in order to use a linear model in case of a bernoulli distribution.</p>
<p>Now comparing the above derived equation of bernoulli distribution with General eqution that we studied in GLM section prevoisly.</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\normalsize{P(Y)=b(Y)\times^{\Large{\eta*T(Y)-a(\eta)}}}\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\normalsize{P(Y)=e^{\large{Y\times\log(p)+(1-Y)\times\log(1-p)}}}\)</span></p>
</div></blockquote>
<hr class="docutils" />
<p><strong>After comparing we can see that-</strong></p>
<p><span class="math notranslate nohighlight">\(1. \large{b(Y)=1}\)</span></p>
<p><span class="math notranslate nohighlight">\(2. \large{\eta=log(\Large{\frac{p}{1-p})}}\)</span></p>
<p><span class="math notranslate nohighlight">\(3. \large{T(Y)=Y}\)</span></p>
<p><span class="math notranslate nohighlight">\(4. \large{a(\eta)=-log(1-p)}\)</span></p>
<p>Which can also be written as</p>
<p><span class="math notranslate nohighlight">\(\large{a(\eta)=log(\Large{\frac{1}{1-p})}}\)</span></p>
<hr class="docutils" />
<p>As discussed in GLM section that the <strong>Expected value of T(Y)is our final pridection</strong>-</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(E[T(Y)]\)</span> <strong>is our prediction</strong></p>
</div></blockquote>
<p>As from observation we can see that <span class="math notranslate nohighlight">\(\small{T(Y)=Y}\)</span> Therefore,</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(E[Y]\)</span> <strong>is our prediction</strong></p>
</div></blockquote>
<p>Previously in this section we derived that <span class="math notranslate nohighlight">\(E[Y]=p\)</span> thus <span class="math notranslate nohighlight">\(p\)</span> will be our final <a class="reference external" href="http://prediction.as">prediction.as</a> <span class="math notranslate nohighlight">\(p\)</span> will be our final prediction now we have to state a relation between our final prediction and the value we will get by applying linear model.</p>
<p>As we know that the value given by the linear model will <span class="math notranslate nohighlight">\(XW\)</span> which is GLM is represented by term <span class="math notranslate nohighlight">\(\eta\)</span> as <span class="math notranslate nohighlight">\(\eta\)</span> is the prediction given by our linear model.</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\eta=XW\)</span></p>
</div></blockquote>
<p>Here we have to state a relation between value given by our linear model( <span class="math notranslate nohighlight">\(\normalsize\eta\)</span> ) and our final prediction (<span class="math notranslate nohighlight">\(XW\)</span>). from above observed operation we can find out that-</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\({\eta=log({\dfrac{p}{1-p})}}\)</span></p>
</div></blockquote>
<p><strong>Taking Anti-Log-</strong></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\({e^\eta=\large{\frac{p}{1-p}}}\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\({e^\eta\times(1-p)={{p}}}\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\({e^\eta-e^\eta\times p=p}\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\({e^\eta=p+e^\eta\times p}\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\({e^\eta=p(1+e^\eta)}\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\({p={\dfrac{e^\eta}{1+e^\eta}}}\)</span></p>
</div></blockquote>
<p><strong>Which than can be simplified and written as-</strong></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\({p={\dfrac{1}{1+e^{-\eta}}}}\)</span></p>
</div></blockquote>
<p>subsituting the value of <span class="math notranslate nohighlight">\(\eta-\)</span></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\({p={\dfrac{1}{1+e^{-XW}}}}\)</span></p>
</div></blockquote>
<p>As discussed <span class="math notranslate nohighlight">\(p\)</span> will be our final prediction therefore-</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\therefore{Y_{pred}={\dfrac{1}{1+e^{-XW}}}}\)</span></p>
</div></blockquote>
<p>The above expression is called <strong>Cannonical Responce Function</strong>. and is commonly given name is <strong>Sigmoid function</strong>.</p>
<section id="sigmoid-function">
<h3>Sigmoid Function<a class="headerlink" href="#sigmoid-function" title="Permalink to this headline">¶</a></h3>
<p>A sigmoid function is a mathematical function having a characteristic “S”-shaped curve or sigmoid curve.A sigmoid function is a bounded, differentiable, real function that is defined for all real input values and has a non-negative derivative at each point and exactly one inflection point. A sigmoid “function” and a sigmoid “curve” refer to the same object.</p>
<p>A common example of a sigmoid function is the logistic function shown in the first figure and defined by the formula:</p>
<p><span class="math notranslate nohighlight">\({\displaystyle S(x)={\frac {1}{1+e^{-x}}}={\frac {e^{x}}{e^{x}+1}}=1-S(-x).}{\displaystyle S(x)={\frac {1}{1+e^{-x}}}={\frac {e^{x}}{e^{x}+1}}=1-S(-x).}\)</span></p>
<p>Other standard sigmoid functions are given in the Examples section. In some fields, most notably in the context of artificial neural networks, the term “sigmoid function” is used as an alias for the logistic function.</p>
<p><img alt="" src="_images/lor3.png" /></p>
</section>
<section id="observations">
<h3>Observations<a class="headerlink" href="#observations" title="Permalink to this headline">¶</a></h3>
<p>Looking closely you will observe that the range of this function is between [0,1] for any value of the input and it is exactly what we want for any value predicted by our linear model we will get value between after passing it to the sigmoid function.
and then we can set a threshold to identify the class.</p>
<p>You can read more about sigmoid function from here-( <a class="reference external" href="https://en.wikipedia.org/wiki/Sigmoid_function">https://en.wikipedia.org/wiki/Sigmoid_function</a> )</p>
</section>
</section>
<section id="visualizing-decision-boundary">
<h2>Visualizing Decision Boundary<a class="headerlink" href="#visualizing-decision-boundary" title="Permalink to this headline">¶</a></h2>
<p>As now we have both our final prediction function and our linear model ready before setting up the parameters of linear model. Let’s just try to analyse how the two classes will get seperated.</p>
<p><strong>We know that</strong></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(P(Y)=\normalsize{p^Y\times(1-p)^{(1-Y)}}\)</span></p>
</div></blockquote>
<p>For <strong>Y=0</strong></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(P(Y)=\normalsize{(1-p)}\)</span></p>
</div></blockquote>
<p>For <strong>Y=1</strong></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(P(Y)=\normalsize p\)</span></p>
</div></blockquote>
<p>As we have seen in MLE to maximize the likelihood we have to maximize it’s probability function Therfore,</p>
<ul class="simple">
<li><p><em>For Y=0 we have to maximize</em> <span class="math notranslate nohighlight">\(\normalsize{(1-p)}\)</span> <em>or in other words minimize</em> <span class="math notranslate nohighlight">\(\normalsize p\)</span></p></li>
<li><p><em>For Y=1 we have to maximize</em> <span class="math notranslate nohighlight">\(\normalsize p\)</span>.</p></li>
</ul>
<p>As discussed above <span class="math notranslate nohighlight">\(\normalsize{p={\frac{1}{1+e^{-XW}}}}\)</span> for the sake of simplicity let us represent the term <span class="math notranslate nohighlight">\(XW\)</span> with term <span class="math notranslate nohighlight">\(\large z\)</span></p>
<ul class="simple">
<li><p>For <strong>Y=0</strong> we have to minimize <span class="math notranslate nohighlight">\(p\)</span> which means minimizing <span class="math notranslate nohighlight">\(\large{\frac{1}{1+e^{-z}}}\)</span> or <strong>maximizing</strong> the term <span class="math notranslate nohighlight">\(\large{{1+e^{-z}}}\)</span> which simply translates to maxmizing the term <span class="math notranslate nohighlight">\(\large{e^{-z}}\)</span>.</p></li>
<li><p>For <strong>Y=1</strong> we have maximize <span class="math notranslate nohighlight">\(p\)</span> which means maximizing <span class="math notranslate nohighlight">\(\large{\frac{1}{1+e^{-z}}}\)</span> or <strong>minimizing</strong> the term <span class="math notranslate nohighlight">\(\large{{1+e^{-z}}}\)</span> which simply translates to minimizing the term <span class="math notranslate nohighlight">\(\large{e^{-z}}\)</span>.</p></li>
</ul>
<p>Observing carefully for both of the above results as e(Exponent) is constant thus will have no effect on either maximizing or minimizing the expression thus the minimum or maximum of the expression only depends on the term <span class="math notranslate nohighlight">\(\normalsize z\)</span>. It can also be seen due to the negative sign in both of the expression that</p>
<hr class="docutils" />
<ul class="simple">
<li><p><strong>For Y=0 we have to minimize</strong> <span class="math notranslate nohighlight">\(\large z\)</span></p></li>
<li><p><strong>For Y=1 we have to maximize</strong> <span class="math notranslate nohighlight">\(\large z\)</span></p></li>
</ul>
<hr class="docutils" />
<p>As <span class="math notranslate nohighlight">\(\large{z}=\small XW\)</span> and it is the value that will come from a linear model thus will be <em>continous</em> so every point having <em>Y=0 will try to minimize the value</em> and every point having <em>Y=1 will try to maximize the value</em>. For a continous value <em>minimum value is</em> <span class="math notranslate nohighlight">\(-\infty\)</span> and <em>maximum value is</em> <span class="math notranslate nohighlight">\(\infty\)</span>.</p>
<p>Therfore when plotting it, a 3d plot is required if number of independent variable is 2. 3rd dimension will have the <span class="math notranslate nohighlight">\(\normalsize z\)</span> plotted on it. also when the graph will get plotted signifying the value of <span class="math notranslate nohighlight">\(\normalsize z\)</span> for the two features.This <em>plane is actually the decision boundary for the 2 dimensional data</em>.</p>
<p>It can be visualised like in the diagrams given below-</p>
<p><img alt="image-2.png" src="_images/lor4.png" /></p>
<p>Here you can see that points belonging to class <strong>1</strong> is pushing <span class="math notranslate nohighlight">\(z\)</span> to get the higher value which is <span class="math notranslate nohighlight">\(\infty\)</span> and points belonging to the class <strong>0</strong> is pushing <span class="math notranslate nohighlight">\(z\)</span> to get the lowest value which is <span class="math notranslate nohighlight">\(-\infty\)</span></p>
<p>The line shown as the decision boundary earlier in this section is the intersection line between the decision boundary plane and the plane in which features lies. For higher dimensions(features) say <em>n</em> dimension, <em>n+1</em> dimension decision boundary will get fitted on the data then it’s intersection with feature’s dimension can be visualized easily below is the decision line formed by the intersection of the decision surface and the palne of the features for <em>n=2</em></p>
<p><img alt="image.png" src="_images/lor5.png" /></p>
</section>
<section id="further-readings">
<h2>Further Readings<a class="headerlink" href="#further-readings" title="Permalink to this headline">¶</a></h2>
<p>You can read more about sigmoid function from here-( <a class="reference external" href="https://en.wikipedia.org/wiki/Sigmoid_function">https://en.wikipedia.org/wiki/Sigmoid_function</a> )</p>
<p><a class="reference external" href="https://en.wikipedia.org/wiki/Bernoulli_distribution">https://en.wikipedia.org/wiki/Bernoulli_distribution</a></p>
</section>
</section>
<script type="text/x-thebe-config">
{
requestKernel: true,
binderOptions: {
repo: "binder-examples/jupyter-stacks-datascience",
ref: "master",
},
codeMirrorConfig: {
theme: "abcdef",
mode: "python"
},
kernelOptions: {
kernelName: "python3",
path: "./."
},
predefinedOutput: true
}
</script>
<script>kernelName = 'python3'</script>
</div>
<!-- Previous / next buttons -->
<div class='prev-next-area'>
<a class='left-prev' id="prev-link" href="4.%20Gradient%20Descent.html" title="previous page">
<i class="fas fa-angle-left"></i>
<div class="prev-next-info">
<p class="prev-next-subtitle">previous</p>
<p class="prev-next-title">Gradient Descent</p>
</div>
</a>
<a class='right-next' id="next-link" href="5.2%20Maximum%20Likelihood%20Estimation%20and%20Implementation.html" title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title">Logistic Regression MLE & Implementation</p>
</div>
<i class="fas fa-angle-right"></i>
</a>
</div>
</div>
</div>
<footer class="footer">
<div class="container">
<p>
By Coding Blocks Pvt Ltd<br/>
© Copyright 2021.<br/>
</p>
</div>
</footer>
</main>
</div>
</div>
<script src="_static/js/index.be7d3bbb2ef33a8344ce.js"></script>
</body>
</html>