-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvm.py
More file actions
51 lines (41 loc) · 1.82 KB
/
Copy pathsvm.py
File metadata and controls
51 lines (41 loc) · 1.82 KB
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
# import libraries
import numpy as np
import pandas as pd
import scipy
from sklearn import svm
from sklearn.preprocessing import StandardScaler
# read features from csv
features = pd.read_csv('features.csv')
test_features = pd.read_csv('test_features.csv')
# create feature dataframe
feature_names = ["idx", "mean", "stdev", "skew", "kurtosis", "zcr_mean", "zcr_stdev",
"rmse_mean", "rmse_stdev", "tempo"] + \
['mfccs_' + str(i+1) + '_mean' for i in range(20)] + \
['mfccs_' + str(i+1) + '_stdev' for i in range(20)] + \
['chroma_' + str(i+1) + '_mean' for i in range(12)] + \
['chroma_' + str(i+1) + '_stdev' for i in range(12)] + \
["centroid_mean", "centroid_stdev"] + \
['contrast_' + str(i+1) + '_mean' for i in range(7)] + \
['contrast_' + str(i+1) + '_std' for i in range(7)] + \
["rolloff_mean", "rolloff_stdev", "genre"]
param_names = feature_names[1:-1]
label_names = feature_names[-1]
# extract parameters and labels
params = features.loc[:, param_names].values
labels = features.loc[:, label_names].values
test_params = test_features.loc[:, param_names].values
# normalize data
params_norm = StandardScaler().fit_transform(params)
test_params_norm = StandardScaler().fit_transform(test_params)
# classifier
clf = svm.SVC(gamma = 0.01) # gamma = 0.01, 0.1, etc.
# learning
clf.fit(params_norm, labels)
#prediction
pred = clf.predict(test_params_norm)
pred_frame = pd.DataFrame(columns=['filename', 'label'])
filenames = test_features.loc[:, 'filename'].values
pred_frame = pd.DataFrame({'filename': filenames, 'label': pred})
# print(pred_frame)
# save features to csv
pred_frame.to_csv('Predictions/svm_predictions.csv', encoding='utf-8', index=False)