08.06.2015 Views

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Classification III – Music Genre Classification<br />

Using the confusion matrix to measure<br />

accuracy in multiclass problems<br />

With multiclass problems, we should also not limit our interest to how well we<br />

manage to correctly classify the genres. In addition, we should also look at which<br />

genres we actually confuse <strong>with</strong> each other. This can be done <strong>with</strong> the so-called<br />

confusion matrix:<br />

>>> from sklearn.metrics import confusion_matrix<br />

>>> cm = confusion_matrix(y_test, y_pred)<br />

>>> print(cm)<br />

[[26 1 2 0 0 2]<br />

[ 4 7 5 0 5 3]<br />

[ 1 2 14 2 8 3]<br />

[ 5 4 7 3 7 5]<br />

[ 0 0 10 2 10 12]<br />

[ 1 0 4 0 13 12]]<br />

It prints the distribution of labels that the classifier predicted for the test set for every<br />

genre. Since we have six genres, we have a six by six matrix. The first row in the<br />

matrix says that for 31 classical songs (sum of the first row), it predicted 26 to belong<br />

to the genre classical, one to be a jazz song, two to belong to the country genre, and<br />

two to be metal. The diagonal shows the correct classifications. In the first row, we<br />

see that out of 31 songs (26 + 1 + 2 + 2 = 31), 26 have been correctly classified as<br />

classical and 5 were misclassifications. This is actually not that bad. The second row<br />

is more sobering: only 4 out of 24 jazz songs have been correctly classified—that is<br />

only 16 percent.<br />

Of course, we follow the train/test split setup from the previous chapters, so that<br />

we actually have to record the confusion matrices per cross-validation fold. We also<br />

have to average and normalize later on so that we have a range between 0 (total<br />

failure) to 1 (everything classified correctly).<br />

A graphical visualization is often much easier to read than NumPy arrays.<br />

Matplotlib's matshow() is our friend:<br />

from matplotlib import pylab<br />

def plot_confusion_matrix(cm, genre_list, name, title):<br />

pylab.clf()<br />

pylab.matshow(cm, fignum=False, cmap='Blues', vmin=0, vmax=1.0)<br />

ax = pylab.axes()<br />

ax.set_xticks(range(len(genre_list)))<br />

ax.set_xticklabels(genre_list)<br />

ax.xaxis.set_ticks_position("bottom")<br />

[ 188 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!