Commit cae5a9b0 authored by Steve Tjoa's avatar Steve Tjoa

genre recognition exercise

parent a96f4a15
......@@ -11754,7 +11754,7 @@ div#notebook {
<div class=" highlight hl-ipython2"><pre><span></span><span class="o">%</span><span class="k">matplotlib</span> inline
<span class="kn">import</span> <span class="nn">seaborn</span>
<span class="kn">import</span> <span class="nn">numpy</span><span class="o">,</span> <span class="nn">scipy</span><span class="o">,</span> <span class="nn">matplotlib.pyplot</span> <span class="kn">as</span> <span class="nn">plt</span><span class="o">,</span> <span class="nn">sklearn</span><span class="o">,</span> <span class="nn">pandas</span><span class="o">,</span> <span class="nn">librosa</span><span class="o">,</span> <span class="nn">urllib</span><span class="o">,</span> <span class="nn">IPython.display</span><span class="o">,</span> <span class="nn">os.path</span>
<span class="n">plt</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">&#39;figure.figsize&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="mi">14</span><span class="p">,</span><span class="mi">5</span><span class="p">)</span>
<span class="n">plt</span><span class="o">.</span><span class="n">rcParams</span><span class="p">[</span><span class="s1">&#39;figure.figsize&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="mi">14</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
</pre></div>
</div>
......@@ -11767,7 +11767,17 @@ div#notebook {
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h1 id="Homework-Part-2:-Genre-Classification">Homework Part 2: Genre Classification<a class="anchor-link" href="#Homework-Part-2:-Genre-Classification">&#182;</a></h1>
<p><a href="index.html">&larr; Back to Index</a></p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h1 id="Exercise:-Genre-Recognition">Exercise: Genre Recognition<a class="anchor-link" href="#Exercise:-Genre-Recognition">&#182;</a></h1>
</div>
</div>
</div>
......@@ -11788,7 +11798,7 @@ div#notebook {
<ol>
<li>Extract features from an audio signal.</li>
<li>Train a genre classifier.</li>
<li>Use the classifier to classify genre in a song.</li>
<li>Use the classifier to classify the genre in a song.</li>
</ol>
</div>
......@@ -11818,10 +11828,10 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">filename1</span> <span class="o">=</span> <span class="s1">&#39;brahms_hungarian_dance_5.mp3&#39;</span>
<span class="n">url</span> <span class="o">=</span> <span class="s2">&quot;http://audio.musicinformationretrieval.com/&quot;</span> <span class="o">+</span> <span class="n">filename1</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">filename1</span><span class="p">):</span>
<span class="n">urllib</span><span class="o">.</span><span class="n">urlretrieve</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="n">filename1</span><span class="p">)</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">filename_brahms</span> <span class="o">=</span> <span class="s1">&#39;brahms_hungarian_dance_5.mp3&#39;</span>
<span class="n">url</span> <span class="o">=</span> <span class="s2">&quot;http://audio.musicinformationretrieval.com/&quot;</span> <span class="o">+</span> <span class="n">filename_brahms</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">exists</span><span class="p">(</span><span class="n">filename_brahms</span><span class="p">):</span>
<span class="n">urllib</span><span class="o">.</span><span class="n">urlretrieve</span><span class="p">(</span><span class="n">url</span><span class="p">,</span> <span class="n">filename</span><span class="o">=</span><span class="n">filename_brahms</span><span class="p">)</span>
</pre></div>
</div>
......@@ -11857,7 +11867,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">x1</span><span class="p">,</span> <span class="n">fs1</span> <span class="o">=</span> <span class="n">librosa</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">filename1</span><span class="p">,</span> <span class="n">duration</span><span class="o">=</span><span class="mi">120</span><span class="p">)</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">x_brahms</span><span class="p">,</span> <span class="n">fs_brahms</span> <span class="o">=</span> <span class="n">librosa</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">filename_brahms</span><span class="p">,</span> <span class="n">duration</span><span class="o">=</span><span class="mi">120</span><span class="p">)</span>
</pre></div>
</div>
......@@ -11880,7 +11890,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>plt.plot<span class="o">?</span>
<div class=" highlight hl-ipython2"><pre><span></span>librosa.display.waveplot<span class="o">?</span>
</pre></div>
</div>
......@@ -11974,20 +11984,31 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">mfcc1</span> <span class="o">=</span> <span class="n">librosa</span><span class="o">.</span><span class="n">feature</span><span class="o">.</span><span class="n">mfcc</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span> <span class="n">sr</span><span class="o">=</span><span class="n">fs1</span><span class="p">,</span> <span class="n">n_mfcc</span><span class="o">=</span><span class="mi">12</span><span class="p">)</span><span class="o">.</span><span class="n">T</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">n_mfcc</span> <span class="o">=</span> <span class="mi">12</span>
<span class="n">mfcc_brahms</span> <span class="o">=</span> <span class="n">librosa</span><span class="o">.</span><span class="n">feature</span><span class="o">.</span><span class="n">mfcc</span><span class="p">(</span><span class="n">x_brahms</span><span class="p">,</span> <span class="n">sr</span><span class="o">=</span><span class="n">fs_brahms</span><span class="p">,</span> <span class="n">n_mfcc</span><span class="o">=</span><span class="n">n_mfcc</span><span class="p">)</span><span class="o">.</span><span class="n">T</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We transpose the result to accommodate scikit-learn which assumes that each row is one observation, and each column is one feature dimension:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">mfcc1</span><span class="o">.</span><span class="n">shape</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">mfcc_brahms</span><span class="o">.</span><span class="n">shape</span>
</pre></div>
</div>
......@@ -12023,7 +12044,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">mfcc1_scaled</span> <span class="o">=</span> <span class="n">scaler</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">mfcc1</span><span class="p">)</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">mfcc_brahms_scaled</span> <span class="o">=</span> <span class="n">scaler</span><span class="o">.</span><span class="n">fit_transform</span><span class="p">(</span><span class="n">mfcc_brahms</span><span class="p">)</span>
</pre></div>
</div>
......@@ -12046,7 +12067,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">mfcc1_scaled</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">mfcc_brahms_scaled</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
......@@ -12059,7 +12080,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">mfcc1_scaled</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">mfcc_brahms_scaled</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
......@@ -12072,8 +12093,7 @@ div#notebook {
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><strong>Repeat steps 1 and 2 for another audio file:</strong></p>
<h3 id="Step-2b:-Repeat-steps-1-and-2-for-another-audio-file.">Step 2b: Repeat steps 1 and 2 for another audio file.<a class="anchor-link" href="#Step-2b:-Repeat-steps-1-and-2-for-another-audio-file.">&#182;</a></h3>
</div>
</div>
</div>
......@@ -12082,8 +12102,8 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">filename2</span> <span class="o">=</span> <span class="s1">&#39;busta_rhymes_hits_for_days.mp3&#39;</span>
<span class="n">url</span> <span class="o">=</span> <span class="s2">&quot;http://audio.musicinformationretrieval.com/&quot;</span> <span class="o">+</span> <span class="n">filename2</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">filename_busta</span> <span class="o">=</span> <span class="s1">&#39;busta_rhymes_hits_for_days.mp3&#39;</span>
<span class="n">url</span> <span class="o">=</span> <span class="s2">&quot;http://audio.musicinformationretrieval.com/&quot;</span> <span class="o">+</span> <span class="n">filename_busta</span>
</pre></div>
</div>
......@@ -12146,6 +12166,7 @@ div#notebook {
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="c1"># Your code here. Load the second audio file in the same manner as the first audio file.</span>
<span class="c1"># x_busta, fs_busta = </span>
</pre></div>
</div>
......@@ -12181,7 +12202,7 @@ div#notebook {
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Plot the time-domain waveform and spectrogram of the second audio file. In what ways does the time-domain waveform look different than the first audio file? What differences in musical attributes might this reflect? What additional insights are gained from plotting the spectrogram? <strong>Explain.</strong></p>
<p>Plot the time-domain waveform and spectrogram of the second audio file. In what ways does the time-domain waveform look different than the first audio file? What differences in musical attributes might this reflect? What additional insights are gained from plotting the spectrogram? Explain.</p>
</div>
</div>
......@@ -12244,27 +12265,31 @@ librosa.feature.melspectrogram<span class="o">?</span>
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><strong>[Please share your answer in this editable text cell.]</strong></p>
<p>Extract MFCCs from the second audio file. Be sure to transpose the resulting matrix such that each row is one observation, i.e. one set of MFCCs. Also be sure that the shape and size of the resulting MFCC matrix is equivalent to that for the first audio file.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Extract MFCCs from the second audio file. Be sure to transpose the resulting matrix such that each row is one observation, i.e. one set of MFCCs. Also be sure that the shape and size of the resulting MFCC matrix is equivalent to that for the first audio file.</p>
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>librosa.feature.mfcc<span class="o">?</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>librosa.feature.mfcc<span class="o">?</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="c1"># Your code here:</span>
<span class="c1"># mfcc_busta =</span>
</pre></div>
</div>
......@@ -12277,7 +12302,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">mfcc2</span><span class="o">.</span><span class="n">shape</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">mfcc_busta</span><span class="o">.</span><span class="n">shape</span>
</pre></div>
</div>
......@@ -12307,6 +12332,20 @@ librosa.feature.melspectrogram<span class="o">?</span>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="c1"># Your code here:</span>
<span class="c1"># mfcc_busta_scaled =</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
......@@ -12323,7 +12362,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>mfcc2_scaled.mean<span class="o">?</span>
<div class=" highlight hl-ipython2"><pre><span></span>mfcc_busta_scaled.mean<span class="o">?</span>
</pre></div>
</div>
......@@ -12336,7 +12375,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>mfcc2_scaled.std<span class="o">?</span>
<div class=" highlight hl-ipython2"><pre><span></span>mfcc_busta_scaled.std<span class="o">?</span>
</pre></div>
</div>
......@@ -12368,7 +12407,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">features</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">vstack</span><span class="p">((</span><span class="n">mfcc1_scaled</span><span class="p">,</span> <span class="n">mfcc2_scaled</span><span class="p">))</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">features</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">vstack</span><span class="p">((</span><span class="n">mfcc_brahms_scaled</span><span class="p">,</span> <span class="n">mfcc_busta_scaled</span><span class="p">))</span>
</pre></div>
</div>
......@@ -12404,7 +12443,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">labels</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">mfcc1_scaled</span><span class="p">)),</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">mfcc2_scaled</span><span class="p">))))</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">labels</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">mfcc_brahms_scaled</span><span class="p">)),</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">mfcc_busta_scaled</span><span class="p">))))</span>
</pre></div>
</div>
......@@ -12496,7 +12535,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">x1_test</span><span class="p">,</span> <span class="n">fs1</span> <span class="o">=</span> <span class="n">librosa</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">filename1</span><span class="p">,</span> <span class="n">duration</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="mi">120</span><span class="p">)</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">x_brahms_test</span><span class="p">,</span> <span class="n">fs_brahms</span> <span class="o">=</span> <span class="n">librosa</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">filename_brahms</span><span class="p">,</span> <span class="n">duration</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="mi">120</span><span class="p">)</span>
</pre></div>
</div>
......@@ -12509,7 +12548,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">x2_test</span><span class="p">,</span> <span class="n">fs2</span> <span class="o">=</span> <span class="n">librosa</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">filename2</span><span class="p">,</span> <span class="n">duration</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="mi">120</span><span class="p">)</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">x_busta_test</span><span class="p">,</span> <span class="n">fs_busta</span> <span class="o">=</span> <span class="n">librosa</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">filename_busta</span><span class="p">,</span> <span class="n">duration</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">offset</span><span class="o">=</span><span class="mi">120</span><span class="p">)</span>
</pre></div>
</div>
......@@ -12752,16 +12791,6 @@ librosa.feature.melspectrogram<span class="o">?</span>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><strong>[Explain your approach in this editable text cell.]</strong></p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
......@@ -12787,7 +12816,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df1</span> <span class="o">=</span> <span class="n">pandas</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">mfcc1_test_scaled</span><span class="p">)</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df_brahms</span> <span class="o">=</span> <span class="n">pandas</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">mfcc_brahms_test_scaled</span><span class="p">)</span>
</pre></div>
</div>
......@@ -12800,7 +12829,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df1</span><span class="o">.</span><span class="n">shape</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df_brahms</span><span class="o">.</span><span class="n">shape</span>
</pre></div>
</div>
......@@ -12813,7 +12842,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df1</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df_brahms</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
</pre></div>
</div>
......@@ -12826,7 +12855,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df2</span> <span class="o">=</span> <span class="n">pandas</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">mfcc2_test_scaled</span><span class="p">)</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df_busta</span> <span class="o">=</span> <span class="n">pandas</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">mfcc_busta_test_scaled</span><span class="p">)</span>
</pre></div>
</div>
......@@ -12849,7 +12878,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df1</span><span class="o">.</span><span class="n">corr</span><span class="p">()</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df_brahms</span><span class="o">.</span><span class="n">corr</span><span class="p">()</span>
</pre></div>
</div>
......@@ -12862,23 +12891,13 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df2</span><span class="o">.</span><span class="n">corr</span><span class="p">()</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df_busta</span><span class="o">.</span><span class="n">corr</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><strong>[Explain your answer in this editable text cell.]</strong></p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
......@@ -12895,7 +12914,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>df1.plot.scatter<span class="o">?</span>
<div class=" highlight hl-ipython2"><pre><span></span>df_brahms.plot.scatter<span class="o">?</span>
</pre></div>
</div>
......@@ -12918,7 +12937,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>df2.plot.scatter<span class="o">?</span>
<div class=" highlight hl-ipython2"><pre><span></span>df_busta.plot.scatter<span class="o">?</span>
</pre></div>
</div>
......@@ -12941,7 +12960,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df1</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="o">.</span><span class="n">hist</span><span class="p">()</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df_brahms</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="o">.</span><span class="n">hist</span><span class="p">()</span>
</pre></div>
</div>
......@@ -12954,7 +12973,7 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df1</span><span class="p">[</span><span class="mi">11</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="o">.</span><span class="n">hist</span><span class="p">()</span>
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">df_busta</span><span class="p">[</span><span class="mi">11</span><span class="p">]</span><span class="o">.</span><span class="n">plot</span><span class="o">.</span><span class="n">hist</span><span class="p">()</span>
</pre></div>
</div>
......@@ -12998,6 +13017,16 @@ librosa.feature.melspectrogram<span class="o">?</span>
<div class="text_cell_render border-box-sizing rendered_html">
<p>Create a new genre classifier by repeating the steps above, but this time use different features. Consult the <a href="http://librosa.github.io/librosa/feature.html">librosa documentation on feature extraction</a> for different choices of features. Which features work well? not well?</p>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><a href="index.html">&larr; Back to Index</a></p>
</div>
</div>
</div>
......
......@@ -11,14 +11,21 @@
"%matplotlib inline\n",
"import seaborn\n",
"import numpy, scipy, matplotlib.pyplot as plt, sklearn, pandas, librosa, urllib, IPython.display, os.path\n",
"plt.rcParams['figure.figsize'] = (14,5)"
"plt.rcParams['figure.figsize'] = (14, 5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Homework Part 2: Genre Classification"
"[&larr; Back to Index](index.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise: Genre Recognition"
]
},
{
......@@ -34,7 +41,7 @@
"source": [
"1. Extract features from an audio signal.\n",
"2. Train a genre classifier.\n",
"3. Use the classifier to classify genre in a song."
"3. Use the classifier to classify the genre in a song."
]
},
{
......@@ -59,10 +66,10 @@
},
"outputs": [],
"source": [
"filename1 = 'brahms_hungarian_dance_5.mp3'\n",
"url = \"http://audio.musicinformationretrieval.com/\" + filename1\n",
"if not os.path.exists(filename1):\n",
" urllib.urlretrieve(url, filename=filename1)"
"filename_brahms = 'brahms_hungarian_dance_5.mp3'\n",
"url = \"http://audio.musicinformationretrieval.com/\" + filename_brahms\n",
"if not os.path.exists(filename_brahms):\n",
" urllib.urlretrieve(url, filename=filename_brahms)"
]
},
{
......@@ -91,7 +98,7 @@
},
"outputs": [],
"source": [
"x1, fs1 = librosa.load(filename1, duration=120)"
"x_brahms, fs_brahms = librosa.load(filename_brahms, duration=120)"
]
},
{
......@@ -109,7 +116,7 @@
},
"outputs": [],
"source": [
"plt.plot?"
"librosa.display.waveplot?"
]
},
{
......@@ -185,7 +192,15 @@
},
"outputs": [],
"source": [
"mfcc1 = librosa.feature.mfcc(x1, sr=fs1, n_mfcc=12).T"
"n_mfcc = 12\n",
"mfcc_brahms = librosa.feature.mfcc(x_brahms, sr=fs_brahms, n_mfcc=n_mfcc).T"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We transpose the result to accommodate scikit-learn which assumes that each row is one observation, and each column is one feature dimension:"
]
},
{
......@@ -196,7 +211,7 @@
},
"outputs": [],
"source": [
"mfcc1.shape"
"mfcc_brahms.shape"
]
},
{
......@@ -225,7 +240,7 @@
},
"outputs": [],
"source": [
"mfcc1_scaled = scaler.fit_transform(mfcc1)"
"mfcc_brahms_scaled = scaler.fit_transform(mfcc_brahms)"
]
},
{
......@@ -243,7 +258,7 @@
},
"outputs": [],
"source": [
"mfcc1_scaled.mean(axis=0)"
"mfcc_brahms_scaled.mean(axis=0)"
]
},
{
......@@ -254,14 +269,14 @@
},
"outputs": [],
"source": [
"mfcc1_scaled.std(axis=0)"
"mfcc_brahms_scaled.std(axis=0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Repeat steps 1 and 2 for another audio file:**"
"### Step 2b: Repeat steps 1 and 2 for another audio file."
]
},
{
......@@ -272,8 +287,8 @@
},
"outputs": [],
"source": [
"filename2 = 'busta_rhymes_hits_for_days.mp3'\n",
"url = \"http://audio.musicinformationretrieval.com/\" + filename2"
"filename_busta = 'busta_rhymes_hits_for_days.mp3'\n",
"url = \"http://audio.musicinformationretrieval.com/\" + filename_busta"
]
},
{
......@@ -324,7 +339,8 @@
},
"outputs": [],
"source": [
"# Your code here. Load the second audio file in the same manner as the first audio file."
"# Your code here. Load the second audio file in the same manner as the first audio file.\n",
"# x_busta, fs_busta = "
]
},
{
......@@ -349,7 +365,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Plot the time-domain waveform and spectrogram of the second audio file. In what ways does the time-domain waveform look different than the first audio file? What differences in musical attributes might this reflect? What additional insights are gained from plotting the spectrogram? **Explain.**"
"Plot the time-domain waveform and spectrogram of the second audio file. In what ways does the time-domain waveform look different than the first audio file? What differences in musical attributes might this reflect? What additional insights are gained from plotting the spectrogram? Explain."
]
},
{
......@@ -401,25 +417,30 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**[Please share your answer in this editable text cell.]**"
"Extract MFCCs from the second audio file. Be sure to transpose the resulting matrix such that each row is one observation, i.e. one set of MFCCs. Also be sure that the shape and size of the resulting MFCC matrix is equivalent to that for the first audio file."
]
},
{
"cell_type": "markdown",
"metadata": {},
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Extract MFCCs from the second audio file. Be sure to transpose the resulting matrix such that each row is one observation, i.e. one set of MFCCs. Also be sure that the shape and size of the resulting MFCC matrix is equivalent to that for the first audio file."
"librosa.feature.mfcc?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
"collapsed": true
},
"outputs": [],
"source": [
"librosa.feature.mfcc?"
"# Your code here:\n",
"# mfcc_busta ="
]
},
{
......@@ -430,7 +451,7 @@
},
"outputs": [],
"source": [
"mfcc2.shape"
"mfcc_busta.shape"
]
},
{
......@@ -451,6 +472,18 @@
"scaler.transform?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Your code here:\n",
"# mfcc_busta_scaled ="
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -466,7 +499,7 @@
},
"outputs": [],
"source": [
"mfcc2_scaled.mean?"
"mfcc_busta_scaled.mean?"
]
},
{
......@@ -477,7 +510,7 @@
},
"outputs": [],
"source": [
"mfcc2_scaled.std?"
"mfcc_busta_scaled.std?"
]
},
{
......@@ -502,7 +535,7 @@
},
"outputs": [],
"source": [
"features = numpy.vstack((mfcc1_scaled, mfcc2_scaled))"
"features = numpy.vstack((mfcc_brahms_scaled, mfcc_busta_scaled))"
]
},
{
......@@ -531,7 +564,7 @@
},
"outputs": [],
"source": [
"labels = numpy.concatenate((numpy.zeros(len(mfcc1_scaled)), numpy.ones(len(mfcc2_scaled))))"
"labels = numpy.concatenate((numpy.zeros(len(mfcc_brahms_scaled)), numpy.ones(len(mfcc_busta_scaled))))"
]
},
{
......@@ -604,7 +637,7 @@
},
"outputs": [],
"source": [
"x1_test, fs1 = librosa.load(filename1, duration=10, offset=120)"
"x_brahms_test, fs_brahms = librosa.load(filename_brahms, duration=10, offset=120)"
]
},
{
......@@ -615,7 +648,7 @@
},
"outputs": [],
"source": [
"x2_test, fs2 = librosa.load(filename2, duration=10, offset=120)"
"x_busta_test, fs_busta = librosa.load(filename_busta, duration=10, offset=120)"
]
},
{
......@@ -806,13 +839,6 @@
"# Your code here."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[Explain your approach in this editable text cell.]**"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -835,7 +861,7 @@
},
"outputs": [],
"source": [
"df1 = pandas.DataFrame(mfcc1_test_scaled)"
"df_brahms = pandas.DataFrame(mfcc_brahms_test_scaled)"
]
},
{
......@@ -846,7 +872,7 @@
},
"outputs": [],
"source": [
"df1.shape"
"df_brahms.shape"
]
},
{
......@@ -857,7 +883,7 @@
},
"outputs": [],
"source": [
"df1.head()"
"df_brahms.head()"
]
},
{
......@@ -868,7 +894,7 @@
},
"outputs": [],
"source": [
"df2 = pandas.DataFrame(mfcc2_test_scaled)"
"df_busta = pandas.DataFrame(mfcc_busta_test_scaled)"
]
},
{
......@@ -886,7 +912,7 @@
},
"outputs": [],
"source": [
"df1.corr()"
"df_brahms.corr()"
]
},
{
......@@ -897,14 +923,7 @@
},
"outputs": [],
"source": [
"df2.corr()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**[Explain your answer in this editable text cell.]**"
"df_busta.corr()"
]
},
{
......@@ -922,7 +941,7 @@
},
"outputs": [],
"source": [
"df1.plot.scatter?"
"df_brahms.plot.scatter?"
]
},
{
......@@ -940,7 +959,7 @@
},
"outputs": [],
"source": [
"df2.plot.scatter?"
"df_busta.plot.scatter?"
]
},
{
......@@ -958,7 +977,7 @@
},
"outputs": [],
"source": [
"df1[0].plot.hist()"
"df_brahms[0].plot.hist()"
]
},
{
......@@ -969,7 +988,7 @@
},
"outputs": [],
"source": [
"df1[11].plot.hist()"
"df_busta[11].plot.hist()"
]
},
{
......@@ -999,6 +1018,13 @@
"source": [
"Create a new genre classifier by repeating the steps above, but this time use different features. Consult the [librosa documentation on feature extraction](http://librosa.github.io/librosa/feature.html) for different choices of features. Which features work well? not well?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[&larr; Back to Index](index.html)"
]
}
],
"metadata": {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment