-
Notifications
You must be signed in to change notification settings - Fork 1
/
9.2 Multinomial Naive Bayes.html
640 lines (568 loc) · 29.5 KB
/
9.2 Multinomial Naive Bayes.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
<!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>Multinomial Naive Bayes — 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/thebe@0.5.1/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="Imbalanced Dataset" href="11.%20Imbalanced%20Dataset.html" />
<link rel="prev" title="Naive Bayes Algorithm" href="9.1%20Naive%20Bayes.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">
<a class="reference internal" href="5.1%20%20Logistic%20Regression.html">
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 current active">
<a class="current reference internal" href="#">
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/9.2 Multinomial Naive Bayes.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/9.2 Multinomial Naive Bayes.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="#types-of-distributions">
Types of Distributions
</a>
<ul class="nav section-nav flex-column">
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#gaussian-normal-distribution">
Gaussian/Normal Distribution
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#bernoulli-distribution">
Bernoulli Distribution
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#binomial-distribution">
Binomial Distribution
</a>
</li>
<li class="toc-h3 nav-item toc-entry">
<a class="reference internal nav-link" href="#multinomial-distribution">
Multinomial Distribution
</a>
</li>
</ul>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#introduction-to-multi-nomial-naive-bayes">
Introduction to Multi-nomial Naive Bayes
</a>
</li>
<li class="toc-h2 nav-item toc-entry">
<a class="reference internal nav-link" href="#mle-for-multinomial-naive-bayes">
MLE for Multinomial Naive Bayes
</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="multinomial-naive-bayes">
<h1>Multinomial Naive Bayes<a class="headerlink" href="#multinomial-naive-bayes" title="Permalink to this headline">¶</a></h1>
<p>Before proceeding to Multinomial Naive Bayes, let’s have a quick look on the types of distributions we’ve seen yet.</p>
<section id="types-of-distributions">
<h2>Types of Distributions<a class="headerlink" href="#types-of-distributions" title="Permalink to this headline">¶</a></h2>
<section id="gaussian-normal-distribution">
<h3>Gaussian/Normal Distribution<a class="headerlink" href="#gaussian-normal-distribution" title="Permalink to this headline">¶</a></h3>
<p>This is the continuos distribution of data, where most of the data lies around the mean value, we’ve seen more about this in Linear Regression MLE. Here the mean of the data is represented by <span class="math notranslate nohighlight">\(\mu\)</span> and the standard deviation of the data is represented by <span class="math notranslate nohighlight">\(\sigma\)</span>.</p>
<p><img alt="standard-normal-distribution-1024x633%20%281%29.jpg" src="_images/mnb1.jpeg" /></p>
</section>
<hr class="docutils" />
<section id="bernoulli-distribution">
<h3>Bernoulli Distribution<a class="headerlink" href="#bernoulli-distribution" title="Permalink to this headline">¶</a></h3>
<p>This is a distribution where the no. of possible outcomes are only two and the event is occurred only a single time.</p>
<p>Eg: Flipping a coin for a single time</p>
<blockquote>
<div><p><strong>Equation :</strong></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\large{P(X) = p^n \times (1-p)^{1-n}}\)</span></p>
<p><em>p = probability of that event</em></p>
<p><em>n = binary output value i.e. either 0 or 1</em></p>
</div></blockquote>
</div></blockquote>
<p>Eg: Probability of heads for a fair coin</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>$P(H) = (0.5)^1 \times (0.5)^0$
$P(H) = 0.5$
</pre></div>
</div>
<p><img alt="ComputeBernoulliDistributionPdfExample_01.jpg" src="_images/mnb2.jpeg" /></p>
</section>
<hr class="docutils" />
<section id="binomial-distribution">
<h3>Binomial Distribution<a class="headerlink" href="#binomial-distribution" title="Permalink to this headline">¶</a></h3>
<p>When a Bernoulli Distribution is performed for ‘n’ no. of trials it becomes a binomial distribution.</p>
<p>Eg: Flipping a coin for 5 times.</p>
<blockquote>
<div><p><strong>Equation:</strong></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\large{P(X) =\hspace{1mm}^nC_x \hspace{1mm}[p^x \times (1-p)^{n-x}]}\)</span><br>
p = probability of a single event<br>
x = value of outcome<br>
n = no. of trials<br></p>
</div></blockquote>
</div></blockquote>
<p><img alt="binomial-distribution-n100-p05.png" src="_images/mnb3.png" /></p>
</section>
<hr class="docutils" />
<section id="multinomial-distribution">
<h3>Multinomial Distribution<a class="headerlink" href="#multinomial-distribution" title="Permalink to this headline">¶</a></h3>
<p>When the number of possible outcomes becomes ‘m’ and no. of trials are ‘n’, it becomes Multinomial Distribution.</p>
<p>Eg: Rolling a die 3 times</p>
<blockquote>
<div><p><strong>Equation:</strong></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\large{P(X) = \dfrac{n!}{(n_1!)(n_2!)...(n_m!)}(P_1)^{n_1}(P_2)^{n_2}...(P_m)^{n_m}}\)</span><br>
<span class="math notranslate nohighlight">\(n\)</span> = number of events<br />
<span class="math notranslate nohighlight">\(n_1\)</span> = number of outcomes, event 1<br />
<span class="math notranslate nohighlight">\(n_2\)</span> = number of outcomes, event 2<br />
<span class="math notranslate nohighlight">\(n_m\)</span> = number of outcomes, event m<br />
<span class="math notranslate nohighlight">\(p_1\)</span> = probability event 1 happens<br />
<span class="math notranslate nohighlight">\(p_2\)</span> = probability event 2 happens<br />
<span class="math notranslate nohighlight">\(p_m\)</span> = probability event m happens</p>
</div></blockquote>
</div></blockquote>
</section>
</section>
<hr class="docutils" />
<section id="introduction-to-multi-nomial-naive-bayes">
<h2>Introduction to Multi-nomial Naive Bayes<a class="headerlink" href="#introduction-to-multi-nomial-naive-bayes" title="Permalink to this headline">¶</a></h2>
<p>As we saw in Naive Bayes, it is a simple technique for constructing binary classifiers: models that are able to classify in binary values(0 & 1). A naive Bayes classifier considers each of these features to contribute independently. A Multinomial Naive Bayes is able to perform a lot of complexer tasks. Like Naive Bayes was able to classify an email into the category of Spam or Not Spam, but if we want to classify some articles into some categories, naive Bayes may not be able to do it, as it is not a binary classification task. But on the other hand, Multinomial Naive Bayes is able to perform this task.</p>
<p>Multinomial Naive Bayes algorithm is a probabilistic learning method that is mostly used in Natural Language Processing (NLP). The algorithm is based on the Bayes theorem and predicts the tag of a text such as a piece of email or newspaper article. It calculates the probability of each tag for a given sample and then gives the tag with the highest probability as output. Let’s try to understand Multinomial naive Bayes with the help of an example.</p>
<p>Classify these articles in the sector of Education, News or Sports:</p>
<ol class="simple">
<li><p>The Prime Minister of India, changed their currency notes.</p></li>
<li><p>Virat Kohli is a great young player of Cricket. He is one of the great batsman of India.</p></li>
<li><p>Vivekanand International is a great school. There are many co-curricular activities also.</p></li>
<li><p>Internet Services has been banned in Jammu. Will be resumed by next month.</p></li>
<li><p>Students of D.A.V are very active. Cricket is very popular among them.</p></li>
</ol>
<p>Now just by looking at the existence of some words, we will not be able to classify them correctly. As we can see, “India” is present on both article no. <span class="math notranslate nohighlight">\(1\)</span> and <span class="math notranslate nohighlight">\(2\)</span>. But the <span class="math notranslate nohighlight">\(1^{st}\)</span> article should be classified under <code class="docutils literal notranslate"><span class="pre">News</span></code> while <span class="math notranslate nohighlight">\(2^{nd}\)</span> should be classified under <code class="docutils literal notranslate"><span class="pre">Sports</span></code>. Same as article no. <span class="math notranslate nohighlight">\(2\)</span> and <span class="math notranslate nohighlight">\(5\)</span>, both have the word “Cricket”. But <span class="math notranslate nohighlight">\(2^{nd}\)</span> is under <code class="docutils literal notranslate"><span class="pre">Sports</span></code> and <span class="math notranslate nohighlight">\(5^{th}\)</span> is under <code class="docutils literal notranslate"><span class="pre">Education</span></code>. So, we need something more robust here, so we can classify them by also seeing the word count. So the vectorization in Multinomial Naive Bayes is performed by filling out the word count, instead of 0 and 1.</p>
<blockquote>
<div><blockquote>
<div><p><strong>So our data should look like:</strong></p>
</div></blockquote>
</div></blockquote>
<table class="colwidths-auto table">
<thead>
<tr class="row-odd"><th class="head"><p>Word1</p></th>
<th class="head"><p>Word2</p></th>
<th class="head"><p>Word3</p></th>
<th class="head"><p>Word4</p></th>
<th class="head"><p>Word5</p></th>
<th class="head"><p>Word6</p></th>
<th class="head"><p>Sports or Not</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>3</p></td>
<td><p>2</p></td>
<td><p>0</p></td>
<td><p>1</p></td>
<td><p>4</p></td>
<td><p>2</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-odd"><td><p>4</p></td>
<td><p>2</p></td>
<td><p>3</p></td>
<td><p>0</p></td>
<td><p>1</p></td>
<td><p>5</p></td>
<td><p>0</p></td>
</tr>
<tr class="row-even"><td><p>2</p></td>
<td><p>2</p></td>
<td><p>2</p></td>
<td><p>1</p></td>
<td><p>0</p></td>
<td><p>0</p></td>
<td><p>1</p></td>
</tr>
<tr class="row-odd"><td><p>4</p></td>
<td><p>1</p></td>
<td><p>1</p></td>
<td><p>0</p></td>
<td><p>0</p></td>
<td><p>0</p></td>
<td><p>0</p></td>
</tr>
<tr class="row-even"><td><p>9</p></td>
<td><p>0</p></td>
<td><p>3</p></td>
<td><p>1</p></td>
<td><p>1</p></td>
<td><p>2</p></td>
<td><p>1</p></td>
</tr>
</tbody>
</table>
<p>Now according to Bayes Theorem:</p>
<p><span class="math notranslate nohighlight">\(P(y/X) = \dfrac{P(X/y) \times P(y)}{P(X)}\)</span></p>
<p>Here <span class="math notranslate nohighlight">\(P(X)\)</span> and <span class="math notranslate nohighlight">\(P(y)\)</span> is constant for every <span class="math notranslate nohighlight">\(P(y/X)\)</span>, so we’ve to focus on <span class="math notranslate nohighlight">\(P(X/y)\)</span></p>
<p><span class="math notranslate nohighlight">\(P(X/y) = P(x_1, x_2, x_3... x_n / y) = \dfrac{N!}{x_1! x_2! x_3! ... x_n!} \times P(W_1)^{x_1}.P(W_2)^{x_2}.P(W_3)^{x_3} ... P(W_n)^{x_n}\)</span></p>
<blockquote>
<div><p>Here:</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(x_1\)</span> = count of Word1</p>
<p><span class="math notranslate nohighlight">\(x_2\)</span> = count of Word2</p>
<p><span class="math notranslate nohighlight">\(x_n\)</span> = count of Wordn</p>
<p><span class="math notranslate nohighlight">\(N\)</span> = Total words count</p>
<p><span class="math notranslate nohighlight">\(P(W_1)\)</span> = Prob. of occurence of Word1 individually</p>
<p><span class="math notranslate nohighlight">\(P(W_2)\)</span> = Prob. of occurence of Word2 individually</p>
<p><span class="math notranslate nohighlight">\(P(W_n)\)</span> = Prob. of occurence of Wordn individually</p>
</div></blockquote>
</div></blockquote>
<p>Generalizing above equation:</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(P(X/y) = \dfrac{N!}{\prod_{i=1}^{n} x_i!} \times \prod^n_{i=1}[P(W_i)^{x_i}]\)</span></p>
</div></blockquote>
<p>Here the value of y can be 0 or 1 (i.e. Sports Article or not)</p>
<p>Taking the data, where y=0</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(P(X/y_{=0}) = \dfrac{N_0!}{\prod_{i=1}^{n} x_i!} \times \prod^n_{i=1}[P(W_i)^{x_i}]\)</span></p>
</div></blockquote>
<p>Notice here now <span class="math notranslate nohighlight">\(N_0\)</span> = Total words count where y=0</p>
<p>Now to maximize the probability of <span class="math notranslate nohighlight">\(P(X/y_{=0/1})\)</span>, we have to maximize the likelihood of the word distribution accordingly.</p>
</section>
<section id="mle-for-multinomial-naive-bayes">
<h2>MLE for Multinomial Naive Bayes<a class="headerlink" href="#mle-for-multinomial-naive-bayes" title="Permalink to this headline">¶</a></h2>
<p>Taking Total no. of words = <span class="math notranslate nohighlight">\(v\)</span></p>
<p>Total Word count = <span class="math notranslate nohighlight">\(N\)</span></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(P(x_1, x_2, x_3 ... x_v) = \dfrac{N!}{x_i!} \prod_{i=1}^{v}p_i^{x_i}\)</span></p>
</div></blockquote>
<p>Taking log:</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(P(x_1, x_2, x_3 ... x_v) = log \dfrac{N!}{x_i!} + \sum_{i=1}^{v} x_i log(p_i)\)</span></p>
</div></blockquote>
<p>Using Lagrange’s Multipliers</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(F(p) = log \dfrac{N!}{x_i!} + \sum_{i=1}^{v} x_i log(p_i)\)</span></p>
</div></blockquote>
<p>and as sum of all probabilities = 1</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(G(p) = \sum_{i=1}^{v} p_i = 1\)</span></p>
</div></blockquote>
<p>Lagrange Multiplier Equation : <span class="math notranslate nohighlight">\(L = \triangle F(p) - \lambda \triangle G(p) = 0\)</span></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\dfrac{dL}{dp_i} = \triangle \begin{pmatrix} log\dfrac{N!}{x_i!} + \sum^{v}_{i=1} x_i.log(p_i) \end{pmatrix} - \lambda \triangle \begin{pmatrix} \sum_{i=1}^v p_i - 1\end{pmatrix}\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\dfrac{dL}{dp_i} = \dfrac{x_i}{p_i} - \lambda\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(0 = \dfrac{x_i}{p_i} - \lambda\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(p_i = \dfrac{x_i}{\lambda} \hspace{2cm}\)</span> <span class="math notranslate nohighlight">\(-- \large{Eq^n.1}\)</span></p>
</div></blockquote>
<p>Put this value of <span class="math notranslate nohighlight">\(p_i\)</span> in <span class="math notranslate nohighlight">\(G(p)\)</span></p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(G(p) = \sum_{i=1}^v \dfrac{x_i}{\lambda} = 1\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(\sum_{i=1}^v x_i = \lambda\)</span></p>
</div></blockquote>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(N = \lambda \hspace{3cm}\)</span> As <span class="math notranslate nohighlight">\(\begin{pmatrix} \sum_{i=1}^v x_i = N \end{pmatrix}\)</span></p>
</div></blockquote>
<p>Put this value of <span class="math notranslate nohighlight">\(\lambda\)</span> in eq.1</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(p_i = \dfrac{x_i}{N}\)</span></p>
</div></blockquote>
<p>So, we got to know probability of each word [<span class="math notranslate nohighlight">\(P(W_i)\)</span>] is <span class="math notranslate nohighlight">\(\dfrac{x_{Wi}}{N}\)</span>, and finally substituting this in our equation</p>
<blockquote>
<div><p><span class="math notranslate nohighlight">\(P(X/y) = \dfrac{N!}{\prod_{i=1}^{n} x_i!} \times \prod_{i=1}^{n}(P_{Wi})^{x_i}\)</span></p>
</div></blockquote>
<p>and we know the values of <span class="math notranslate nohighlight">\(N\)</span> and <span class="math notranslate nohighlight">\(x_i\)</span> using the table, that we made using vectorization and hence we can find <span class="math notranslate nohighlight">\(P(X/y)\)</span> and eventually we can make our prediction.</p>
</section>
<hr class="docutils" />
<section id="further-readings">
<h2>Further Readings<a class="headerlink" href="#further-readings" title="Permalink to this headline">¶</a></h2>
<p>Like we saw Multinomial Naive Bayes, other Naive Bayes depending on some other distributions of the data also exist.</p>
<p>You may read about them in the docs:</p>
<p>Multinomial NB: <a class="reference external" href="https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.MultinomialNB.html#examples-using-sklearn-naive-bayes-multinomialnb">https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.MultinomialNB.html#examples-using-sklearn-naive-bayes-multinomialnb</a></p>
<p>Bernoulli NB: <a class="reference external" href="https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.BernoulliNB.html#sklearn.naive_bayes.BernoulliNB">https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.BernoulliNB.html#sklearn.naive_bayes.BernoulliNB</a></p>
<p>Categorical NB: <a class="reference external" href="https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.CategoricalNB.html#sklearn.naive_bayes.CategoricalNB">https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.CategoricalNB.html#sklearn.naive_bayes.CategoricalNB</a></p>
<p>Gaussian NB: <a class="reference external" href="https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html#sklearn.naive_bayes.GaussianNB">https://scikit-learn.org/stable/modules/generated/sklearn.naive_bayes.GaussianNB.html#sklearn.naive_bayes.GaussianNB</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="9.1%20Naive%20Bayes.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">Naive Bayes Algorithm</p>
</div>
</a>
<a class='right-next' id="next-link" href="11.%20Imbalanced%20Dataset.html" title="next page">
<div class="prev-next-info">
<p class="prev-next-subtitle">next</p>
<p class="prev-next-title">Imbalanced Dataset</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>