-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
793 lines (756 loc) · 72.1 KB
/
index.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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.1.132">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Daniel Loos">
<meta name="dcterms.date" content="2022-10-10">
<title>Predicting the music genre of spotify tracks using deep learning</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { color: #008000; } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { color: #008000; font-weight: bold; } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script src="index_files/libs/clipboard/clipboard.min.js"></script>
<script src="index_files/libs/quarto-html/quarto.js"></script>
<script src="index_files/libs/quarto-html/popper.min.js"></script>
<script src="index_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="index_files/libs/quarto-html/anchor.min.js"></script>
<link href="index_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="index_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="index_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="index_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="index_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script src="index_files/libs/htmlwidgets-1.5.4/htmlwidgets.js"></script>
<link href="index_files/libs/vis-9.1.0/vis-network.min.css" rel="stylesheet">
<script src="index_files/libs/vis-9.1.0/vis-network.min.js"></script>
<script src="index_files/libs/visNetwork-binding-2.1.2/visNetwork.js"></script>
</head>
<body>
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#abstract" id="toc-abstract" class="nav-link active" data-scroll-target="#abstract">Abstract</a></li>
<li><a href="#tech-stack" id="toc-tech-stack" class="nav-link" data-scroll-target="#tech-stack">Tech stack</a></li>
<li><a href="#etl-pipeline" id="toc-etl-pipeline" class="nav-link" data-scroll-target="#etl-pipeline">ETL pipeline</a></li>
<li><a href="#data-overview" id="toc-data-overview" class="nav-link" data-scroll-target="#data-overview">Data overview</a></li>
<li><a href="#summary-features-for-prediction" id="toc-summary-features-for-prediction" class="nav-link" data-scroll-target="#summary-features-for-prediction">Summary features for prediction</a></li>
<li><a href="#pitch-sequences-for-prediction" id="toc-pitch-sequences-for-prediction" class="nav-link" data-scroll-target="#pitch-sequences-for-prediction">Pitch sequences for prediction</a></li>
<li><a href="#evaluate-deep-learning-models" id="toc-evaluate-deep-learning-models" class="nav-link" data-scroll-target="#evaluate-deep-learning-models">Evaluate deep learning models</a></li>
<li><a href="#conclusion" id="toc-conclusion" class="nav-link" data-scroll-target="#conclusion">Conclusion</a></li>
</ul>
</nav>
</div>
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Predicting the music genre of spotify tracks using deep learning</h1>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p>Daniel Loos </p>
</div>
</div>
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">October 10, 2022</p>
</div>
</div>
</div>
</header>
<div class="cell" data-hash="index_cache/html/setup_6fd715ffe3259a739d71012a906c2d88">
</div>
<section id="abstract" class="level2">
<h2 class="anchored" data-anchor-id="abstract">Abstract</h2>
<p>Music genres are often composed of particular pitch patterns that can be used for prediction. The Spotify API provides features for entire tracks, e.g. its loudness or acousticness scores, as well as the sequence of the individual pitches (notes). Totaling 3600 tracks across techno, rock, jazz and classicsal generes were analyzed and used for both classical Machine Learning and Deep Learning modeling methods. Validation accuracy of both approaches were similar suggesting that more sophisticated network architectures are needed to increase the model performance.</p>
</section>
<section id="tech-stack" class="level2">
<h2 class="anchored" data-anchor-id="tech-stack">Tech stack</h2>
<ul>
<li><a href="https://keras.io/">keras</a> deep learning framework</li>
<li><a href="https://www.tensorflow.org/">Tensorflow</a> deep learning framework</li>
<li><a href="https://www.tidymodels.org/">tidymodels</a> machine learning framework</li>
<li><a href="https://www.tidyverse.org/">tidyverse</a> data wrangling</li>
<li><a href="https://books.ropensci.org/targets/">R targets</a> pipeline system</li>
<li><a href="https://www.rcharlie.com/spotifyr/">spotifyr</a> REST API calls</li>
<li><a href="https://quarto.org/">quarto</a> notebook documentation</li>
</ul>
<p>Keywords:</p>
<ul>
<li>Spatial data analysis</li>
<li>deep learning</li>
<li>CNN</li>
<li>LSTM</li>
<li>REST APIs</li>
</ul>
<p><a href="https://github.com/danlooo/spotify-datasci">This project on GitHub</a></p>
</section>
<section id="etl-pipeline" class="level2">
<h2 class="anchored" data-anchor-id="etl-pipeline">ETL pipeline</h2>
<p><a href="https://developer.spotify.com/community/showcase/spotify-audio-analysis/">Spotify audio analysis API</a> was queried to get summary features, e.g. danceability and accousticness, as well as the individual pitch sequences of 3600 music tracks. An R targets pipeline DAG was created to retrieve and transform the data allowing parallelization and caching:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-1_bfb5575a3d42c4cab50c51763a08c50b">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">source</span>(<span class="st">"_targets.R"</span>)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="fu">tar_load</span>(<span class="fu">c</span>(<span class="st">"terms"</span>, <span class="st">"track_audio_features"</span>, <span class="st">"selected_audio_features"</span>, <span class="st">"audio_analyses"</span>, <span class="st">"track_train_test_split"</span>, <span class="st">"track_searches"</span>, <span class="st">"track_pitches"</span>, <span class="st">"valid_tracks"</span>, <span class="st">"track_audio_analyses"</span>))</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a><span class="fu">tar_visnetwork</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.3.6 ✔ purrr 0.3.4
✔ tibble 3.1.8 ✔ dplyr 1.0.10
✔ tidyr 1.2.1 ✔ stringr 1.4.1
✔ readr 2.1.2 ✔ forcats 0.5.1
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
Attaching package: ‘spotifyr’
The following object is masked from ‘package:broom’:
tidy</code></pre>
</div>
<div class="cell-output-display">
<div id="htmlwidget-109197c45e245c71ed52" style="width:100%;height:464px;" class="visNetwork html-widget"></div>
<script type="application/json" data-for="htmlwidget-109197c45e245c71ed52">{"x":{"nodes":{"name":["evaluations","model_archs","model_data","model_fits","selected_audio_features","terms","track_audio_analyses","track_audio_features","track_features_file","track_ids","track_pitches","track_searches","track_searches_offsets","track_train_test_split","track_train_test_split_file","valid_tracks"],"type":["stem","stem","stem","pattern","stem","stem","pattern","pattern","stem","stem","pattern","pattern","stem","stem","stem","stem"],"status":["uptodate","uptodate","uptodate","uptodate","uptodate","uptodate","uptodate","uptodate","uptodate","uptodate","uptodate","uptodate","uptodate","uptodate","uptodate","uptodate"],"seconds":[0.76,0.001,312.615,157.616,0.002,0.001,541.597,5.769,0.201,0.001,103.001,16.661,0.002,0.035,0.033,1.414],"bytes":[364,62610914,62609975,60808,143,82,159560939,132104,1422851,63192,19882699,493034,105,64414,64414,61916],"branches":[null,null,null,4,null,null,3600,72,null,null,3600,72,null,null,null,null],"label":["evaluations","model_archs","model_data","model_fits","selected_audio_features","terms","track_audio_analyses","track_audio_features","track_features_file","track_ids","track_pitches","track_searches","track_searches_offsets","track_train_test_split","track_train_test_split_file","valid_tracks"],"color":["#354823","#354823","#354823","#354823","#354823","#354823","#354823","#354823","#354823","#354823","#354823","#354823","#354823","#354823","#354823","#354823"],"id":["evaluations","model_archs","model_data","model_fits","selected_audio_features","terms","track_audio_analyses","track_audio_features","track_features_file","track_ids","track_pitches","track_searches","track_searches_offsets","track_train_test_split","track_train_test_split_file","valid_tracks"],"level":[10,8,7,9,0,1,4,3,4,3,5,2,1,3,4,6],"shape":["dot","dot","dot","square","dot","dot","square","square","dot","dot","square","square","dot","dot","dot","dot"]},"edges":{"from":["track_ids","track_audio_features","track_searches","track_searches","track_pitches","track_searches","track_train_test_split","valid_tracks","terms","track_searches_offsets","track_searches","model_archs","model_data","model_archs","model_fits","track_audio_analyses","track_audio_features","track_pitches","track_searches","track_audio_analyses","track_train_test_split","track_searches","model_data"],"to":["track_audio_analyses","track_features_file","track_features_file","track_ids","model_data","model_data","model_data","model_data","track_searches","track_searches","track_audio_features","model_fits","model_fits","evaluations","evaluations","valid_tracks","valid_tracks","valid_tracks","valid_tracks","track_pitches","track_train_test_split_file","track_train_test_split","model_archs"],"arrows":["to","to","to","to","to","to","to","to","to","to","to","to","to","to","to","to","to","to","to","to","to","to","to"]},"nodesToDataframe":true,"edgesToDataframe":true,"options":{"width":"100%","height":"100%","nodes":{"shape":"dot","physics":false},"manipulation":{"enabled":false},"edges":{"smooth":{"type":"cubicBezier","forceDirection":"horizontal"}},"physics":{"stabilization":false},"interaction":{"zoomSpeed":1},"layout":{"hierarchical":{"enabled":true,"direction":"LR"}}},"groups":null,"width":null,"height":null,"idselection":{"enabled":false,"style":"width: 150px; height: 26px","useLabels":true,"main":"Select by id"},"byselection":{"enabled":false,"style":"width: 150px; height: 26px","multiple":false,"hideColor":"rgba(200,200,200,0.5)","highlight":false},"main":{"text":"","style":"font-family:Georgia, Times New Roman, Times, serif;font-weight:bold;font-size:20px;text-align:center;"},"submain":null,"footer":null,"background":"rgba(0, 0, 0, 0)","highlight":{"enabled":true,"hoverNearest":false,"degree":{"from":1,"to":1},"algorithm":"hierarchical","hideColor":"rgba(200,200,200,0.5)","labelOnly":true},"collapse":{"enabled":true,"fit":false,"resetHighlight":true,"clusterOptions":null,"keepCoord":true,"labelSuffix":"(cluster)"},"legend":{"width":0.2,"useGroups":false,"position":"right","ncol":1,"stepX":100,"stepY":100,"zoom":true,"nodes":{"label":["Up to date","Stem","Pattern"],"color":["#354823","#899DA4","#899DA4"],"shape":["dot","dot","square"]},"nodesToDataframe":true},"tooltipStay":300,"tooltipStyle":"position: fixed;visibility:hidden;padding: 5px;white-space: nowrap;font-family: verdana;font-size:14px;font-color:#000000;background-color: #f5f4ed;-moz-border-radius: 3px;-webkit-border-radius: 3px;border-radius: 3px;border: 1px solid #808074;box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2);"},"evals":[],"jsHooks":[]}</script>
</div>
</div>
</section>
<section id="data-overview" class="level2">
<h2 class="anchored" data-anchor-id="data-overview">Data overview</h2>
<p>Spotify was queried by the following terms. Up to 50 tracks per term were retrieved.</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-2_eb222c0ddfdd5c30ce64cc73475df33f">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>terms</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "techno" "rock" "jazz" "classical"</code></pre>
</div>
</div>
<p>Total number of tracks:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-3_135f83265c7df7e219d8036306c60425">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">nrow</span>(track_searches)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3600</code></pre>
</div>
</div>
<p>Tracks per term:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-4_749892612d4ff6edb8a982e1d3d9a381">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a>track_searches <span class="sc">|></span> <span class="fu">count</span>(term)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 2
term n
<chr> <int>
1 classical 900
2 jazz 900
3 rock 900
4 techno 900</code></pre>
</div>
</div>
<p>Features per track:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-5_bba61c6af056de076ed3eb45e2a0546f">
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a>tracks <span class="ot"><-</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> track_audio_features <span class="sc">|></span></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">left_join</span>(track_searches, <span class="at">by =</span> <span class="st">"id"</span>) <span class="sc">|></span></span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(id <span class="sc">%in%</span> valid_tracks) <span class="sc">|></span></span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">term =</span> <span class="fu">factor</span>(term))</span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(tracks)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] "danceability" "energy"
[3] "key" "loudness"
[5] "mode" "speechiness"
[7] "acousticness" "instrumentalness"
[9] "liveness" "valence"
[11] "tempo" "type.x"
[13] "id" "uri.x"
[15] "track_href" "analysis_url"
[17] "duration_ms.x" "time_signature"
[19] "term" "offset"
[21] "artists" "available_markets"
[23] "disc_number" "duration_ms.y"
[25] "explicit" "href"
[27] "is_local" "name"
[29] "popularity" "preview_url"
[31] "track_number" "type.y"
[33] "uri.y" "album.album_type"
[35] "album.artists" "album.available_markets"
[37] "album.href" "album.id"
[39] "album.images" "album.name"
[41] "album.release_date" "album.release_date_precision"
[43] "album.total_tracks" "album.type"
[45] "album.uri" "album.external_urls.spotify"
[47] "external_ids.isrc" "external_urls.spotify" </code></pre>
</div>
</div>
<p>Number of tracks after sanity checks:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-6_405611462743cecf3eca081ec033d92a">
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">nrow</span>(tracks)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3477</code></pre>
</div>
</div>
</section>
<section id="summary-features-for-prediction" class="level2">
<h2 class="anchored" data-anchor-id="summary-features-for-prediction">Summary features for prediction</h2>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-7_6cb504d521713b762fa4123a70683cde">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a>features <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"danceability"</span>, <span class="st">"acousticness"</span>)</span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a><span class="fu">tar_load</span>(track_train_test_split)</span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a>tracks_train <span class="ot"><-</span> tracks <span class="sc">|></span> <span class="fu">inner_join</span>(track_train_test_split) <span class="sc">|></span> <span class="fu">filter</span>(is_train)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Joining, by = "id"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a>tracks_train <span class="sc">|></span></span>
<span id="cb15-2"><a href="#cb15-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(term, features) <span class="sc">|></span></span>
<span id="cb15-3"><a href="#cb15-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="fu">across</span>(features, scale)) <span class="sc">|></span></span>
<span id="cb15-4"><a href="#cb15-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(features) <span class="sc">|></span></span>
<span id="cb15-5"><a href="#cb15-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(term, value)) <span class="sc">+</span></span>
<span id="cb15-6"><a href="#cb15-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_quasirandom</span>() <span class="sc">+</span></span>
<span id="cb15-7"><a href="#cb15-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>(<span class="at">outlier.size =</span> <span class="cn">NULL</span>, <span class="at">width =</span> <span class="fl">0.5</span>) <span class="sc">+</span></span>
<span id="cb15-8"><a href="#cb15-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span> name, <span class="at">scales =</span> <span class="st">"free"</span>) <span class="sc">+</span></span>
<span id="cb15-9"><a href="#cb15-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Note: Using an external vector in selections is ambiguous.
ℹ Use `all_of(features)` instead of `features` to silence this message.
ℹ See <https://tidyselect.r-lib.org/reference/faq-external-vector.html>.
This message is displayed once per session.</code></pre>
</div>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-7-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<ul>
<li>Techno songs are high in danceability and low in acousticness</li>
</ul>
<p>(Linear Euclidean) ordination biplot to show at all numeric features at once:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-8_70303c54a202c00691c439cc24b7b0eb">
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a>pca <span class="ot"><-</span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a> track_audio_features <span class="sc">|></span></span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">semi_join</span>(tracks_train) <span class="sc">|></span></span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">column_to_rownames</span>(<span class="st">"id"</span>) <span class="sc">|></span></span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(selected_audio_features) <span class="sc">|></span></span>
<span id="cb17-6"><a href="#cb17-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="fu">across</span>(<span class="fu">everything</span>(), scale)) <span class="sc">|></span></span>
<span id="cb17-7"><a href="#cb17-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(<span class="fu">if_any</span>(<span class="fu">everything</span>(), <span class="sc">~</span> <span class="sc">!</span> <span class="fu">is.na</span>(.x))) <span class="sc">|></span></span>
<span id="cb17-8"><a href="#cb17-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">prcomp</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Joining, by = c("danceability", "energy", "key", "loudness", "mode",
"speechiness", "acousticness", "instrumentalness", "liveness", "valence",
"tempo", "id", "track_href", "analysis_url", "time_signature")
Note: Using an external vector in selections is ambiguous. ℹ Use
`all_of(selected_audio_features)` instead of `selected_audio_features` to
silence this message. ℹ See
<https://tidyselect.r-lib.org/reference/faq-external-vector.html>. This message
is displayed once per session.</code></pre>
</div>
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>tracks_pca <span class="ot"><-</span></span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> pca<span class="sc">$</span>x <span class="sc">|></span></span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">as_tibble</span>(<span class="at">rownames =</span> <span class="st">"id"</span>) <span class="sc">|></span></span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">left_join</span>(track_audio_features, <span class="at">by =</span> <span class="st">"id"</span>) <span class="sc">|></span></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">left_join</span>(track_searches, <span class="at">by =</span> <span class="st">"id"</span>)</span>
<span id="cb19-6"><a href="#cb19-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-7"><a href="#cb19-7" aria-hidden="true" tabindex="-1"></a><span class="co"># get medoids</span></span>
<span id="cb19-8"><a href="#cb19-8" aria-hidden="true" tabindex="-1"></a>track_clusters <span class="ot"><-</span></span>
<span id="cb19-9"><a href="#cb19-9" aria-hidden="true" tabindex="-1"></a> tracks_pca <span class="sc">|></span></span>
<span id="cb19-10"><a href="#cb19-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(term) <span class="sc">|></span></span>
<span id="cb19-11"><a href="#cb19-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="fu">across</span>(<span class="fu">c</span>(PC1, PC2), median))</span>
<span id="cb19-12"><a href="#cb19-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb19-13"><a href="#cb19-13" aria-hidden="true" tabindex="-1"></a><span class="fu">tibble</span>() <span class="sc">|></span></span>
<span id="cb19-14"><a href="#cb19-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> PC1, <span class="at">y =</span> PC2, <span class="at">color =</span> group)) <span class="sc">+</span></span>
<span id="cb19-15"><a href="#cb19-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb19-16"><a href="#cb19-16" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> track_clusters <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">group =</span> <span class="st">"term"</span>),</span>
<span id="cb19-17"><a href="#cb19-17" aria-hidden="true" tabindex="-1"></a> <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">label =</span> term)</span>
<span id="cb19-18"><a href="#cb19-18" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb19-19"><a href="#cb19-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_text</span>(</span>
<span id="cb19-20"><a href="#cb19-20" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> pca<span class="sc">$</span>rotation <span class="sc">|></span> <span class="fu">as_tibble</span>(<span class="at">rownames =</span> <span class="st">"feature"</span>) <span class="sc">|></span> <span class="fu">mutate</span>(<span class="at">group =</span> <span class="st">"feature"</span>),</span>
<span id="cb19-21"><a href="#cb19-21" aria-hidden="true" tabindex="-1"></a> <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">label =</span> feature)</span>
<span id="cb19-22"><a href="#cb19-22" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-8-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>More detailed biplot:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-9_a9aaac1eeb0c19d43db845b92cf173d1">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="fu">tibble</span>() <span class="sc">|></span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> PC1, <span class="at">y =</span> PC2)) <span class="sc">+</span></span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(</span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> tracks_pca,</span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a> <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">color =</span> term),</span>
<span id="cb20-6"><a href="#cb20-6" aria-hidden="true" tabindex="-1"></a> <span class="at">alpha =</span> <span class="fl">0.3</span></span>
<span id="cb20-7"><a href="#cb20-7" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb20-8"><a href="#cb20-8" aria-hidden="true" tabindex="-1"></a> ggrepel<span class="sc">::</span><span class="fu">geom_label_repel</span>(</span>
<span id="cb20-9"><a href="#cb20-9" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> track_clusters,</span>
<span id="cb20-10"><a href="#cb20-10" aria-hidden="true" tabindex="-1"></a> <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">label =</span> term, <span class="at">color =</span> term)</span>
<span id="cb20-11"><a href="#cb20-11" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb20-12"><a href="#cb20-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">guides</span>(<span class="at">color =</span> <span class="cn">FALSE</span>) <span class="sc">+</span></span>
<span id="cb20-13"><a href="#cb20-13" aria-hidden="true" tabindex="-1"></a> ggnewscale<span class="sc">::</span><span class="fu">new_scale_color</span>() <span class="sc">+</span></span>
<span id="cb20-14"><a href="#cb20-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_segment</span>(</span>
<span id="cb20-15"><a href="#cb20-15" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> pca<span class="sc">$</span>rotation <span class="sc">|></span> <span class="fu">as_tibble</span>(<span class="at">rownames =</span> <span class="st">"feature"</span>),</span>
<span id="cb20-16"><a href="#cb20-16" aria-hidden="true" tabindex="-1"></a> <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">x =</span> <span class="dv">0</span>, <span class="at">y =</span> <span class="dv">0</span>, <span class="at">xend =</span> <span class="fu">max</span>(<span class="fu">abs</span>(pca<span class="sc">$</span>x[,<span class="dv">1</span>])) <span class="sc">*</span> PC1, <span class="at">yend =</span> <span class="fu">max</span>(<span class="fu">abs</span>(pca<span class="sc">$</span>x[,<span class="dv">2</span>])) <span class="sc">*</span> PC2),</span>
<span id="cb20-17"><a href="#cb20-17" aria-hidden="true" tabindex="-1"></a> <span class="at">arrow =</span> <span class="fu">arrow</span>()</span>
<span id="cb20-18"><a href="#cb20-18" aria-hidden="true" tabindex="-1"></a> ) <span class="sc">+</span></span>
<span id="cb20-19"><a href="#cb20-19" aria-hidden="true" tabindex="-1"></a> ggrepel<span class="sc">::</span><span class="fu">geom_label_repel</span>(</span>
<span id="cb20-20"><a href="#cb20-20" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> pca<span class="sc">$</span>rotation <span class="sc">|></span> <span class="fu">as_tibble</span>(<span class="at">rownames =</span> <span class="st">"feature"</span>),</span>
<span id="cb20-21"><a href="#cb20-21" aria-hidden="true" tabindex="-1"></a> <span class="at">mapping =</span> <span class="fu">aes</span>(<span class="at">label =</span> feature, <span class="at">x =</span> <span class="fu">max</span>(<span class="fu">abs</span>(pca<span class="sc">$</span>x[,<span class="dv">1</span>])) <span class="sc">*</span> PC1, <span class="at">y =</span> <span class="fu">max</span>(<span class="fu">abs</span>(pca<span class="sc">$</span>x[,<span class="dv">2</span>])) <span class="sc">*</span> PC2)</span>
<span id="cb20-22"><a href="#cb20-22" aria-hidden="true" tabindex="-1"></a> )</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
"none")` instead.</code></pre>
</div>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Sanity checks:</p>
<ul>
<li><p>classical track are associated with acousticness</p></li>
<li><p>rock and techno tracks are associated with loudness</p></li>
</ul>
<p>There is no clear separation between the genre clusters suggesting a complicated classification task.</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-10_df20b6cd44d28f2c9d247dea48bc932a">
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(pca)<span class="sc">$</span>importance[<span class="st">"Cumulative Proportion"</span>,<span class="st">"PC2"</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0.47009</code></pre>
</div>
</div>
<p>Almost half of the variance can be explained by the first principal components, motivating the prediction of the terms based on the features. These features were also significantly different across the terms:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-11_32f929c1b7ee144728bb0f8411d2373f">
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>features <span class="sc">|></span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">paste0</span>(<span class="at">collapse =</span> <span class="st">"+"</span>) <span class="sc">|></span></span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">paste0</span>(<span class="st">"~ term"</span>) <span class="sc">|></span></span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">lm</span>(<span class="at">data =</span> tracks) <span class="sc">|></span></span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">anova</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>Analysis of Variance Table
Response: danceability + acousticness
Df Sum Sq Mean Sq F value Pr(>F)
term 3 213.03 71.009 915.1 < 2.2e-16 ***
Residuals 3473 269.49 0.078
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1</code></pre>
</div>
</div>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-12_85df8d14cd7b77604dcb143dbcafd242">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>features <span class="sc">|></span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">paste0</span>(<span class="at">collapse =</span> <span class="st">"+"</span>) <span class="sc">|></span></span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">paste0</span>(<span class="st">"~ term"</span>) <span class="sc">|></span></span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">lm</span>(<span class="at">data =</span> tracks) <span class="sc">|></span></span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">lm</span>() <span class="sc">|></span></span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">summary</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Call:
lm(formula = lm(paste0(paste0(features, collapse = "+"), "~ term"),
data = tracks))
Residuals:
Min 1Q Median 3Q Max
-1.18660 -0.17087 0.00622 0.16813 0.96074
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 1.362783 0.009687 140.69 <2e-16 ***
termjazz -0.135915 0.013476 -10.09 <2e-16 ***
termrock -0.517331 0.013525 -38.25 <2e-16 ***
termtechno -0.588527 0.013436 -43.80 <2e-16 ***
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.2786 on 3473 degrees of freedom
Multiple R-squared: 0.4415, Adjusted R-squared: 0.441
F-statistic: 915.1 on 3 and 3473 DF, p-value: < 2.2e-16</code></pre>
</div>
</div>
<p>We use the same set of test samples throughout the entire analysis:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-13_4abee9d732d578d819a46aef5d9850c9">
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidymodels)</span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a><span class="fu">tar_load</span>(model_data)</span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a>train <span class="ot"><-</span></span>
<span id="cb28-5"><a href="#cb28-5" aria-hidden="true" tabindex="-1"></a> track_audio_features <span class="sc">|></span></span>
<span id="cb28-6"><a href="#cb28-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(id <span class="sc">%in%</span> <span class="fu">rownames</span>(model_data<span class="sc">$</span>train_y)) <span class="sc">|></span></span>
<span id="cb28-7"><a href="#cb28-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">left_join</span>(track_searches, <span class="at">by =</span> <span class="st">"id"</span>) <span class="sc">|></span></span>
<span id="cb28-8"><a href="#cb28-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">term =</span> term <span class="sc">|></span> <span class="fu">factor</span>()) <span class="sc">|></span></span>
<span id="cb28-9"><a href="#cb28-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(term, selected_audio_features)</span>
<span id="cb28-10"><a href="#cb28-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb28-11"><a href="#cb28-11" aria-hidden="true" tabindex="-1"></a>test <span class="ot"><-</span></span>
<span id="cb28-12"><a href="#cb28-12" aria-hidden="true" tabindex="-1"></a> track_audio_features <span class="sc">|></span></span>
<span id="cb28-13"><a href="#cb28-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(id <span class="sc">%in%</span> <span class="fu">rownames</span>(model_data<span class="sc">$</span>test_y)) <span class="sc">|></span></span>
<span id="cb28-14"><a href="#cb28-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">left_join</span>(track_searches, <span class="at">by =</span> <span class="st">"id"</span>) <span class="sc">|></span></span>
<span id="cb28-15"><a href="#cb28-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">term =</span> term <span class="sc">|></span> <span class="fu">factor</span>()) <span class="sc">|></span></span>
<span id="cb28-16"><a href="#cb28-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(term, selected_audio_features)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Let’s start with a (linear) Support Vector Machine (SVM):</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-14_3895e6a6a7ea2c2601131f34fb4d7011">
<div class="sourceCode cell-code" id="cb29"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" aria-hidden="true" tabindex="-1"></a><span class="fu">svm_linear</span>(<span class="at">mode =</span> <span class="st">"classification"</span>) <span class="sc">|></span></span>
<span id="cb29-2"><a href="#cb29-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">fit</span>(term <span class="sc">~</span> ., <span class="at">data =</span> train) <span class="sc">|></span><span class="co">#</span></span>
<span id="cb29-3"><a href="#cb29-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">predict</span>(test) <span class="sc">|></span></span>
<span id="cb29-4"><a href="#cb29-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">bind_cols</span>(test) <span class="sc">|></span></span>
<span id="cb29-5"><a href="#cb29-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="fu">across</span>(<span class="fu">c</span>(<span class="st">"term"</span>, <span class="st">".pred_class"</span>), <span class="sc">~</span> <span class="fu">factor</span>(.x, <span class="at">levels =</span> test<span class="sc">$</span>term <span class="sc">|></span> <span class="fu">unique</span>()))) <span class="sc">|></span></span>
<span id="cb29-6"><a href="#cb29-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">accuracy</span>(<span class="at">truth =</span> term, <span class="at">estimate =</span> .pred_class)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 accuracy multiclass 0.715</code></pre>
</div>
</div>
<p>A (non-linear) random forest showed similar performance:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-15_9e0da2d371f9e41795ae9323a755ed15">
<div class="sourceCode cell-code" id="cb31"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" aria-hidden="true" tabindex="-1"></a><span class="fu">rand_forest</span>(<span class="at">mode =</span> <span class="st">"classification"</span>) <span class="sc">|></span></span>
<span id="cb31-2"><a href="#cb31-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">fit</span>(term <span class="sc">~</span> ., <span class="at">data =</span> train) <span class="sc">|></span></span>
<span id="cb31-3"><a href="#cb31-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">predict</span>(test) <span class="sc">|></span></span>
<span id="cb31-4"><a href="#cb31-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">bind_cols</span>(test) <span class="sc">|></span></span>
<span id="cb31-5"><a href="#cb31-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="fu">across</span>(<span class="fu">c</span>(<span class="st">"term"</span>, <span class="st">".pred_class"</span>), <span class="sc">~</span> <span class="fu">factor</span>(.x, <span class="at">levels =</span> test<span class="sc">$</span>term <span class="sc">|></span> <span class="fu">unique</span>()))) <span class="sc">|></span></span>
<span id="cb31-6"><a href="#cb31-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">accuracy</span>(<span class="at">truth =</span> term, <span class="at">estimate =</span> .pred_class)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 1 × 3
.metric .estimator .estimate
<chr> <chr> <dbl>
1 accuracy multiclass 0.791</code></pre>
</div>
</div>
<p>The test accuracy was very high in general. Can it even be improved using the individual pitch sequences instead of relying on just a few summary features describing the entire track?</p>
</section>
<section id="pitch-sequences-for-prediction" class="level2">
<h2 class="anchored" data-anchor-id="pitch-sequences-for-prediction">Pitch sequences for prediction</h2>
<p>Some features are highly correlated, suggesting redundancy, e.g. :</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-16_f3d73077b73be58d5e376846b0b1ed9b">
<div class="sourceCode cell-code" id="cb33"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" aria-hidden="true" tabindex="-1"></a>tracks <span class="sc">|></span></span>
<span id="cb33-2"><a href="#cb33-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(danceability, loudness)) <span class="sc">+</span></span>
<span id="cb33-3"><a href="#cb33-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb33-4"><a href="#cb33-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">stat_smooth</span>(<span class="at">method =</span> <span class="st">"lm"</span>) <span class="sc">+</span></span>
<span id="cb33-5"><a href="#cb33-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">stat_cor</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-16-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Indeed, lots of features were significantly correlated after FDR adjustment:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-17_be56a34b23e2a20e300bceb92854ffd3">
<div class="sourceCode cell-code" id="cb35"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb35-1"><a href="#cb35-1" aria-hidden="true" tabindex="-1"></a>tracks <span class="sc">|></span></span>
<span id="cb35-2"><a href="#cb35-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(selected_audio_features) <span class="sc">|></span></span>
<span id="cb35-3"><a href="#cb35-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">as.matrix</span>() <span class="sc">|></span></span>
<span id="cb35-4"><a href="#cb35-4" aria-hidden="true" tabindex="-1"></a> Hmisc<span class="sc">::</span><span class="fu">rcorr</span>() <span class="sc">|></span></span>
<span id="cb35-5"><a href="#cb35-5" aria-hidden="true" tabindex="-1"></a> broom<span class="sc">::</span><span class="fu">tidy</span>() <span class="sc">|></span></span>
<span id="cb35-6"><a href="#cb35-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">ungroup</span>() <span class="sc">|></span></span>
<span id="cb35-7"><a href="#cb35-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">q.value =</span> p.value <span class="sc">|></span> <span class="fu">p.adjust</span>(<span class="at">method =</span> <span class="st">"fdr"</span>)) <span class="sc">|></span></span>
<span id="cb35-8"><a href="#cb35-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(q.value <span class="sc"><</span> <span class="fl">0.05</span> <span class="sc">&</span> <span class="fu">abs</span>(estimate) <span class="sc">></span> <span class="fl">0.2</span>) <span class="sc">|></span></span>
<span id="cb35-9"><a href="#cb35-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="sc">-</span><span class="fu">abs</span>(estimate)) <span class="sc">|></span></span>
<span id="cb35-10"><a href="#cb35-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">unite</span>(<span class="at">col =</span> comparision, column1, column2, <span class="at">sep =</span> <span class="st">" vs. "</span>) <span class="sc">|></span></span>
<span id="cb35-11"><a href="#cb35-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">head</span>(<span class="dv">10</span>) <span class="sc">|></span></span>
<span id="cb35-12"><a href="#cb35-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(comparision, estimate)) <span class="sc">+</span></span>
<span id="cb35-13"><a href="#cb35-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>() <span class="sc">+</span></span>
<span id="cb35-14"><a href="#cb35-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span></span>
<span id="cb35-15"><a href="#cb35-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">y =</span> <span class="st">"Pearson correlation"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-17-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Music is composed of shorter and longer patterns. We can make use of the temporal property by doing convolutions on the time axis while using loudness of pitch frequencies as features.</p>
<p><a href="https://developer.spotify.com/community/showcase/spotify-audio-analysis/">Spotify audio analysis</a> separates the track into many segments and calculates the loudness for each of the 12 pitches (half steps) of the scale.</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-18_8fc0e30c1d8d6dfeb326e099ecc49c30">
<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a>track_audio_analyses<span class="sc">$</span>audio_analysis[[<span class="dv">1</span>]]<span class="sc">$</span>segments<span class="sc">$</span>pitches[<span class="dv">1</span>][[<span class="dv">1</span>]]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 0.366 0.128 0.311 0.106 0.412 1.000 0.886 0.633 0.333 0.122 0.213 0.473</code></pre>
</div>
</div>
<p>These are spectrograms of a subset of tracks representing the feature space for deep learning:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-19_0f5a85191d17850c3f6f811cbfa446d0">
<div class="sourceCode cell-code" id="cb38"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a>track_pitches <span class="sc">|></span></span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">left_join</span>(track_searches) <span class="sc">|></span></span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">sample_frac</span>(<span class="fl">0.01</span>) <span class="sc">|></span></span>
<span id="cb38-4"><a href="#cb38-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(id, term, pitches) <span class="sc">|></span></span>
<span id="cb38-5"><a href="#cb38-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">unnest</span>(pitches) <span class="sc">|></span></span>
<span id="cb38-6"><a href="#cb38-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(id) <span class="sc">|></span></span>
<span id="cb38-7"><a href="#cb38-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">segment =</span> <span class="fu">row_number</span>()) <span class="sc">|></span></span>
<span id="cb38-8"><a href="#cb38-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="fu">starts_with</span>(<span class="st">"V"</span>), <span class="at">names_to =</span> <span class="st">"pitch_name"</span>, <span class="at">values_to =</span> <span class="st">"pitch"</span>) <span class="sc">|></span></span>
<span id="cb38-9"><a href="#cb38-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">pitch_name =</span> pitch_name <span class="sc">|></span> <span class="fu">str_extract</span>(<span class="st">"[0-9]+"</span>) <span class="sc">|></span> <span class="fu">as.numeric</span>() <span class="sc">|></span> <span class="fu">factor</span>()) <span class="sc">|></span></span>
<span id="cb38-10"><a href="#cb38-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(term, segment, pitch_name) <span class="sc">|></span></span>
<span id="cb38-11"><a href="#cb38-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="at">pitch =</span> <span class="fu">median</span>(pitch)) <span class="sc">|></span></span>
<span id="cb38-12"><a href="#cb38-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-13"><a href="#cb38-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(segment, pitch_name)) <span class="sc">+</span></span>
<span id="cb38-14"><a href="#cb38-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_tile</span>(<span class="fu">aes</span>(<span class="at">fill =</span> pitch)) <span class="sc">+</span></span>
<span id="cb38-15"><a href="#cb38-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>term, <span class="at">ncol =</span> <span class="dv">1</span>) <span class="sc">+</span></span>
<span id="cb38-16"><a href="#cb38-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_fill_viridis_c</span>() <span class="sc">+</span></span>
<span id="cb38-17"><a href="#cb38-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_x_continuous</span>(<span class="at">limits =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">800</span>), <span class="at">expand =</span> <span class="fu">c</span>(<span class="dv">0</span>, <span class="dv">0</span>)) <span class="sc">+</span></span>
<span id="cb38-18"><a href="#cb38-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Time (segment)"</span>, <span class="at">y =</span> <span class="st">"Pitch (semitone)"</span>, <span class="at">fill =</span> <span class="st">"Median loudness"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Joining, by = "id"
`summarise()` has grouped output by 'term', 'segment'. You can override using
the `.groups` argument.</code></pre>
</div>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-19-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>On average, techno tracks uses a variety of different niotes across the time of the track, whereas classical tracks mostly use a few particular notes.</p>
<p>Let’s define some model architectures for deep learning:</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-20_205c430997647f54a5816dd4f6031e11">
<div class="sourceCode cell-code" id="cb40"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a><span class="fu">tar_load</span>(model_archs)</span>
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb40-3"><a href="#cb40-3" aria-hidden="true" tabindex="-1"></a>model_archs<span class="sc">$</span><span class="fu">base</span>() <span class="sc">|></span> <span class="fu">plot</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Loaded Tensorflow version 2.9.1</code></pre>
</div>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-20-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>This base model does not utilize the spatialness of the data and is used for comparison.</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-21_becd1eb83afe2c47ed22c91f9f7a67b9">
<div class="sourceCode cell-code" id="cb42"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>model_archs<span class="sc">$</span><span class="fu">cnn1</span>() <span class="sc">|></span> <span class="fu">plot</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-21-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>This is a <em>sequential</em> Convolutional Neural Network (CNN).</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-22_6dd17bbdaf644bcfb2fb2e60222b401a">
<div class="sourceCode cell-code" id="cb43"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb43-1"><a href="#cb43-1" aria-hidden="true" tabindex="-1"></a>model_archs<span class="sc">$</span><span class="fu">cnn2</span>() <span class="sc">|></span> <span class="fu">plot</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-22-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>This is a <em>non sequential</em> Convolutional Neural Network (CNN). The idea behind this model is that both short and long pitch patterns can be directly used for final prediction.</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-23_fff9beb76422c7dad23228a029fe44c6">
<div class="sourceCode cell-code" id="cb44"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a>model_archs<span class="sc">$</span><span class="fu">lstm</span>() <span class="sc">|></span> <span class="fu">plot</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-23-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Long Short-Term Memory (LSTM) networks view time as a one directional spatial feature, whereas one can go in both directions with CNNs. This makes sense for time series data, like the pitch sequences.</p>
</section>
<section id="evaluate-deep-learning-models" class="level2">
<h2 class="anchored" data-anchor-id="evaluate-deep-learning-models">Evaluate deep learning models</h2>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-24_db25d63c0d516c8fe0a39d76f8e098e3">
<div class="sourceCode cell-code" id="cb45"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb45-1"><a href="#cb45-1" aria-hidden="true" tabindex="-1"></a><span class="fu">tar_load</span>(evaluations)</span>
<span id="cb45-2"><a href="#cb45-2" aria-hidden="true" tabindex="-1"></a>evaluations</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code># A tibble: 4 × 7
name model epoch accuracy loss val_accuracy val_loss
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 cnn1 model_fits_a4330c15 93 0.748 0.636 0.731 0.725
2 cnn2 model_fits_fe0655a7 19 0.838 0.467 0.698 0.820
3 base model_fits_90ac26ad 81 0.559 1.02 0.557 1.08
4 lstm model_fits_1a00e901 14 0.494 1.10 0.532 1.11 </code></pre>
</div>
</div>
<p>All models outperformed the random guess with an expected accuracy of 25%. The simple CNN1 had the highest accuracy in the validation set.</p>
<div class="cell" data-hash="index_cache/html/unnamed-chunk-25_02bd1764b0892c4deacc02e7f93f8557">
<div class="sourceCode cell-code" id="cb47"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb47-1"><a href="#cb47-1" aria-hidden="true" tabindex="-1"></a>evaluations <span class="sc">|></span></span>
<span id="cb47-2"><a href="#cb47-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(name, model) <span class="sc">|></span></span>
<span id="cb47-3"><a href="#cb47-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">history =</span> model <span class="sc">|></span> <span class="fu">map</span>(<span class="sc">~</span> <span class="fu">str_glue</span>(<span class="st">"tmp/train_history/{.x}.csv"</span>) <span class="sc">|></span> <span class="fu">read_csv</span>())) <span class="sc">|></span></span>
<span id="cb47-4"><a href="#cb47-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">unnest</span>(history) <span class="sc">|></span></span>
<span id="cb47-5"><a href="#cb47-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="fu">c</span>(<span class="st">"accuracy"</span>, <span class="st">"val_accuracy"</span>), <span class="at">names_to =</span> <span class="st">"subset"</span>) <span class="sc">|></span></span>
<span id="cb47-6"><a href="#cb47-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">subset =</span> subset <span class="sc">|></span> <span class="fu">recode</span>(<span class="at">accuracy =</span> <span class="st">"train"</span>, <span class="st">"val_accuracy"</span> <span class="ot">=</span> <span class="st">"validation"</span>)) <span class="sc">|></span></span>
<span id="cb47-7"><a href="#cb47-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(epoch, value, <span class="at">color =</span> subset)) <span class="sc">+</span></span>
<span id="cb47-8"><a href="#cb47-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>() <span class="sc">+</span></span>
<span id="cb47-9"><a href="#cb47-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>name, <span class="at">ncol =</span> <span class="dv">1</span>) <span class="sc">+</span></span>
<span id="cb47-10"><a href="#cb47-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">y =</span> <span class="st">"Accuracy"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Rows: 96 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (5): epoch, accuracy, loss, val_accuracy, val_loss
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 40 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (5): epoch, accuracy, loss, val_accuracy, val_loss
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 106 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (5): epoch, accuracy, loss, val_accuracy, val_loss
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Rows: 55 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
dbl (5): epoch, accuracy, loss, val_accuracy, val_loss
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.</code></pre>
</div>
<div class="cell-output-display">
<p><img src="index_files/figure-html/unnamed-chunk-25-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>The base and CNN1 model generalize well on the external validation samples, whereas CNN2 is affected by over-fitting. This is maybe due to the high number of trainable parameters.</p>
</section>
<section id="conclusion" class="level2">
<h2 class="anchored" data-anchor-id="conclusion">Conclusion</h2>
<p>CNN1 outperformed a linear SVM but its test accuracy was lower compared to a random forest. Regarding model complexity and computational effort for training, the analysis suggest that depp learning model might not be worth the efforts to predict the music genre if meaningful summary features, e.g. danceability and accousticness are available. However, using more sophisticated deep learning architectures, new deep learning models might be developed in the future to improve the validation accuracy even further.</p>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
setTimeout(function() {
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const cites = ref.parentNode.getAttribute('data-cites').split(' ');
tippyHover(ref, function() {
var popup = window.document.createElement('div');
cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
});
</script>
</div> <!-- /content -->
</body></html>