-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdd.html
735 lines (705 loc) · 31 KB
/
dd.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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Categorical Data and the Dirichlet Discrete Distribution</title>
<link rel="stylesheet" href="_static/basic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/bootswatch-3.3.4/lumen/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="_static/bootstrap-sphinx.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.1.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="_static/js/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="_static/js/jquery-fix.js"></script>
<script type="text/javascript" src="_static/bootstrap-3.3.4/js/bootstrap.min.js"></script>
<script type="text/javascript" src="_static/bootstrap-sphinx.js"></script>
<link rel="top" title="None" href="index.html" />
<link rel="up" title="Tutorials" href="docs.html" />
<link rel="next" title="Real Valued Data and the Normal Inverse-Wishart Distribution" href="niw.html" />
<link rel="prev" title="Binary Data with the Beta Bernouli Distribution" href="bb.html" />
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1'>
<meta name="apple-mobile-web-app-capable" content="yes">
</head>
<body role="document">
<div id="navbar" class="navbar navbar-inverse navbar-default navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<!-- .btn-navbar is used as the toggle for collapsed navbar content -->
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">
datamicroscopes</a>
<span class="navbar-text navbar-version pull-left"><b>0.1</b></span>
</div>
<div class="collapse navbar-collapse nav-collapse">
<ul class="nav navbar-nav">
<li><a href="https://github.com/datamicroscopes">GitHub</a></li>
<li><a href="https://qadium.com/">Qadium</a></li>
<li class="dropdown globaltoc-container">
<a role="button"
id="dLabelGlobalToc"
data-toggle="dropdown"
data-target="#"
href="index.html">Site <b class="caret"></b></a>
<ul class="dropdown-menu globaltoc"
role="menu"
aria-labelledby="dLabelGlobalToc"><ul>
<li class="toctree-l1"><a class="reference internal" href="intro.html">Discovering structure in your data: an overview of clustering</a></li>
<li class="toctree-l1"><a class="reference internal" href="ncluster.html">Finding the number of clusters with the Dirichlet Process</a></li>
<li class="toctree-l1"><a class="reference internal" href="enron_blog.html">Network Modeling with the Infinite Relational Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="topic.html">Bayesian Nonparametric Topic Modeling with the Daily Kos</a></li>
</ul>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="datatypes.html">Datatypes and Bayesian Nonparametric Models</a></li>
<li class="toctree-l1"><a class="reference internal" href="bb.html">Binary Data with the Beta Bernouli Distribution</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="">Categorical Data and the Dirichlet Discrete Distribution</a></li>
<li class="toctree-l1"><a class="reference internal" href="niw.html">Real Valued Data and the Normal Inverse-Wishart Distribution</a></li>
<li class="toctree-l1"><a class="reference internal" href="nic.html">Univariate Data with the Normal Inverse Chi-Square Distribution</a></li>
<li class="toctree-l1"><a class="reference internal" href="gamma_poisson.html">Count Data and Ordinal Data with the Gamma-Poisson Distribution</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="gauss2d.html">Inferring Gaussians with the Dirichlet Process Mixture Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="mnist_predictions.html">Digit recognition with the MNIST dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="enron_email.html">Clustering the Enron e-mail corpus using the Infinite Relational Model</a></li>
<li class="toctree-l1"><a class="reference internal" href="hdp.html">Learning Topics in The Daily Kos with the Hierarchical Dirichlet Process</a></li>
</ul>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="docs.html">Tutorials</a><ul>
<li class="toctree-l2"><a class="reference internal" href="intro.html">Discovering structure in your data: an overview of clustering</a></li>
<li class="toctree-l2"><a class="reference internal" href="ncluster.html">Finding the number of clusters with the Dirichlet Process</a></li>
<li class="toctree-l2"><a class="reference internal" href="enron_blog.html">Network Modeling with the Infinite Relational Model</a></li>
<li class="toctree-l2"><a class="reference internal" href="topic.html">Bayesian Nonparametric Topic Modeling with the Daily Kos</a></li>
</ul>
</li>
<li class="toctree-l1 current"><a class="reference internal" href="docs.html#datatypes-and-likelihood-models-in-datamicroscopes">Datatypes and likelihood models in datamicroscopes</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="datatypes.html">Datatypes and Bayesian Nonparametric Models</a></li>
<li class="toctree-l2"><a class="reference internal" href="bb.html">Binary Data with the Beta Bernouli Distribution</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="">Categorical Data and the Dirichlet Discrete Distribution</a></li>
<li class="toctree-l2"><a class="reference internal" href="niw.html">Real Valued Data and the Normal Inverse-Wishart Distribution</a></li>
<li class="toctree-l2"><a class="reference internal" href="nic.html">Univariate Data with the Normal Inverse Chi-Square Distribution</a></li>
<li class="toctree-l2"><a class="reference internal" href="gamma_poisson.html">Count Data and Ordinal Data with the Gamma-Poisson Distribution</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="docs.html#examples">Examples</a><ul>
<li class="toctree-l2"><a class="reference internal" href="gauss2d.html">Inferring Gaussians with the Dirichlet Process Mixture Model</a></li>
<li class="toctree-l2"><a class="reference internal" href="mnist_predictions.html">Digit recognition with the MNIST dataset</a></li>
<li class="toctree-l2"><a class="reference internal" href="enron_email.html">Clustering the Enron e-mail corpus using the Infinite Relational Model</a></li>
<li class="toctree-l2"><a class="reference internal" href="hdp.html">Learning Topics in The Daily Kos with the Hierarchical Dirichlet Process</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="api.html">API Reference</a><ul>
<li class="toctree-l2"><a class="reference internal" href="microscopes.common.dataview.html">dataviews</a></li>
<li class="toctree-l2"><a class="reference internal" href="microscopes.common.util.html">util</a></li>
<li class="toctree-l2"><a class="reference internal" href="microscopes.common.random.html">microscopes.common.random</a></li>
<li class="toctree-l2"><a class="reference internal" href="microscopes.common.query.html">query</a></li>
<li class="toctree-l2"><a class="reference internal" href="microscopes.common.validator.html">microscopes.common.validator</a></li>
<li class="toctree-l2"><a class="reference internal" href="microscopes.kernels.parallel.html">parallel</a></li>
<li class="toctree-l2"><a class="reference internal" href="microscopes.mixture.html">mixturemodel</a></li>
<li class="toctree-l2"><a class="reference internal" href="microscopes.irm.html">irm</a></li>
<li class="toctree-l2"><a class="reference internal" href="microscopes.kernels.html">kernels</a></li>
<li class="toctree-l2"><a class="reference internal" href="api.html#indices-and-tables">Indices and tables</a></li>
</ul>
</li>
</ul>
</ul>
</li>
<li class="dropdown">
<a role="button"
id="dLabelLocalToc"
data-toggle="dropdown"
data-target="#"
href="#">Contents <b class="caret"></b></a>
<ul class="dropdown-menu localtoc"
role="menu"
aria-labelledby="dLabelLocalToc"><ul>
<li><a class="reference internal" href="#">Categorical Data and the Dirichlet Discrete Distribution</a></li>
</ul>
</ul>
</li>
<li class="hidden-sm">
<div id="sourcelink">
<a href="_sources/dd.txt"
rel="nofollow">Source</a>
</div></li>
</ul>
<form class="navbar-form navbar-right" action="search.html" method="get">
<div class="form-group">
<input type="text" name="q" class="form-control" placeholder="Search" />
</div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="section" id="categorical-data-and-the-dirichlet-discrete-distribution">
<h1>Categorical Data and the Dirichlet Discrete Distribution<a class="headerlink" href="#categorical-data-and-the-dirichlet-discrete-distribution" title="Permalink to this headline">¶</a></h1>
<hr class="docutils" />
<p>Let’s consider some examples of data with categorical variables</p>
<div class="code python highlight-python"><div class="highlight"><pre>import pandas as pd
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
sns.set_context('talk')
sns.set_style('darkgrid')
</pre></div>
</div>
<p>First, the passenger list of the Titanic</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">titanic</span> <span class="o">=</span> <span class="n">sns</span><span class="o">.</span><span class="n">load_dataset</span><span class="p">(</span><span class="s">"titanic"</span><span class="p">)</span>
</pre></div>
</div>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">titanic</span><span class="o">.</span><span class="n">head</span><span class="p">(</span><span class="n">n</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>survived</th>
<th>pclass</th>
<th>sex</th>
<th>age</th>
<th>sibsp</th>
<th>parch</th>
<th>fare</th>
<th>embarked</th>
<th>class</th>
<th>who</th>
<th>adult_male</th>
<th>deck</th>
<th>embark_town</th>
<th>alive</th>
<th>alone</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>3</td>
<td>male</td>
<td>22</td>
<td>1</td>
<td>0</td>
<td>7.2500</td>
<td>S</td>
<td>Third</td>
<td>man</td>
<td>True</td>
<td>NaN</td>
<td>Southampton</td>
<td>no</td>
<td>False</td>
</tr>
<tr>
<th>1</th>
<td>1</td>
<td>1</td>
<td>female</td>
<td>38</td>
<td>1</td>
<td>0</td>
<td>71.2833</td>
<td>C</td>
<td>First</td>
<td>woman</td>
<td>False</td>
<td>C</td>
<td>Cherbourg</td>
<td>yes</td>
<td>False</td>
</tr>
<tr>
<th>2</th>
<td>1</td>
<td>3</td>
<td>female</td>
<td>26</td>
<td>0</td>
<td>0</td>
<td>7.9250</td>
<td>S</td>
<td>Third</td>
<td>woman</td>
<td>False</td>
<td>NaN</td>
<td>Southampton</td>
<td>yes</td>
<td>True</td>
</tr>
<tr>
<th>3</th>
<td>1</td>
<td>1</td>
<td>female</td>
<td>35</td>
<td>1</td>
<td>0</td>
<td>53.1000</td>
<td>S</td>
<td>First</td>
<td>woman</td>
<td>False</td>
<td>C</td>
<td>Southampton</td>
<td>yes</td>
<td>False</td>
</tr>
<tr>
<th>4</th>
<td>0</td>
<td>3</td>
<td>male</td>
<td>35</td>
<td>0</td>
<td>0</td>
<td>8.0500</td>
<td>S</td>
<td>Third</td>
<td>man</td>
<td>True</td>
<td>NaN</td>
<td>Southampton</td>
<td>no</td>
<td>True</td>
</tr>
<tr>
<th>5</th>
<td>0</td>
<td>3</td>
<td>male</td>
<td>NaN</td>
<td>0</td>
<td>0</td>
<td>8.4583</td>
<td>Q</td>
<td>Third</td>
<td>man</td>
<td>True</td>
<td>NaN</td>
<td>Queenstown</td>
<td>no</td>
<td>True</td>
</tr>
<tr>
<th>6</th>
<td>0</td>
<td>1</td>
<td>male</td>
<td>54</td>
<td>0</td>
<td>0</td>
<td>51.8625</td>
<td>S</td>
<td>First</td>
<td>man</td>
<td>True</td>
<td>E</td>
<td>Southampton</td>
<td>no</td>
<td>True</td>
</tr>
<tr>
<th>7</th>
<td>0</td>
<td>3</td>
<td>male</td>
<td>2</td>
<td>3</td>
<td>1</td>
<td>21.0750</td>
<td>S</td>
<td>Third</td>
<td>child</td>
<td>False</td>
<td>NaN</td>
<td>Southampton</td>
<td>no</td>
<td>False</td>
</tr>
<tr>
<th>8</th>
<td>1</td>
<td>3</td>
<td>female</td>
<td>27</td>
<td>0</td>
<td>2</td>
<td>11.1333</td>
<td>S</td>
<td>Third</td>
<td>woman</td>
<td>False</td>
<td>NaN</td>
<td>Southampton</td>
<td>yes</td>
<td>False</td>
</tr>
<tr>
<th>9</th>
<td>1</td>
<td>2</td>
<td>female</td>
<td>14</td>
<td>1</td>
<td>0</td>
<td>30.0708</td>
<td>C</td>
<td>Second</td>
<td>child</td>
<td>False</td>
<td>NaN</td>
<td>Cherbourg</td>
<td>yes</td>
<td>False</td>
</tr>
</tbody>
</table>
</div><p>One of the categorical variables in this dataset is <code class="docutils literal"><span class="pre">embark_town</span></code></p>
<p>Let’s plot the number of passengers departing from each town</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">ax</span> <span class="o">=</span> <span class="n">titanic</span><span class="o">.</span><span class="n">groupby</span><span class="p">([</span><span class="s">'embark_town'</span><span class="p">])[</span><span class="s">'age'</span><span class="p">]</span><span class="o">.</span><span class="n">count</span><span class="p">()</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">kind</span><span class="o">=</span><span class="s">'bar'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xticks</span><span class="p">(</span><span class="n">rotation</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">xlabel</span><span class="p">(</span><span class="s">'Departure Town'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s">'Passengers'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s">'Number of Passengers by Town of Departure'</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><matplotlib.text.Text at 0x1029b9a10>
</pre></div>
</div>
<img alt="_images/dirichlet-discrete_6_1.png" src="_images/dirichlet-discrete_6_1.png" />
<p>Let’s look at another example: the <a class="reference external" href="https://stat.ethz.ch/R-manual/R-devel/library/MASS/html/Cars93.html">cars93
dataset</a></p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">cars</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">read_csv</span><span class="p">(</span><span class="s">'https://vincentarelbundock.github.io/Rdatasets/csv/MASS/Cars93.csv'</span><span class="p">,</span> <span class="n">index_col</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">cars</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Manufacturer</th>
<th>Model</th>
<th>Type</th>
<th>Min.Price</th>
<th>Price</th>
<th>Max.Price</th>
<th>MPG.city</th>
<th>MPG.highway</th>
<th>AirBags</th>
<th>DriveTrain</th>
<th>...</th>
<th>Passengers</th>
<th>Length</th>
<th>Wheelbase</th>
<th>Width</th>
<th>Turn.circle</th>
<th>Rear.seat.room</th>
<th>Luggage.room</th>
<th>Weight</th>
<th>Origin</th>
<th>Make</th>
</tr>
</thead>
<tbody>
<tr>
<th>1</th>
<td>Acura</td>
<td>Integra</td>
<td>Small</td>
<td>12.9</td>
<td>15.9</td>
<td>18.8</td>
<td>25</td>
<td>31</td>
<td>None</td>
<td>Front</td>
<td>...</td>
<td>5</td>
<td>177</td>
<td>102</td>
<td>68</td>
<td>37</td>
<td>26.5</td>
<td>11</td>
<td>2705</td>
<td>non-USA</td>
<td>Acura Integra</td>
</tr>
<tr>
<th>2</th>
<td>Acura</td>
<td>Legend</td>
<td>Midsize</td>
<td>29.2</td>
<td>33.9</td>
<td>38.7</td>
<td>18</td>
<td>25</td>
<td>Driver & Passenger</td>
<td>Front</td>
<td>...</td>
<td>5</td>
<td>195</td>
<td>115</td>
<td>71</td>
<td>38</td>
<td>30.0</td>
<td>15</td>
<td>3560</td>
<td>non-USA</td>
<td>Acura Legend</td>
</tr>
<tr>
<th>3</th>
<td>Audi</td>
<td>90</td>
<td>Compact</td>
<td>25.9</td>
<td>29.1</td>
<td>32.3</td>
<td>20</td>
<td>26</td>
<td>Driver only</td>
<td>Front</td>
<td>...</td>
<td>5</td>
<td>180</td>
<td>102</td>
<td>67</td>
<td>37</td>
<td>28.0</td>
<td>14</td>
<td>3375</td>
<td>non-USA</td>
<td>Audi 90</td>
</tr>
<tr>
<th>4</th>
<td>Audi</td>
<td>100</td>
<td>Midsize</td>
<td>30.8</td>
<td>37.7</td>
<td>44.6</td>
<td>19</td>
<td>26</td>
<td>Driver & Passenger</td>
<td>Front</td>
<td>...</td>
<td>6</td>
<td>193</td>
<td>106</td>
<td>70</td>
<td>37</td>
<td>31.0</td>
<td>17</td>
<td>3405</td>
<td>non-USA</td>
<td>Audi 100</td>
</tr>
<tr>
<th>5</th>
<td>BMW</td>
<td>535i</td>
<td>Midsize</td>
<td>23.7</td>
<td>30.0</td>
<td>36.2</td>
<td>22</td>
<td>30</td>
<td>Driver only</td>
<td>Rear</td>
<td>...</td>
<td>4</td>
<td>186</td>
<td>109</td>
<td>69</td>
<td>39</td>
<td>27.0</td>
<td>13</td>
<td>3640</td>
<td>non-USA</td>
<td>BMW 535i</td>
</tr>
</tbody>
</table>
<p>5 rows × 27 columns</p>
</div><div class="code python highlight-python"><div class="highlight"><pre><span class="n">cars</span><span class="o">.</span><span class="n">ix</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre>Manufacturer Acura
Model Integra
Type Small
Min.Price 12.9
Price 15.9
Max.Price 18.8
MPG.city 25
MPG.highway 31
AirBags None
DriveTrain Front
Cylinders 4
EngineSize 1.8
Horsepower 140
RPM 6300
Rev.per.mile 2890
Man.trans.avail Yes
Fuel.tank.capacity 13.2
Passengers 5
Length 177
Wheelbase 102
Width 68
Turn.circle 37
Rear.seat.room 26.5
Luggage.room 11
Weight 2705
Origin non-USA
Make Acura Integra
Name: 1, dtype: object
</pre></div>
</div>
<p>This dataset has multiple categorical variables</p>
<p>Based on the description of the cars93 datatset, we’ll consider
<code class="docutils literal"><span class="pre">Manufacturer</span></code>, and <code class="docutils literal"><span class="pre">DriveTrain</span></code> to be categorical variables</p>
<p>Let’s plot <code class="docutils literal"><span class="pre">Manufacturer</span></code> and <code class="docutils literal"><span class="pre">DriveTrain</span></code></p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">cars</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">'Manufacturer'</span><span class="p">)[</span><span class="s">'Model'</span><span class="p">]</span><span class="o">.</span><span class="n">count</span><span class="p">()</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">kind</span><span class="o">=</span><span class="s">'bar'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s">'Cars'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s">'Number of Cars by Manufacturer'</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><matplotlib.text.Text at 0x114d9e6d0>
</pre></div>
</div>
<img alt="_images/dirichlet-discrete_12_1.png" src="_images/dirichlet-discrete_12_1.png" />
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">cars</span><span class="o">.</span><span class="n">groupby</span><span class="p">(</span><span class="s">'DriveTrain'</span><span class="p">)[</span><span class="s">'Model'</span><span class="p">]</span><span class="o">.</span><span class="n">count</span><span class="p">()</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">kind</span><span class="o">=</span><span class="s">'bar'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">ylabel</span><span class="p">(</span><span class="s">'Cars'</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s">'Number of Cars by Drive Train'</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><matplotlib.text.Text at 0x117554e50>
</pre></div>
</div>
<img alt="_images/dirichlet-discrete_13_1.png" src="_images/dirichlet-discrete_13_1.png" />
<p>If our categorical data has labels, we need to convert them to integer
id’s</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">col_2_ids</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">col</span><span class="p">):</span>
<span class="n">ids</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="n">col</span><span class="p">]</span><span class="o">.</span><span class="n">drop_duplicates</span><span class="p">()</span><span class="o">.</span><span class="n">sort</span><span class="p">(</span><span class="n">inplace</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span><span class="o">.</span><span class="n">reset_index</span><span class="p">(</span><span class="n">drop</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="n">ids</span><span class="o">.</span><span class="n">index</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s">'</span><span class="si">%s</span><span class="s">_ids'</span> <span class="o">%</span> <span class="n">col</span>
<span class="n">ids</span> <span class="o">=</span> <span class="n">ids</span><span class="o">.</span><span class="n">reset_index</span><span class="p">()</span>
<span class="n">df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">merge</span><span class="p">(</span><span class="n">df</span><span class="p">,</span> <span class="n">ids</span><span class="p">,</span> <span class="n">how</span><span class="o">=</span><span class="s">'left'</span><span class="p">)</span>
<span class="k">del</span> <span class="n">df</span><span class="p">[</span><span class="n">col</span><span class="p">]</span>
<span class="k">return</span> <span class="n">df</span>
</pre></div>
</div>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">cat_columns</span> <span class="o">=</span> <span class="p">[</span><span class="s">'Manufacturer'</span><span class="p">,</span> <span class="s">'DriveTrain'</span><span class="p">]</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">cat_columns</span><span class="p">:</span>
<span class="k">print</span> <span class="n">c</span>
<span class="n">cars</span> <span class="o">=</span> <span class="n">col_2_ids</span><span class="p">(</span><span class="n">cars</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-python"><div class="highlight"><pre><span class="n">Manufacturer</span>
<span class="n">DriveTrain</span>
</pre></div>
</div>
<div class="code python highlight-python"><div class="highlight"><pre><span class="n">cars</span><span class="p">[[</span><span class="s">'</span><span class="si">%s</span><span class="s">_ids'</span> <span class="o">%</span> <span class="n">c</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">cat_columns</span><span class="p">]]</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
<div style="max-height:1000px;max-width:1500px;overflow:auto;">
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>Manufacturer_ids</th>
<th>DriveTrain_ids</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>0</td>
<td>1</td>
</tr>
<tr>
<th>1</th>
<td>0</td>
<td>1</td>
</tr>
<tr>
<th>2</th>
<td>1</td>
<td>1</td>
</tr>
<tr>
<th>3</th>
<td>1</td>
<td>1</td>
</tr>
<tr>
<th>4</th>
<td>2</td>
<td>2</td>
</tr>
</tbody>
</table>
</div><p>Just as we model binary data with the beta Bernoulli distribution, we
can model categorical data with the Dirichlet discrete distribution</p>
<p>The beta Bernoulli distribution allows us to learn the underlying
probability, <span class="math">\(\theta\)</span>, of the binary random variable, <span class="math">\(x\)</span></p>
<div class="math">
\[P(x=1) =\theta\]</div>
<div class="math">
\[P(x=0) = 1-\theta\]</div>
<p>The Dirichlet discrete distribution extends the beta Bernoulli
distribution to the case in which <span class="math">\(x\)</span> can assume more than two
states</p>
<div class="math">
\[\forall i \in [0,1,...n] \hspace{2mm} P(x = i) = \theta_i\]</div>
<div class="math">
\[\sum_{i=0}^n \theta_i = 1\]</div>
<p>Again, the Dirichlet distribution takes advantage of the fact that the
Dirichlet distribution and the discrete distribution are conjugate. Note
that the discrete distriution is sometimes called the categorical
distribution or the multinomial distribution.</p>
<p>To import the Dirichlet discrete distribution call</p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">microscopes.models</span> <span class="kn">import</span> <span class="n">dd</span> <span class="k">as</span> <span class="n">dirichlet_discrete</span>
</pre></div>
</div>
<p>Then given the specific model we’d want we’d import</p>
<p><code class="docutils literal"><span class="pre">from</span> <span class="pre">microscopes.model_name.definition</span> <span class="pre">import</span> <span class="pre">model_definition</span></code></p>
<div class="code python highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">microscopes.irm.definition</span> <span class="kn">import</span> <span class="n">model_definition</span> <span class="k">as</span> <span class="n">irm_definition</span>
<span class="kn">from</span> <span class="nn">microscopes.mixture.definition</span> <span class="kn">import</span> <span class="n">model_definition</span> <span class="k">as</span> <span class="n">mm_definition</span>
</pre></div>
</div>
<p>See <code class="docutils literal"><span class="pre">Defining</span> <span class="pre">Your</span> <span class="pre">Model</span></code> to learn more about model definitions</p>
</div>
</div>
</div>
</div>
<!-- your html code here -->
<center> Datamicroscopes is developed by <a href="http://www.qadium.com">Qadium</a>, with funding from the <a href="http://www.darpa.mil">DARPA</a> <a href="http://www.darpa.mil/program/xdata">XDATA</a> program. Copyright Qadium 2015. </center>
</body>
</html>