-
Notifications
You must be signed in to change notification settings - Fork 4
/
index-1.html
2132 lines (1882 loc) · 141 KB
/
index-1.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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html prefix=" og: http://ogp.me/ns# article: http://ogp.me/ns/article# " vocab="http://ogp.me/ns" lang="en">
<head>
<meta charset="utf-8">
<meta name="description" content="The Chicago Python User Group's coding workshops for Python Project Night.">
<meta name="viewport" content="width=device-width">
<title>Python Project Night Challenges (old posts, page 1) | Python Project Night Challenges</title>
<link href="assets/css/custom.css" rel="stylesheet" type="text/css">
<meta name="theme-color" content="#18354c">
<meta name="generator" content="Nikola (getnikola.com)">
<link rel="alternate" type="application/rss+xml" title="RSS" href="rss.xml">
<link rel="canonical" href="https://chicagopython.github.io/index-1.html">
<link rel="icon" href="favicon.ico" sizes="16x16">
<link rel="manifest" href="site.webmanifest">
<link rel="mask-icon" href="safari-pinned-tab.svg" color="#1f91c2">
<meta name="msapplication-TileColor" content="#00aba9">
<meta name="theme-color" content="#cceeff">
<!-- favicons generated using http://realfavicongenerator.net/ --><link rel="prev" href="." type="text/html">
<!--[if lt IE 9]><script src="assets/js/html5shiv-printshiv.min.js"></script><![endif]--><link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.10.0-beta/katex.min.css" integrity="sha256-sI/DdD47R/Sa54XZDNFjRWlS+Dv8MC5xfkqQLRh0Jes=" crossorigin="anonymous">
</head>
<body>
<a href="#content" class="sr-only sr-only-focusable">Skip to main content</a>
<header id="header" class="hidden-print"><nav id="menu"><a href="https://chicagopython.github.io/" title="Python Project Night Challenges" rel="home">
<img src="assets/img/chipy-chipmunk.png" alt="Python Project Night Challenges" id="logo" aria-hidden>
Python Project Night Challenges
</a>
<ul>
<li><a href="categories/">Categories</a></li>
<li><a href="about/">About</a></li>
</ul></nav></header><main id="content"><div class="postindex">
<article class="h-entry post-text"><img src="assets/img/team-sales-business-meeting_4460x4460.jpg" alt="article thumbnail"><h3 class="p-name entry-title"><a href="posts/data-analysis-with-pandas/" class="u-url">Data Analysis with Pandas</a></h3>
<span class="metadata">
<time datetime="2019-03-17T12:08:50-05:00">March 17, 2019</time><i class="fas fa-tags"></i>
</span>
<!--
<div class="p-summary entry-summary">
<div><div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Data-Analysis-using-Pandas">Data Analysis using Pandas<a class="anchor-link" href="/posts/data-analysis-with-pandas/#Data-Analysis-using-Pandas">¶</a></h4><p>Pandas has become the defacto package for data analysis. In this workshop, we are going to use the basics of pandas to analyze the interests of today's group. We are going to use meetup.com's api and fetch the list of interests that are listed in each of our meetup.com profile. We will compute which interests are common, which are uncommon, and find out which of the two members have most similar interests. Lets get started by importing the essentials.</p>
<p>You would need meetup.com's python api and pandas installed.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">meetup.api</span>
<span class="kn">import</span> <span class="nn">pandas</span> <span class="kn">as</span> <span class="nn">pd</span>
<span class="kn">from</span> <span class="nn">IPython.display</span> <span class="kn">import</span> <span class="n">Image</span><span class="p">,</span> <span class="n">display</span><span class="p">,</span> <span class="n">HTML</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="kn">import</span> <span class="n">combinations</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Next we need your meetup.com API. You will find it <a href="https://secure.meetup.com/meetup_api/key/">https://secure.meetup.com/meetup_api/key/</a>
Also we need today's event id. The event id created under Chicago Pythonistas is <strong>233460758</strong> and that under Chicago Python user group is <strong>236205125</strong>. Use the one that has the higher number of RSVPs so that you get more data points. As an additional exercise, you might go for merging the two sets of RSVPs - but that's not needed for the workshop.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">API_KEY</span> <span class="o">=</span> <span class="s1">''</span>
<span class="n">event_id</span><span class="o">=</span><span class="s1">''</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The following function uses the api and loads the data into a pandas data frame. Note we are a bit sloppy both in style and how we load the data. In actual production code, we should add adequate logging with well-defined exceptions to indicate what's going wrong.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [114]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">def</span> <span class="nf">get_members</span><span class="p">(</span><span class="n">event_id</span><span class="p">):</span>
<span class="n">client</span> <span class="o">=</span> <span class="n">meetup</span><span class="o">.</span><span class="n">api</span><span class="o">.</span><span class="n">Client</span><span class="p">(</span><span class="n">API_KEY</span><span class="p">)</span>
<span class="n">rsvps</span><span class="o">=</span><span class="n">client</span><span class="o">.</span><span class="n">GetRsvps</span><span class="p">(</span><span class="n">event_id</span><span class="o">=</span><span class="n">event_id</span><span class="p">,</span> <span class="n">urlname</span><span class="o">=</span><span class="s1">'_ChiPy_'</span><span class="p">)</span>
<span class="n">member_id</span> <span class="o">=</span> <span class="s1">','</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">str</span><span class="p">(</span><span class="n">i</span><span class="p">[</span><span class="s1">'member'</span><span class="p">][</span><span class="s1">'member_id'</span><span class="p">])</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">rsvps</span><span class="o">.</span><span class="n">results</span><span class="p">])</span>
<span class="k">return</span> <span class="n">client</span><span class="o">.</span><span class="n">GetMembers</span><span class="p">(</span><span class="n">member_id</span><span class="o">=</span><span class="n">member_id</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">get_topics</span><span class="p">(</span><span class="n">members</span><span class="p">):</span>
<span class="n">topics</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
<span class="k">for</span> <span class="n">member</span> <span class="ow">in</span> <span class="n">members</span><span class="o">.</span><span class="n">results</span><span class="p">:</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">member</span><span class="p">[</span><span class="s1">'topics'</span><span class="p">]:</span>
<span class="n">topics</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="s1">'name'</span><span class="p">])</span>
<span class="k">except</span><span class="p">:</span>
<span class="k">pass</span>
<span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">topics</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">df_topics</span><span class="p">(</span><span class="n">event_id</span><span class="p">):</span>
<span class="n">members</span> <span class="o">=</span> <span class="n">get_members</span><span class="p">(</span><span class="n">event_id</span><span class="o">=</span><span class="n">event_id</span><span class="p">)</span>
<span class="n">topics</span> <span class="o">=</span> <span class="n">get_topics</span><span class="p">(</span><span class="n">members</span><span class="p">)</span>
<span class="n">columns</span><span class="o">=</span><span class="p">[</span><span class="s1">'name'</span><span class="p">,</span><span class="s1">'id'</span><span class="p">,</span><span class="s1">'thumb_link'</span><span class="p">]</span> <span class="o">+</span> <span class="n">topics</span>
<span class="n">data</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">member</span> <span class="ow">in</span> <span class="n">members</span><span class="o">.</span><span class="n">results</span><span class="p">:</span>
<span class="n">topic_vector</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">*</span><span class="nb">len</span><span class="p">(</span><span class="n">topics</span><span class="p">)</span>
<span class="k">for</span> <span class="n">topic</span> <span class="ow">in</span> <span class="n">member</span><span class="p">[</span><span class="s1">'topics'</span><span class="p">]:</span>
<span class="n">index</span> <span class="o">=</span> <span class="n">topics</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">topic</span><span class="p">[</span><span class="s1">'name'</span><span class="p">])</span>
<span class="n">topic_vector</span><span class="p">[</span><span class="n">index</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">member</span><span class="p">[</span><span class="s1">'name'</span><span class="p">],</span> <span class="n">member</span><span class="p">[</span><span class="s1">'id'</span><span class="p">],</span> <span class="n">member</span><span class="p">[</span><span class="s1">'photo'</span><span class="p">][</span><span class="s1">'thumb_link'</span><span class="p">]]</span> <span class="o">+</span> <span class="n">topic_vector</span><span class="p">)</span>
<span class="k">except</span><span class="p">:</span>
<span class="k">pass</span>
<span class="k">return</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">data</span><span class="o">=</span><span class="n">data</span><span class="p">,</span> <span class="n">columns</span><span class="o">=</span><span class="n">columns</span><span class="p">)</span>
<span class="c1">#df.to_csv('output.csv', sep=";")</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>So you need to call the df_topics function with the event id and it would give you back a pandas dataframe containing basic information of a member and along with all possible interests. If the member has indicated interest, that column will have a one, if not then the column will have a zero.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Load-data-from-meetup.com-into-a-dataframe-by-calling-df_topics-with-the-event-id-as-parameter">Load data from meetup.com into a dataframe by calling df_topics with the event id as parameter<a class="anchor-link" href="/posts/data-analysis-with-pandas/#Load-data-from-meetup.com-into-a-dataframe-by-calling-df_topics-with-the-event-id-as-parameter">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="What-does-the-first-and-last-10-rows-of-the-dataset-look-like?">What does the first and last 10 rows of the dataset look like?<a class="anchor-link" href="/posts/data-analysis-with-pandas/#What-does-the-first-and-last-10-rows-of-the-dataset-look-like?">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="What-are-the-column-names?">What are the column names?<a class="anchor-link" href="/posts/data-analysis-with-pandas/#What-are-the-column-names?">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Additional-Exercise:-Can-you-merge-the-two-data-for-two-events-into-one-data-frame-and-remove-the-dups?">Additional Exercise: Can you merge the two data for two events into one data frame and remove the dups?<a class="anchor-link" href="/posts/data-analysis-with-pandas/#Additional-Exercise:-Can-you-merge-the-two-data-for-two-events-into-one-data-frame-and-remove-the-dups?">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="What-are-the-top-10-most-common-interests-of-today’s-attendees?">What are the top 10 most common interests of today’s attendees?<a class="anchor-link" href="/posts/data-analysis-with-pandas/#What-are-the-top-10-most-common-interests-of-today%E2%80%99s-attendees?">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="What-is-the-third-most-popular-and-third-least-popular-topic-of-interest?-Are-there-ties?">What is the third most popular and third least popular topic of interest? Are there ties?<a class="anchor-link" href="/posts/data-analysis-with-pandas/#What-is-the-third-most-popular-and-third-least-popular-topic-of-interest?-Are-there-ties?">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Which-members-have-the-third-most-popular-interest?">Which members have the third most popular interest?<a class="anchor-link" href="/posts/data-analysis-with-pandas/#Which-members-have-the-third-most-popular-interest?">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Which-members-have-the-third-most-popular-interest?">Which members have the third most popular interest?<a class="anchor-link" href="/posts/data-analysis-with-pandas/#Which-members-have-the-third-most-popular-interest?">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Which-memebers-have-the-highest-number-of-topics-of-interest?">Which memebers have the highest number of topics of interest?<a class="anchor-link" href="/posts/data-analysis-with-pandas/#Which-memebers-have-the-highest-number-of-topics-of-interest?">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="What-is-the-average-number-of-topics-of-interest?">What is the average number of topics of interest?<a class="anchor-link" href="/posts/data-analysis-with-pandas/#What-is-the-average-number-of-topics-of-interest?">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Which-two-members-have-the-most-common-overlap-of-interests?">Which two members have the most common overlap of interests?<a class="anchor-link" href="/posts/data-analysis-with-pandas/#Which-two-members-have-the-most-common-overlap-of-interests?">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="How-many-members-are-there-who-have-no-overlaps-at-all?">How many members are there who have no overlaps at all?<a class="anchor-link" href="/posts/data-analysis-with-pandas/#How-many-members-are-there-who-have-no-overlaps-at-all?">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Given-a-member-which-other-member(s)-have-the-most-common-interests?">Given a member which other member(s) have the most common interests?<a class="anchor-link" href="/posts/data-analysis-with-pandas/#Given-a-member-which-other-member(s)-have-the-most-common-interests?">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div></div> <hr/>
</div>-->
</article><article class="h-entry post-text"><img src="assets/img/team-sales-business-meeting_4460x4460.jpg" alt="article thumbnail"><h3 class="p-name entry-title"><a href="posts/introduction-to-text-analysis-with-sklearn/" class="u-url">Introduction to Text Analysis with sklearn</a></h3>
<span class="metadata">
<time datetime="2019-03-17T11:43:40-05:00">March 17, 2019</time><i class="fas fa-tags"></i>
</span>
<!--
<div class="p-summary entry-summary">
<div><div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Introduction-to-pandas-and-sklearn">Introduction to pandas and sklearn<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Introduction-to-pandas-and-sklearn">¶</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Recommendation-System">Recommendation System<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Recommendation-System">¶</a></h4><p>We live in a world surrounded by recommendation systems - our shopping habbits, our reading habits, political opinions are heavily influenced by recommendation algorithms. So lets take a closer look at how to build a basic recommendation system.</p>
<p>Simply put a recommendation system learns from your previous behavior and tries to recommend items that are similar to your previous choices. While there are a multitude of approaches for building recommendation systems, we will take a simple approach that is easy to understand and has a reasonable performance.</p>
<p>For this exercise we will build a recommendation system that predicts which talks you'll enjoy at a conference - specifically our favorite conference Pycon!</p>
<h4 id="Before-you-proceed">Before you proceed<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Before-you-proceed">¶</a></h4><p>This project is still in alpha stage. Bugs, typos, spelling, grammar, terminologies - there's every scope of finding bugs. If you have found one - <a href="https://github.com/chicagopython/CodingWorkshops/issues/new">open an issue on github</a>. Pull Requests with corrections, fixes and enhancements will be received with open arms! Don't forget to add yourself to the <a href="https://github.com/chicagopython/CodingWorkshops/blob/master/README.md">list of contributors to this project</a>.</p>
<h5 id="Recommendation-for-Pycon-talks">Recommendation for Pycon talks<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Recommendation-for-Pycon-talks">¶</a></h5><p>Take a look at 2018 <a href="https://us.pycon.org/2018/schedule/">schedule</a>.
With 32 tuotorials, 12 sponsor workshops, 16 talks at the education summit, and 95 talks at the main conference - Pycon has a lot to offer. Reading through all the talk descriptions and filtering out the ones that you should go to is a tedious process.
Lets build a recommendation system that recommends talks from Pycon 2018, based on the ones that a person went to in 2017. This way the attendee does not waste any time deciding which talk to go to and spend more time making friends on the hallway track!</p>
<p>We will be using <a href="https://pandas.pydata.org/"><code>pandas</code></a> and <a href="http://scikit-learn.org/"><code>scikit-learn</code></a> to build the recommnedation system using the text description of talks.</p>
<h4 id="Definitions">Definitions<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Definitions">¶</a></h4><h5 id="Documents">Documents<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Documents">¶</a></h5><p>In our example the talk descriptions make up the documents</p>
<h5 id="Class">Class<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Class">¶</a></h5><p>We have two classes to classify our documents</p>
<ul>
<li>The talks that the attendee would like to see "in person". Denoted by 1</li>
<li>The talks that the attendee would watch "later online". Denoted by 0</li>
</ul>
<p>A talk description is labeled 0 would mean the user has chosen to watch it later and a label 1 would mean the user has chose to watch it in person.</p>
<h4 id="Supervised-Learning">Supervised Learning<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Supervised-Learning">¶</a></h4><p>In Supervised learning we inspect each observation in a given dataset and manually label them. These manually labeled data is used to construct a model that can predict the labels on new data. We will use a Supervised Learning technique called Support Vector Machines.</p>
<p>In unsupervised learning we do not need any manual labeling. The recommendation system finds the pattern in the data to build a model that can be used for recommendation.</p>
<h4 id="Dataset">Dataset<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Dataset">¶</a></h4><p>The dataset contains the talk description and speaker details from Pycon 2017 and 2018. All the 2017 talk data has been labeled by a user who has been to Pycon 2017.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Required-packages-installation">Required packages installation<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Required-packages-installation">¶</a></h4><p>The following packages are needed for this project. Execute the cell below to install them.</p>
<pre><code>numpy==1.14.2
pandas==0.22.0
python-dateutil==2.7.2
pytz==2018.4
scikit-learn==0.19.1
scipy==1.0.1
six==1.11.0
sklearn==0.0</code></pre>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">!</span>pip install -r requirements.txt
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Exercise-A:-Load-the-data">Exercise A: Load the data<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Exercise-A:-Load-the-data">¶</a></h4><p>The data directory contains the snapshot of one such user's labeling - lets load that up and start with our analysis.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">pandas</span> <span class="k">as</span> <span class="nn">pd</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="n">df</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="s1">'talks.csv'</span><span class="p">)</span>
<span class="n">df</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[3]:</div>
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>id</th>
<th>title</th>
<th>description</th>
<th>presenters</th>
<th>date_created</th>
<th>date_modified</th>
<th>location</th>
<th>talk_dt</th>
<th>year</th>
<th>label</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>5 ways to deploy your Python web app in 2017</td>
<td>You’ve built a fine Python web application and...</td>
<td>Andrew T. Baker</td>
<td>2018-04-19 00:59:20.151875</td>
<td>2018-04-19 00:59:20.151875</td>
<td>Portland Ballroom 252–253</td>
<td>2017-05-08 15:15:00.000000</td>
<td>2017</td>
<td>0.0</td>
</tr>
<tr>
<th>1</th>
<td>2</td>
<td>A gentle introduction to deep learning with Te...</td>
<td>Deep learning's explosion of spectacular resul...</td>
<td>Michelle Fullwood</td>
<td>2018-04-19 00:59:20.158338</td>
<td>2018-04-19 00:59:20.158338</td>
<td>Oregon Ballroom 203–204</td>
<td>2017-05-08 16:15:00.000000</td>
<td>2017</td>
<td>0.0</td>
</tr>
<tr>
<th>2</th>
<td>3</td>
<td>aiosmtpd - A better asyncio based SMTP server</td>
<td>smtpd.py has been in the standard library for ...</td>
<td>Barry Warsaw</td>
<td>2018-04-19 00:59:20.161866</td>
<td>2018-04-19 00:59:20.161866</td>
<td>Oregon Ballroom 203–204</td>
<td>2017-05-08 14:30:00.000000</td>
<td>2017</td>
<td>1.0</td>
</tr>
<tr>
<th>3</th>
<td>4</td>
<td>Algorithmic Music Generation</td>
<td>Music is mainly an artistic act of inspired cr...</td>
<td>Padmaja V Bhagwat</td>
<td>2018-04-19 00:59:20.165526</td>
<td>2018-04-19 00:59:20.165526</td>
<td>Portland Ballroom 251 & 258</td>
<td>2017-05-08 17:10:00.000000</td>
<td>2017</td>
<td>0.0</td>
</tr>
<tr>
<th>4</th>
<td>5</td>
<td>An Introduction to Reinforcement Learning</td>
<td>Reinforcement learning (RL) is a subfield of m...</td>
<td>Jessica Forde</td>
<td>2018-04-19 00:59:20.169075</td>
<td>2018-04-19 00:59:20.169075</td>
<td>Portland Ballroom 252–253</td>
<td>2017-05-08 13:40:00.000000</td>
<td>2017</td>
<td>0.0</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Here is a brief description of the interesting fields.</p>
<table>
<thead><tr>
<th>variable</th>
<th>description </th>
</tr>
</thead>
<tbody>
<tr>
<td><code>title</code></td>
<td>Title of the talk</td>
</tr>
<tr>
<td><code>description</code></td>
<td>Description of the talk</td>
</tr>
<tr>
<td><code>year</code></td>
<td>Is it a <code>2017</code> talk or <code>2018</code> </td>
</tr>
<tr>
<td><code>label</code></td>
<td><code>1</code> indicates the user preferred seeing the talk in person,<br> <code>0</code> indicates they would schedule it for later.</td>
</tr>
</tbody>
</table>
<p>Note all 2018 talks are set to 1. However they are only placeholders, and are not used in training the model. We will use 2017 data for training, and predict the labels on the 2018 talks.</p>
<p>Lets start by selecting the 2017 talk descriptions that were labeled by the user for watching in person.</p>
<div class="highlight"><pre><span></span><span class="n">df</span><span class="p">[(</span><span class="n">df</span><span class="o">.</span><span class="n">year</span><span class="o">==</span><span class="mi">2017</span><span class="p">)</span> <span class="o">&</span> <span class="p">(</span><span class="n">df</span><span class="o">.</span><span class="n">label</span><span class="o">==</span><span class="mi">1</span><span class="p">)][</span><span class="s1">'description'</span><span class="p">]</span>
</pre></div>
<p>Print the description of the talks that the user preferred watching in person. How many such talks are there?</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Exercise-1:-Exploring-the-dataset">Exercise 1: Exploring the dataset<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Exercise-1:-Exploring-the-dataset">¶</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Exercise-1.1:-Select-2017-talk-description-and-labels-from-the-Pandas-dataframe.-How-many-of-them-are-present?-Do-the-same-for-2018-talks.">Exercise 1.1: Select 2017 talk description and labels from the Pandas dataframe. How many of them are present? Do the same for 2018 talks.<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Exercise-1.1:-Select-2017-talk-description-and-labels-from-the-Pandas-dataframe.-How-many-of-them-are-present?-Do-the-same-for-2018-talks.">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The 2017 talks will be used for training and the 2018 talks will we used for predicting. Set the values of <code>year_labeled</code> and <code>year_predict</code> to appropriate values and print out the values of <code>description_labeled</code> and <code>description_predict</code>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">year_labeled</span><span class="o">=</span>
<span class="n">year_predict</span><span class="o">=</span>
<span class="n">description_labeled</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">year</span><span class="o">==</span><span class="n">year_labeled</span><span class="p">][</span><span class="s1">'description'</span><span class="p">]</span>
<span class="n">description_predict</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">year</span><span class="o">==</span><span class="n">year_predict</span><span class="p">][</span><span class="s1">'description'</span><span class="p">]</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Quick-Introduction-to-Text-Analysis">Quick Introduction to Text Analysis<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Quick-Introduction-to-Text-Analysis">¶</a></h3><p><img src="/posts/introduction-to-text-analysis-with-sklearn/text-analysis.jpg" alt="text-analysis"></p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Lets have a quick overview of text analysis. Our end goal is to train a machine learning algorithm by making it go through enough documents from each class to recognize the distingusihing characteristics in documents from a particular class.</p>
<ol>
<li><em>Labeling</em> - This is the step where the user (i.e. a human) reviews a set of documents and manually classifies them. For our problem, here a Pycon attendee is labeling a talk description from 2017 as "watch later"(0) or "watch now" (1).</li>
<li><em>Training/Testing split</em> - In order to test our algorithm, we split parts of our labeled data into training (used to train the algorithm) and testing set (used to test the algorithm).</li>
<li><em>Vectorization & feature extraction</em> - Since machine learning algorithms deal with numbers rather than words, we vectorize our documents - i.e. we split the documents into individual unique words and count the frequency of their occurance across documents. There are different data normalization is possible at this stage like stop words removal, <a href="https://spacy.io/api/lemmatizer">lemmatization</a> - but we will skip them for now. Each individual token occurrence frequency (normalized or not) is treated as a feature.</li>
<li><em>Model training</em> - This is where we build the model.</li>
<li><em>Model testing</em> - Here we test out the model to see how it is performing against label data as we subject it to the previously set aside test set.</li>
<li><em>Tweak and train</em> - If our measures are not satisfactory, we will change the parameters that define different aspects of the machine learning algorithm and we will train the model again.</li>
<li>Once satisfied with the results from the previous step, we are now ready to deploy the model and have new unlabled documents be classified by it.</li>
</ol>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Exercise-2:-Vectorize-and-Feature-Extraction">Exercise 2: Vectorize and Feature Extraction<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Exercise-2:-Vectorize-and-Feature-Extraction">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>In this step we build the feature set by tokenization, counting and normalization of the bi-grams from the text descriptions of the talk.</p>
<p><strong>tokenizing</strong> strings and giving an integer id for each possible token, for instance by using white-spaces and punctuation as token separators</p>
<p><strong>counting</strong> the occurrences of tokens in each document</p>
<p><strong>normalizing</strong> and weighting with diminishing importance tokens that occur in the majority of samples / documents</p>
<p>You can find more information on text feature extraction <a href="http://scikit-learn.org/stable/modules/feature_extraction.html#text-feature-extraction">here</a> and TfidfVectorizer <a href="http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html">here</a>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.feature_extraction.text</span> <span class="k">import</span> <span class="n">TfidfVectorizer</span>
<span class="n">vectorizer</span> <span class="o">=</span> <span class="n">TfidfVectorizer</span><span class="p">(</span><span class="n">ngram_range</span><span class="o">=</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span> <span class="n">stop_words</span><span class="o">=</span><span class="s2">"english"</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h5 id="Extra-Credit">Extra Credit<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Extra-Credit">¶</a></h5><p>Note that we are choosing default value on all parameters for <code>TfidfVectorizer</code>. While this is a starting point, for better results we would want to come back and tune them to reduce noise. You can try that after you have taken a first pass through all the exercises. You might consider using <a href="https://spacy.io/api/lemmatizer">spacy</a> to fine tune the input to <code>TfidfVectorizer</code>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Exercise-2.1-Fit_transform">Exercise 2.1 Fit_transform<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Exercise-2.1-Fit_transform">¶</a></h4><p>We will use the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.CountVectorizer.html#sklearn.feature_extraction.text.CountVectorizer.fit_transform">fit_transform</a> method to learn the vocabulary dictionary and return term-document matrix. What should be the input to <code>fit_transform</code>?</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [32]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">vectorized_text_labeled</span> <span class="o">=</span> <span class="n">vectorizer</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span> <span class="o">...</span> <span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Exercise-2.2-Inspect-the-vocabulary">Exercise 2.2 Inspect the vocabulary<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Exercise-2.2-Inspect-the-vocabulary">¶</a></h4><p>Take a look at the vocabulary dictionary that is accessible by calling <code>vocabulary_</code> on the <code>vectorizer</code>. The stopwords can be accessed using <code>stop_words_</code> attribute.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Use the <code>get_feature_names</code> function on the Tfidf <code>vectorizer</code> to get the features (terms).</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">occurrences</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">vectorized_text_labeled</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">))</span><span class="o">.</span><span class="n">ravel</span><span class="p">()</span>
<span class="n">terms</span> <span class="o">=</span> <span class="p">(</span> <span class="o">...</span> <span class="p">)</span>
<span class="n">counts_df</span> <span class="o">=</span> <span class="n">pd</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">({</span><span class="s1">'terms'</span><span class="p">:</span> <span class="n">terms</span><span class="p">,</span> <span class="s1">'occurrences'</span><span class="p">:</span> <span class="n">occurrences</span><span class="p">})</span><span class="o">.</span><span class="n">sort_values</span><span class="p">(</span><span class="s1">'occurrences'</span><span class="p">,</span> <span class="n">ascending</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">counts_df</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Exercise-2.3-Transform-documents-for-prediction-into-document-term-matrix">Exercise 2.3 Transform documents for prediction into document-term matrix<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Exercise-2.3-Transform-documents-for-prediction-into-document-term-matrix">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>For the data on which we will do our predictions, we will use the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.feature_extraction.text.TfidfVectorizer.html#sklearn.feature_extraction.text.TfidfVectorizer.transform">transform</a> method to get the document-term matrix.
We will use this later, once we have our model ready. What should be the input to the <code>transform</code> function?</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [29]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">vectorized_text_predict</span> <span class="o">=</span> <span class="n">vectorizer</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span> <span class="o">...</span> <span class="p">)</span>
<span class="n">vectorized_text_predict</span><span class="o">.</span><span class="n">toarray</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Exercise-3:-Split-into-training-and-testing-set">Exercise 3: Split into training and testing set<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Exercise-3:-Split-into-training-and-testing-set">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Next we split our data into training set and testing set. This allows us to do cross validation and avoid overfitting. Use the <code>train_test_split</code> method from <code>sklearn.model_selection</code> to split the <code>vectorized_text_labeled</code> into training and testing set with the test size as one third of the size (0.3) of the labeled.</p>
<p><a href="http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.train_test_split.html">Here</a> is the documentation for the function. The example usage should be helpful for understanding what <code>X_train, X_test, y_train, y_test</code> tuple represents.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">sklearn.model_selection</span> <span class="k">import</span> <span class="n">train_test_split</span>
<span class="n">labels</span> <span class="o">=</span> <span class="n">df</span><span class="p">[</span><span class="n">df</span><span class="o">.</span><span class="n">year</span> <span class="o">==</span> <span class="mi">2017</span><span class="p">][</span><span class="s1">'label'</span><span class="p">]</span>
<span class="n">test_size</span><span class="o">=</span> <span class="o">...</span>
<span class="n">X_train</span><span class="p">,</span> <span class="n">X_test</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">y_test</span> <span class="o">=</span> <span class="n">train_test_split</span><span class="p">(</span><span class="n">vectorized_text_labeled</span><span class="p">,</span> <span class="n">labels</span><span class="p">,</span> <span class="n">test_size</span><span class="o">=</span><span class="n">test_size</span><span class="p">,</span> <span class="n">random_state</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Exercise-3.1-Inspect-the-shape-of-each-output-of-train_test_split">Exercise 3.1 Inspect the shape of each output of train_test_split<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Exercise-3.1-Inspect-the-shape-of-each-output-of-train_test_split">¶</a></h4><p>For each of the output above, get the shape of the matrices.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [ ]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Exercise-4:-Train-the-model">Exercise 4: Train the model<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Exercise-4:-Train-the-model">¶</a></h4><p>Finally we get to the stage for training the model. We are going to use a linear <a href="http://scikit-learn.org/stable/modules/generated/sklearn.svm.LinearSVC.html">support vector classifier</a> and check its accuracy by using the <code>classification_report</code> function. Note that we have not done any parameter tuning done yet, so your model might not give you the best results. Like <code>TfIdfVectorizer</code> you can come back and tune these parameters later.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In [49]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">sklearn</span>
<span class="kn">from</span> <span class="nn">sklearn.svm</span> <span class="k">import</span> <span class="n">LinearSVC</span>
<span class="n">classifier</span> <span class="o">=</span> <span class="n">LinearSVC</span><span class="p">(</span><span class="n">verbose</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">classifier</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>[LibLinear]</pre>
</div>
</div>
<div class="output_area">
<div class="prompt output_prompt">Out[49]:</div>
<div class="output_text output_subarea output_execute_result">
<pre>LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
intercept_scaling=1, loss='squared_hinge', max_iter=1000,
multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
verbose=1)</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4 id="Exercise-5:-Evaluate-the-model">Exercise 5: Evaluate the model<a class="anchor-link" href="/posts/introduction-to-text-analysis-with-sklearn/#Exercise-5:-Evaluate-the-model">¶</a></h4>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Evaluate the model by using the the <code>classification_report</code> method from the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html">classification_report</a>. What are the values of precision, recall and f1-scores? They are defined <a href="http://scikit-learn.org/stable/auto_examples/model_selection/plot_precision_recall.html">here</a>.</p>
</div>