-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabelrelations.html
1271 lines (1221 loc) · 68.2 KB
/
labelrelations.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 xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>scikit-multilearn: Multi-Label Classification in Python — Multi-Label Classification for Python</title>
<link rel="stylesheet" href="_static/" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></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>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="4. Using the MEKA wrapper" href="meka.html" />
<link rel="prev" title="5. How to select a classifier" href="modelselection.html" />
<meta content="True" name="HandheldFriendly">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@scikitml">
<meta name="twitter:title" content="scikit-multilearn">
<meta name="twitter:description" content="A native Python implementation of a variety of multi-label classification algorithms. Includes a Meka, MULAN, Weka wrapper. BSD licensed.">
<meta name="keywords" content="scikit-multilearn, multi-label classification, clustering, python, machinelearning">
<meta property="og:title" content="scikit-multilearn | Multi-label classification package for python" />
<meta property="og:description" content="A native Python implementation of a variety of multi-label classification algorithms. Includes a Meka, MULAN, Weka wrapper. BSD licensed." />
<!-- Compiled and minified CSS -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/css/materialize.min.css">
<link rel="stylesheet" href="/_static/custom.css">
<link href="https://fonts.googleapis.com/css?family=IBM+Plex+Mono|IBM+Plex+Sans|IBM+Plex+Sans+Condensed|IBM+Plex+Serif" rel="stylesheet">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous">
<!-- Compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-rc.2/js/materialize.min.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-51136636-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-51136636-1');
</script>
</head><body>
<div class="navbar-fixed">
<nav>
<div class="nav-wrapper container">
<a href="index.html" class="brand-logo">scikit-multilearn</a>
<ul id="nav-mobile" class="right hide-on-med-and-down">
<li><a href="userguide.html">User Guide</a></li>
<li><a href="api/skmultilearn.html">Reference</a></li>
<li><a href="https://github.com/scikit-multilearn/scikit-multilearn">Github</a></li>
<li><a href="https://pypi.org/project/scikit-multilearn">PyPi</a></li>
<li id="navbar-about"><a href="authors.html">About</a></li>
</ul>
</div>
</nav>
</div>
<!-- this is a replacement -->
<div class="container">
<div class="row">
<!-- Table of contents -->
<div class="col hide-on-small-only m3 xl2">
<div class="toc-wrapper">
<div style="height: 1px;">
<ul class="section table-of-contents">
<ul>
<li><a class="reference internal" href="#">1. Exploring Label Relations</a><ul>
<li><a class="reference internal" href="#Detecting-communities-in-Label-Relations-Graph">1.1. Detecting communities in Label Relations Graph</a><ul>
<li><a class="reference internal" href="#Building-a-Label-Graph">1.1.1. Building a Label Graph</a></li>
<li><a class="reference internal" href="#NetworkX">1.1.2. NetworkX</a></li>
</ul>
</li>
</ul>
</li>
<li><a class="reference internal" href="#Using-iGraph">2. Using iGraph</a></li>
<li><a class="reference internal" href="#Stochastic-Blockmodel-from-graph-tool">3. Stochastic Blockmodel from graph-tool</a><ul>
<li><a class="reference internal" href="#Using-scikit-learn-clusterers">3.1. Using scikit-learn clusterers</a></li>
<li><a class="reference internal" href="#Fixed-partition-based-on-expert-knowledge">3.2. Fixed partition based on expert knowledge</a></li>
</ul>
</li>
</ul>
</ul>
</div>
</div>
</div>
<div class="main-text section col s12 m8 offset-m1 xl9 offset-xl3">
<style>
/* CSS for nbsphinx extension */
/* remove conflicting styling from Sphinx themes */
div.nbinput,
div.nbinput div.prompt,
div.nbinput div.input_area,
div.nbinput div[class*=highlight],
div.nbinput div[class*=highlight] pre,
div.nboutput,
div.nbinput div.prompt,
div.nbinput div.output_area,
div.nboutput div[class*=highlight],
div.nboutput div[class*=highlight] pre {
background: none;
border: none;
padding: 0 0;
margin: 0;
box-shadow: none;
}
/* avoid gaps between output lines */
div.nboutput div[class*=highlight] pre {
line-height: normal;
}
/* input/output containers */
div.nbinput,
div.nboutput {
display: -webkit-flex;
display: flex;
align-items: flex-start;
margin: 0;
width: 100%;
}
@media (max-width: 540px) {
div.nbinput,
div.nboutput {
flex-direction: column;
}
}
/* input container */
div.nbinput {
padding-top: 5px;
}
/* last container */
div.nblast {
padding-bottom: 5px;
}
/* input prompt */
div.nbinput div.prompt pre {
color: #303F9F;
}
/* output prompt */
div.nboutput div.prompt pre {
color: #D84315;
}
/* all prompts */
div.nbinput div.prompt,
div.nboutput div.prompt {
min-width: 9ex;
padding-top: 0.4em;
padding-right: 0.4em;
text-align: right;
flex: 0;
}
@media (max-width: 540px) {
div.nbinput div.prompt,
div.nboutput div.prompt {
text-align: left;
padding: 0.4em;
}
div.nboutput div.prompt.empty {
padding: 0;
}
}
/* disable scrollbars on prompts */
div.nbinput div.prompt pre,
div.nboutput div.prompt pre {
overflow: hidden;
}
/* input/output area */
div.nbinput div.input_area,
div.nboutput div.output_area {
padding: 0.4em;
-webkit-flex: 1;
flex: 1;
overflow: auto;
}
@media (max-width: 540px) {
div.nbinput div.input_area,
div.nboutput div.output_area {
width: 100%;
}
}
/* input area */
div.nbinput div.input_area {
border: 1px solid #cfcfcf;
border-radius: 2px;
background: #f7f7f7;
}
/* override MathJax center alignment in output cells */
div.nboutput div[class*=MathJax] {
text-align: left !important;
}
/* override sphinx.ext.pngmath center alignment in output cells */
div.nboutput div.math p {
text-align: left;
}
/* standard error */
div.nboutput div.output_area.stderr {
background: #fdd;
}
/* ANSI colors */
.ansi-black-fg { color: #3E424D; }
.ansi-black-bg { background-color: #3E424D; }
.ansi-black-intense-fg { color: #282C36; }
.ansi-black-intense-bg { background-color: #282C36; }
.ansi-red-fg { color: #E75C58; }
.ansi-red-bg { background-color: #E75C58; }
.ansi-red-intense-fg { color: #B22B31; }
.ansi-red-intense-bg { background-color: #B22B31; }
.ansi-green-fg { color: #00A250; }
.ansi-green-bg { background-color: #00A250; }
.ansi-green-intense-fg { color: #007427; }
.ansi-green-intense-bg { background-color: #007427; }
.ansi-yellow-fg { color: #DDB62B; }
.ansi-yellow-bg { background-color: #DDB62B; }
.ansi-yellow-intense-fg { color: #B27D12; }
.ansi-yellow-intense-bg { background-color: #B27D12; }
.ansi-blue-fg { color: #208FFB; }
.ansi-blue-bg { background-color: #208FFB; }
.ansi-blue-intense-fg { color: #0065CA; }
.ansi-blue-intense-bg { background-color: #0065CA; }
.ansi-magenta-fg { color: #D160C4; }
.ansi-magenta-bg { background-color: #D160C4; }
.ansi-magenta-intense-fg { color: #A03196; }
.ansi-magenta-intense-bg { background-color: #A03196; }
.ansi-cyan-fg { color: #60C6C8; }
.ansi-cyan-bg { background-color: #60C6C8; }
.ansi-cyan-intense-fg { color: #258F8F; }
.ansi-cyan-intense-bg { background-color: #258F8F; }
.ansi-white-fg { color: #C5C1B4; }
.ansi-white-bg { background-color: #C5C1B4; }
.ansi-white-intense-fg { color: #A1A6B2; }
.ansi-white-intense-bg { background-color: #A1A6B2; }
.ansi-default-inverse-fg { color: #FFFFFF; }
.ansi-default-inverse-bg { background-color: #000000; }
.ansi-bold { font-weight: bold; }
.ansi-underline { text-decoration: underline; }
</style>
<div class="section" id="Exploring-Label-Relations">
<h1>1. Exploring Label Relations<a class="headerlink" href="#Exploring-Label-Relations" title="Permalink to this headline">¶</a></h1>
<p>Multi-label classification tends to have problems with overfitting and
underfitting classifiers when the label space is large, especially in
problem transformation approaches. A well known approach to remedy this
is to split the problem into subproblems with smaller label subsets to
improve the generalization quality.</p>
<p>Scikit-multilearn library is the first Python library to provide this
functionality, this will guide your through using different libraries
for label space division. Let’s start with loading up the well-cited
<code class="docutils literal notranslate"><span class="pre">emotions</span></code> dataset, that use throughout the User Guide:</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [1]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">skmultilearn.dataset</span> <span class="kn">import</span> <span class="n">load_dataset</span>
<span class="n">X_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">feature_names</span><span class="p">,</span> <span class="n">label_names</span> <span class="o">=</span> <span class="n">load_dataset</span><span class="p">(</span><span class="s1">'emotions'</span><span class="p">,</span> <span class="s1">'train'</span><span class="p">)</span>
<span class="n">X_test</span><span class="p">,</span> <span class="n">y_test</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">load_dataset</span><span class="p">(</span><span class="s1">'emotions'</span><span class="p">,</span> <span class="s1">'test'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
emotions:train - exists, not redownloading
emotions:test - exists, not redownloading
</pre></div></div>
</div>
<p>Label relationships can be exploited in a handful of ways:</p>
<ol class="arabic simple">
<li>inferring the label space division from the label assignment matrix
in the training set:<ul>
<li>through building a label graph and <a class="reference external" href="http://www.mdpi.com/1099-4300/18/8/282/htm">inferring community structure
of this graph</a>,
this can be facilitated with three network libraries in
scikit-multilearn: NetworkX (BSD), igraph (GPL) and graphtool
(GPL)</li>
<li>through using a traditional clustering approach from scikit-learn
to cluster label assignment vectors, ex. using k-means, this
usually required parameter estimation</li>
</ul>
</li>
<li>employing expert knowledge to divide the label space</li>
<li>random label space partitioning with methods like <a class="reference external" href="https://ieeexplore.ieee.org/document/5567103/">random k-label
sets</a></li>
</ol>
<p>In most cases these approaches are used with a Label Powerset problem
transformation classifier and a base multi-class classifier, for the
examples in this chapter we will use sklearn’s Gaussian Naive Bayes
classifier, but you can use whatever classifiers you in your ensembles.</p>
<p>Let’s go through the approaches:</p>
<div class="section" id="Detecting-communities-in-Label-Relations-Graph">
<h2>1.1. Detecting communities in Label Relations Graph<a class="headerlink" href="#Detecting-communities-in-Label-Relations-Graph" title="Permalink to this headline">¶</a></h2>
<p>Exploring label relations using the current methods of Network Science
is a new approach to improve classification results. This area is still
under research, both in terms of methods used for label space division
and in terms of what qualities should be represented in the Label
Relations Graph.</p>
<p>In scikit-multilearn classifying with label space division based on
label graphs requires three elements:</p>
<ul class="simple">
<li>selecting a graph builder, a class that constructs a graph based on
the label assignment matrix <code class="docutils literal notranslate"><span class="pre">y</span></code>, at the moment scikit-multilearn
provides one such graph builder, based on the notion of label
co-occurrence</li>
<li>selecting a Label Graph clusterer which employs community detection
methods from different sources to provide a label space clustering</li>
<li>selecting a classification approach, i.e. how to train and merge
results of classifiers, scikit-multilearn provides two approaches:<ul>
<li>a partitioning classifier which trains a classifier per label
cluster, assuming they are disjoint, and merges the results of
each subclassifier’s prediction</li>
<li>a majority voting classifier that trains a classifier per label
clusters, but if they overlap, it follows the decision of the
majority of subclassifiers concerning assigning the label or not</li>
</ul>
</li>
</ul>
<p>Let’s start with looking at the Label Graph builder.</p>
<div class="section" id="Building-a-Label-Graph">
<h3>1.1.1. Building a Label Graph<a class="headerlink" href="#Building-a-Label-Graph" title="Permalink to this headline">¶</a></h3>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [2]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">skmultilearn.cluster</span> <span class="kn">import</span> <span class="n">LabelCooccurrenceGraphBuilder</span>
</pre></div>
</div>
</div>
<p>This graph builder constructs a Label Graph based on the output matrix
where two label nodes are connected when at least one sample is labeled
with both of them. If the graph is weighted, the weight of an edge
between two label nodes is the number of samples labeled with these two
labels. Self-edge weights contain the number of samples with a given
label.</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [3]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">graph_builder</span> <span class="o">=</span> <span class="n">LabelCooccurrenceGraphBuilder</span><span class="p">(</span><span class="n">weighted</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">include_self_edges</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [4]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">edge_map</span> <span class="o">=</span> <span class="n">graph_builder</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">y_train</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"{} labels, {} edges"</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">label_names</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">edge_map</span><span class="p">)))</span>
<span class="k">print</span><span class="p">(</span><span class="n">edge_map</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<div class="highlight"><pre>
6 labels, 14 edges
{(1, 2): 58.0, (0, 1): 33.0, (1, 3): 6.0, (4, 5): 9.0, (1, 4): 1.0, (0, 2): 9.0, (1, 5): 6.0, (0, 5): 61.0, (0, 4): 4.0, (2, 3): 66.0, (2, 5): 5.0, (3, 4): 56.0, (2, 4): 60.0, (3, 5): 2.0}
</pre></div></div>
</div>
<p>The dictionary <code class="docutils literal notranslate"><span class="pre">edge_map</span></code> contains the adjacency matrix in
dictionary-of-keys format, each key is a label number tuple, weight is
the number of samples with the two labels assigned. Its values will be
used by all of the supported Label Graph Clusterers below:</p>
<ul class="simple">
<li>NetworkX</li>
<li>igraph</li>
<li>graph-tool</li>
</ul>
<p>All these clusterers take their names from the respected Python
graph/network libraries which they are using to infer community
structure and provide the label space clustering.</p>
</div>
<div class="section" id="NetworkX">
<h3>1.1.2. NetworkX<a class="headerlink" href="#NetworkX" title="Permalink to this headline">¶</a></h3>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [5]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">skmultilearn.cluster</span> <span class="kn">import</span> <span class="n">NetworkXLabelGraphClusterer</span>
<span class="c1"># we define a helper function for visualization purposes</span>
<span class="k">def</span> <span class="nf">to_membership_vector</span><span class="p">(</span><span class="n">partition</span><span class="p">):</span>
<span class="k">return</span> <span class="p">{</span>
<span class="n">member</span> <span class="p">:</span> <span class="n">partition_id</span>
<span class="k">for</span> <span class="n">partition_id</span><span class="p">,</span> <span class="n">members</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">partition</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="p">}</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [6]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">clusterer</span> <span class="o">=</span> <span class="n">NetworkXLabelGraphClusterer</span><span class="p">(</span><span class="n">graph_builder</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">'louvain'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [7]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">partition</span> <span class="o">=</span> <span class="n">clusterer</span><span class="o">.</span><span class="n">fit_predict</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>
<span class="n">partition</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>Out[7]:
</pre></div>
</div>
<div class="output_area highlight-none notranslate"><div class="highlight"><pre>
<span></span>array([[0, 1, 5],
[2, 3, 4]])
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [8]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">membership_vector</span> <span class="o">=</span> <span class="n">to_membership_vector</span><span class="p">(</span><span class="n">partition</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [9]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">networkx</span> <span class="kn">as</span> <span class="nn">nx</span>
<span class="n">names_dict</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">'-'</span><span class="p">,</span><span class="s1">'-</span><span class="se">\n</span><span class="s1">'</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">label_names</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [10]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span>
<span class="o">%</span><span class="k">matplotlib</span> inline
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [11]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">nx</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span>
<span class="n">clusterer</span><span class="o">.</span><span class="n">graph_</span><span class="p">,</span>
<span class="n">pos</span><span class="o">=</span><span class="n">nx</span><span class="o">.</span><span class="n">circular_layout</span><span class="p">(</span><span class="n">clusterer</span><span class="o">.</span><span class="n">graph_</span><span class="p">),</span>
<span class="n">labels</span><span class="o">=</span><span class="n">names_dict</span><span class="p">,</span>
<span class="n">with_labels</span> <span class="o">=</span> <span class="bp">True</span><span class="p">,</span>
<span class="n">width</span> <span class="o">=</span> <span class="p">[</span><span class="mi">10</span><span class="o">*</span><span class="n">x</span><span class="o">/</span><span class="n">y_train</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">clusterer</span><span class="o">.</span><span class="n">weights_</span><span class="p">[</span><span class="s1">'weight'</span><span class="p">]],</span>
<span class="n">node_color</span> <span class="o">=</span> <span class="p">[</span><span class="n">membership_vector</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">y_train</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])],</span>
<span class="n">cmap</span><span class="o">=</span><span class="n">plt</span><span class="o">.</span><span class="n">cm</span><span class="o">.</span><span class="n">Spectral</span><span class="p">,</span>
<span class="n">node_size</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span>
<span class="n">font_size</span><span class="o">=</span><span class="mi">14</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="_images/labelrelations_16_0.png" src="_images/labelrelations_16_0.png" />
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [12]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">skmultilearn.ensemble</span> <span class="kn">import</span> <span class="n">LabelSpacePartitioningClassifier</span>
<span class="kn">from</span> <span class="nn">skmultilearn.problem_transform</span> <span class="kn">import</span> <span class="n">LabelPowerset</span>
<span class="kn">from</span> <span class="nn">sklearn.naive_bayes</span> <span class="kn">import</span> <span class="n">GaussianNB</span>
<span class="kn">from</span> <span class="nn">sklearn.metrics</span> <span class="kn">import</span> <span class="n">accuracy_score</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [13]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">classifier</span> <span class="o">=</span> <span class="n">LabelSpacePartitioningClassifier</span><span class="p">(</span>
<span class="n">classifier</span> <span class="o">=</span> <span class="n">LabelPowerset</span><span class="p">(</span><span class="n">classifier</span><span class="o">=</span><span class="n">GaussianNB</span><span class="p">()),</span>
<span class="n">clusterer</span> <span class="o">=</span> <span class="n">clusterer</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [14]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></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>
<span class="n">prediction</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_test</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [15]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">accuracy_score</span><span class="p">(</span><span class="n">y_test</span><span class="p">,</span> <span class="n">prediction</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>Out[15]:
</pre></div>
</div>
<div class="output_area highlight-none notranslate"><div class="highlight"><pre>
<span></span>0.17821782178217821
</pre></div>
</div>
</div>
</div>
</div>
</div>
<div class="section" id="Using-iGraph">
<h1>2. Using iGraph<a class="headerlink" href="#Using-iGraph" title="Permalink to this headline">¶</a></h1>
<p>To use igraph with scikit-multilearn you need to install the igraph
python package:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$ pip install python-igraph
</pre></div>
</div>
<p>Do not install the <code class="docutils literal notranslate"><span class="pre">igraph</span></code> package which is not the correct
python-igraph library. Information about build requirements of
<code class="docutils literal notranslate"><span class="pre">python-igraph</span></code> can be found in the <a class="reference external" href="http://igraph.org/python/#pyinstall">library
documentation</a>.</p>
<p>Let’s load the python igraph library and scikit-multilearn’s
igraph-based clusterer.</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [16]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">skmultilearn.cluster</span> <span class="kn">import</span> <span class="n">IGraphLabelGraphClusterer</span>
<span class="kn">import</span> <span class="nn">igraph</span> <span class="kn">as</span> <span class="nn">ig</span>
</pre></div>
</div>
</div>
<p>Igraph provides a set of community detection methods, out of which the
following are supported:</p>
<table border="1" class="docutils">
<colgroup>
<col width="59%" />
<col width="41%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Method name string</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">fastgreedy</span></code></td>
<td>Detecting
communities
with largest
modularity
using
incremental
greedy search</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">infomap</span></code></td>
<td>Detecting
communities
through
information
flow
compressing
simulated via
random walks</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">label_propagation</span></code></td>
<td>Detecting
communities
from colorings
via multiple
label
propagation on
the graph</td>
</tr>
<tr class="row-odd"><td><a href="#id1"><span class="problematic" id="id2">``</span></a>leading_eigenvector
``</td>
<td>Detecting
communities
with largest
modularity
through
adjacency
matrix
eigenvectors</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">multilevel</span></code></td>
<td>Recursive
communitiy
detection with
largest
modularity
step by step
maximization</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">walktrap</span></code></td>
<td>Finding
communities by
trapping many
random walks</td>
</tr>
</tbody>
</table>
<p>Each of them denotes a <code class="docutils literal notranslate"><span class="pre">community_*</span></code> method of the Graph object, you
can read more about the methods in <a class="reference external" href="http://igraph.org/python/doc/igraph.Graph-class.html#community_fastgreedy">igraph
documentation</a>
and in comparison of their performance in <a class="reference external" href="http://www.mdpi.com/1099-4300/18/8/282/htm">multi-label
classification</a>.</p>
<p>Let’s start with detecting a community structure in the label
co-occurrence graph and visualizing it with igraph.</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [17]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">clusterer_igraph</span> <span class="o">=</span> <span class="n">IGraphLabelGraphClusterer</span><span class="p">(</span><span class="n">graph_builder</span><span class="o">=</span><span class="n">graph_builder</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="s1">'walktrap'</span><span class="p">)</span>
<span class="n">partition</span> <span class="o">=</span> <span class="n">clusterer_igraph</span><span class="o">.</span><span class="n">fit_predict</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>
<span class="n">partition</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>Out[17]:
</pre></div>
</div>
<div class="output_area highlight-none notranslate"><div class="highlight"><pre>
<span></span>array([[0, 5], [1, 2, 3, 4]], dtype=object)
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [18]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">colors</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'red'</span><span class="p">,</span> <span class="s1">'white'</span><span class="p">,</span> <span class="s1">'blue'</span><span class="p">]</span>
<span class="n">membership_vector</span> <span class="o">=</span> <span class="n">to_membership_vector</span><span class="p">(</span><span class="n">partition</span><span class="p">)</span>
<span class="n">visual_style</span> <span class="o">=</span> <span class="p">{</span>
<span class="s2">"vertex_size"</span> <span class="p">:</span> <span class="mi">20</span><span class="p">,</span>
<span class="s2">"vertex_label"</span><span class="p">:</span> <span class="p">[</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">label_names</span><span class="p">],</span>
<span class="s2">"edge_width"</span> <span class="p">:</span> <span class="p">[</span><span class="mi">10</span><span class="o">*</span><span class="n">x</span><span class="o">/</span><span class="n">y_train</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">clusterer_igraph</span><span class="o">.</span><span class="n">graph_</span><span class="o">.</span><span class="n">es</span><span class="p">[</span><span class="s1">'weight'</span><span class="p">]],</span>
<span class="s2">"vertex_color"</span><span class="p">:</span> <span class="p">[</span><span class="n">colors</span><span class="p">[</span><span class="n">membership_vector</span><span class="p">[</span><span class="n">i</span><span class="p">]]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">y_train</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">])],</span>
<span class="s2">"bbox"</span><span class="p">:</span> <span class="p">(</span><span class="mi">400</span><span class="p">,</span><span class="mi">400</span><span class="p">),</span>
<span class="s2">"margin"</span><span class="p">:</span> <span class="mi">80</span><span class="p">,</span>
<span class="s2">"layout"</span><span class="p">:</span> <span class="n">clusterer_igraph</span><span class="o">.</span><span class="n">graph_</span><span class="o">.</span><span class="n">layout_circle</span><span class="p">()</span>
<span class="p">}</span>
<span class="n">ig</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">clusterer_igraph</span><span class="o">.</span><span class="n">graph_</span><span class="p">,</span> <span class="o">**</span><span class="n">visual_style</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>Out[18]:
</pre></div>
</div>
<div class="output_area docutils container">
<img alt="_images/labelrelations_27_0.svg" src="_images/labelrelations_27_0.svg" /></div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [19]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">classifier</span> <span class="o">=</span> <span class="n">LabelSpacePartitioningClassifier</span><span class="p">(</span>
<span class="n">classifier</span> <span class="o">=</span> <span class="n">LabelPowerset</span><span class="p">(</span><span class="n">classifier</span><span class="o">=</span><span class="n">GaussianNB</span><span class="p">()),</span>
<span class="n">clusterer</span> <span class="o">=</span> <span class="n">clusterer_igraph</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>
<span class="n">prediction</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_test</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [20]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">accuracy_score</span><span class="p">(</span><span class="n">y_test</span><span class="p">,</span> <span class="n">prediction</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>Out[20]:
</pre></div>
</div>
<div class="output_area highlight-none notranslate"><div class="highlight"><pre>
<span></span>0.19306930693069307
</pre></div>
</div>
</div>
</div>
<div class="section" id="Stochastic-Blockmodel-from-graph-tool">
<h1>3. Stochastic Blockmodel from graph-tool<a class="headerlink" href="#Stochastic-Blockmodel-from-graph-tool" title="Permalink to this headline">¶</a></h1>
<p>Another approach to label space division is to fit a <a class="reference external" href="https://en.wikipedia.org/wiki/Stochastic_block_model">Stochastic Block
Model</a> to the
label graph. An efficient implementation of the Stochastic Block Model
in Python is provided by <a class="reference external" href="https://graph-tool.skewed.de">graphtool</a>.
Note that using graphtool incurs GPL requirements on your code.</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [21]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">skmultilearn.cluster.graphtool</span> <span class="kn">import</span> <span class="n">GraphToolLabelGraphClusterer</span><span class="p">,</span> <span class="n">StochasticBlockModel</span>
</pre></div>
</div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">StochasticBlockModel</span></code> class fits the model and specifies the
variant of SBM to be used, it can include:</p>
<ul class="simple">
<li>whether to use a nested blockmodel or not</li>
<li>whether to take degree correlation into account</li>
<li>whether to allow overlapping communities</li>
<li>how to model weights of label relationships</li>
</ul>
<p>Selecting these parameters efficiently for multi-label purposes is still
researched, but reading the <a class="reference external" href="https://graph-tool.skewed.de/static/doc/inference.html">inference
documentation</a>
in graphtool will give you an intuition what to choose.</p>
<p>As the emotions data set is small there is no reason to use the nested
model, we select the real-normal weight model as it is reasonable to
believe that label assignments come from an i.i.d source and should
follow some limit theorem.</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [22]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">model</span> <span class="o">=</span> <span class="n">StochasticBlockModel</span><span class="p">(</span><span class="n">nested</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">use_degree_correlation</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">allow_overlap</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">weight_model</span><span class="o">=</span><span class="s1">'real-normal'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [23]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">clusterer_graphtool</span> <span class="o">=</span> <span class="n">GraphToolLabelGraphClusterer</span><span class="p">(</span><span class="n">graph_builder</span><span class="o">=</span><span class="n">graph_builder</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="n">model</span><span class="p">)</span>
<span class="n">clusterer_graphtool</span><span class="o">.</span><span class="n">fit_predict</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>Out[23]:
</pre></div>
</div>
<div class="output_area highlight-none notranslate"><div class="highlight"><pre>
<span></span>array([[0, 1, 5],
[2, 3, 4]])
</pre></div>
</div>
</div>
<p>The above partition was generated by the model, let’s visualize it.</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [28]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">node_label</span> <span class="o">=</span> <span class="n">clusterer_graphtool</span><span class="o">.</span><span class="n">graph_</span><span class="o">.</span><span class="n">new_vertex_property</span><span class="p">(</span><span class="s2">"string"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">clusterer_graphtool</span><span class="o">.</span><span class="n">graph_</span><span class="o">.</span><span class="n">vertices</span><span class="p">()):</span>
<span class="n">node_label</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="n">label_names</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
<span class="n">clusterer_graphtool</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">model_</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="n">vertex_text</span><span class="o">=</span><span class="n">node_label</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="_images/labelrelations_37_0.png" src="_images/labelrelations_37_0.png" />
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>Out[28]:
</pre></div>
</div>
<div class="output_area highlight-none notranslate"><div class="highlight"><pre>
<span></span><PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x7fd284397b90, at 0x7fd283cf1450>
</pre></div>
</div>
</div>
<p>We can use this clusterer as an argument for the label space
partitioning classifier, as we did not enable overlapping communities:</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [29]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">classifier</span> <span class="o">=</span> <span class="n">LabelSpacePartitioningClassifier</span><span class="p">(</span>
<span class="n">classifier</span> <span class="o">=</span> <span class="n">LabelPowerset</span><span class="p">(</span><span class="n">classifier</span><span class="o">=</span><span class="n">GaussianNB</span><span class="p">()),</span>
<span class="n">clusterer</span> <span class="o">=</span> <span class="n">clusterer_graphtool</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>
<span class="n">prediction</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_test</span><span class="p">)</span>
<span class="n">accuracy_score</span><span class="p">(</span><span class="n">y_test</span><span class="p">,</span> <span class="n">prediction</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>Out[29]:
</pre></div>
</div>
<div class="output_area highlight-none notranslate"><div class="highlight"><pre>
<span></span>0.19306930693069307
</pre></div>
</div>
</div>
<p>Now let’s try to go with the same variant of the model, but now we allow
overlapping communities:</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [30]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">model</span> <span class="o">=</span> <span class="n">StochasticBlockModel</span><span class="p">(</span><span class="n">nested</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">use_degree_correlation</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">allow_overlap</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">weight_model</span><span class="o">=</span><span class="s1">'real-normal'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [55]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">clusterer_graphtool</span> <span class="o">=</span> <span class="n">GraphToolLabelGraphClusterer</span><span class="p">(</span><span class="n">graph_builder</span><span class="o">=</span><span class="n">graph_builder</span><span class="p">,</span> <span class="n">model</span><span class="o">=</span><span class="n">model</span><span class="p">)</span>
<span class="n">clusterer_graphtool</span><span class="o">.</span><span class="n">fit_predict</span><span class="p">(</span><span class="bp">None</span><span class="p">,</span> <span class="n">y_train</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>Out[55]:
</pre></div>
</div>
<div class="output_area highlight-none notranslate"><div class="highlight"><pre>
<span></span>array([[0, 1, 2, 5],
[2, 3, 4, 5]])
</pre></div>
</div>
</div>
<p>We have a division, note that we train the same number of classifiers as
in the partitioning case. Let’s visualize label membership likelihoods
alongside the division:</p>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [56]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">node_label</span> <span class="o">=</span> <span class="n">clusterer_graphtool</span><span class="o">.</span><span class="n">graph_</span><span class="o">.</span><span class="n">new_vertex_property</span><span class="p">(</span><span class="s2">"string"</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">clusterer_graphtool</span><span class="o">.</span><span class="n">graph_</span><span class="o">.</span><span class="n">vertices</span><span class="p">()):</span>
<span class="n">node_label</span><span class="p">[</span><span class="n">v</span><span class="p">]</span> <span class="o">=</span> <span class="n">label_names</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span>
<span class="n">clusterer_graphtool</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">model_</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="n">vertex_text</span><span class="o">=</span><span class="n">node_label</span><span class="p">,</span> <span class="n">vertex_text_color</span><span class="o">=</span><span class="s1">'black'</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nboutput docutils container">
<div class="prompt empty docutils container">
</div>
<div class="output_area docutils container">
<img alt="_images/labelrelations_44_0.png" src="_images/labelrelations_44_0.png" />
</div>
</div>
<div class="nboutput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>Out[56]:
</pre></div>
</div>
<div class="output_area highlight-none notranslate"><div class="highlight"><pre>
<span></span><PropertyMap object with key type 'Vertex' and value type 'vector<double>', for Graph 0x7fd283d14bd0, at 0x7fd259aa2a90>
</pre></div>
</div>
</div>
<p>We can now perform classification, but for it to work we now need to use
a classifier that can decide whether to assign a label if more than one
subclassifiers were making a decision about the label. We will use the
<code class="docutils literal notranslate"><span class="pre">MajorityVotingClassifier</span></code> which makes a decision if the majority of
classifiers decide to assign the label.</p>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [57]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="kn">from</span> <span class="nn">skmultilearn.ensemble.voting</span> <span class="kn">import</span> <span class="n">MajorityVotingClassifier</span>
</pre></div>
</div>
</div>
<div class="nbinput nblast docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>
<span></span>In [58]:
</pre></div>
</div>
<div class="input_area highlight-ipython2 notranslate"><div class="highlight"><pre>
<span></span><span class="n">classifier</span> <span class="o">=</span> <span class="n">MajorityVotingClassifier</span><span class="p">(</span>
<span class="n">classifier</span><span class="o">=</span><span class="n">LabelPowerset</span><span class="p">(</span><span class="n">classifier</span><span class="o">=</span><span class="n">GaussianNB</span><span class="p">()),</span>
<span class="n">clusterer</span><span class="o">=</span><span class="n">clusterer_graphtool</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>
<span class="n">prediction</span> <span class="o">=</span> <span class="n">classifier</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_test</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="nbinput docutils container">
<div class="prompt highlight-none notranslate"><div class="highlight"><pre>