Skip to content

Commit 0b9d692

Browse files
committed
Cleaned up path handling in chapter 09
1 parent 9b3184e commit 0b9d692

4 files changed

Lines changed: 27 additions & 30 deletions

File tree

ch09/01_fft_based_classifier.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@
1414

1515
from sklearn.metrics import confusion_matrix
1616

17-
from utils import plot_pr, plot_roc, plot_confusion_matrix, GENRE_LIST
17+
from utils import plot_pr, plot_roc, plot_confusion_matrix, GENRE_LIST, TEST_DIR
1818

1919
from fft import read_fft
2020

21-
TEST_DIR = "/media/sf_P/pymlbook-data/09-genre-class/private"
22-
2321
genre_list = GENRE_LIST
2422

2523

@@ -83,7 +81,7 @@ def train_model(clf_factory, X, Y, name, plot=False):
8381

8482
if plot:
8583
for label in labels:
86-
print "Plotting", genre_list[label]
84+
print("Plotting %s"%genre_list[label])
8785
scores_to_sort = roc_scores[label]
8886
median = np.argsort(scores_to_sort)[len(scores_to_sort) / 2]
8987

@@ -96,7 +94,7 @@ def train_model(clf_factory, X, Y, name, plot=False):
9694
all_pr_scores = np.asarray(pr_scores.values()).flatten()
9795
summary = (np.mean(scores), np.std(scores),
9896
np.mean(all_pr_scores), np.std(all_pr_scores))
99-
print "%.3f\t%.3f\t%.3f\t%.3f\t" % summary
97+
print("%.3f\t%.3f\t%.3f\t%.3f\t" % summary)
10098

10199
return np.mean(train_errors), np.mean(test_errors), np.asarray(cms)
102100

@@ -117,7 +115,5 @@ def create_model():
117115
cm_avg = np.mean(cms, axis=0)
118116
cm_norm = cm_avg / np.sum(cm_avg, axis=0)
119117

120-
print cm_norm
121-
122118
plot_confusion_matrix(cm_norm, genre_list, "fft",
123119
"Confusion matrix of an FFT based classifier")

ch09/02_ceps_based_classifier.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414

1515
from sklearn.metrics import confusion_matrix
1616

17-
from utils import plot_roc, plot_confusion_matrix, GENRE_LIST
17+
from utils import plot_roc, plot_confusion_matrix, GENRE_LIST, TEST_DIR
1818

1919
from ceps import read_ceps
2020

21-
TEST_DIR = "/media/sf_P/pymlbook-data/09-genre-class/private"
2221

2322
genre_list = GENRE_LIST
2423

@@ -83,7 +82,7 @@ def train_model(clf_factory, X, Y, name, plot=False):
8382

8483
if plot:
8584
for label in labels:
86-
print "Plotting", genre_list[label]
85+
print("Plotting %s"%genre_list[label])
8786
scores_to_sort = roc_scores[label]
8887
median = np.argsort(scores_to_sort)[len(scores_to_sort) / 2]
8988

@@ -94,7 +93,7 @@ def train_model(clf_factory, X, Y, name, plot=False):
9493
all_pr_scores = np.asarray(pr_scores.values()).flatten()
9594
summary = (np.mean(scores), np.std(scores),
9695
np.mean(all_pr_scores), np.std(all_pr_scores))
97-
print "%.3f\t%.3f\t%.3f\t%.3f\t" % summary
96+
print("%.3f\t%.3f\t%.3f\t%.3f\t" % summary)
9897

9998
return np.mean(train_errors), np.mean(test_errors), np.asarray(cms)
10099

@@ -115,7 +114,5 @@ def create_model():
115114
cm_avg = np.mean(cms, axis=0)
116115
cm_norm = cm_avg / np.sum(cm_avg, axis=0)
117116

118-
print cm_norm
119-
120117
plot_confusion_matrix(cm_norm, genre_list, "ceps",
121118
"Confusion matrix of a CEPS based classifier")

ch09/Makefile

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
CHART_DIR = ../charts
2-
TARGET_DIR = /media/sf_P/Dropbox/pymlbook/pmle/ch09
3-
4-
copy: rocs_fft.png rocs_ceps.png fft_demo
5-
#cp $(CHART_DIR)/Spectrogram_Genres.png $(TARGET_DIR)/1400_09_01.png
6-
cp $(CHART_DIR)/confusion_matrix_fft.png $(TARGET_DIR)/1400_09_02.png
7-
cp $(CHART_DIR)/rocs_fft.png $(TARGET_DIR)/1400_09_03.png
8-
cp $(CHART_DIR)/rocs_ceps.png $(TARGET_DIR)/1400_09_04.png
9-
cp fft_demo.png $(TARGET_DIR)/1400_09_05.png
10-
cp fft_example.png $(TARGET_DIR)/1400_09_06.png
11-
cp $(CHART_DIR)/confusion_matrix_ceps.png $(TARGET_DIR)/1400_09_07.png
12-
cp roc_pr.png $(TARGET_DIR)/1400_09_08.png
13-
cp *.py $(TARGET_DIR)/code
14-
cp Makefile $(TARGET_DIR)/code
1+
CHART_DIR = charts
152

163
fft:
174
python 01_fft_based_classifier.py

ch09/utils.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,33 @@
66
# It is made available under the MIT License
77

88
import os
9+
import sys
910

1011
from matplotlib import pylab
1112
import numpy as np
1213

13-
DATA_DIR = os.path.join("..", "data")
14-
CHART_DIR = os.path.join("..", "charts")
14+
DATA_DIR = os.path.join(
15+
os.path.dirname(os.path.realpath(__file__)), "data")
1516

16-
GENRE_DIR = "/media/sf_P/pymlbook-data/09-genre-class/genres"
17+
CHART_DIR = os.path.join(
18+
os.path.dirname(os.path.realpath(__file__)), "charts")
19+
20+
for d in [DATA_DIR, CHART_DIR]:
21+
if not os.path.exists(d):
22+
os.mkdir(d)
23+
24+
# Put your directory to the different music genres here
25+
GENRE_DIR = None
1726
GENRE_LIST = ["classical", "jazz", "country", "pop", "rock", "metal"]
1827

28+
# Put your directory to the test dir here
29+
TEST_DIR = None
30+
31+
if GENRE_DIR is None or TEST_DIR is None:
32+
print("Please set GENRE_DIR and TEST_DIR in utils.py")
33+
sys.exit(1)
34+
35+
1936

2037
def plot_confusion_matrix(cm, genre_list, name, title):
2138
pylab.clf()

0 commit comments

Comments
 (0)