-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathbaseline_svm.py
executable file
·72 lines (43 loc) · 1.71 KB
/
baseline_svm.py
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
#!/usr/bin/env python3
"""Adapted from https://github.com/mdeff/fma/blob/master/baselines.ipynb"""
import numpy as np
import pandas as pd
from sklearn.utils import shuffle
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
import fma
y_train = pd.read_csv('data/train_labels.csv', index_col=0, squeeze=True)
features = fma.load('data/features.csv')
X_train = features[:25000]
X_test = features[25000:]
# Data cleaning
X_train = X_train.drop(fma.FILES_TRAIN_FAULTY)
y_train = y_train.drop(fma.FILES_TRAIN_FAULTY)
# The track IDs are integers for the training set.
X_train.index = pd.Index((int(i) for i in X_train.index), name='track_id')
# Should be done already, but better be sure.
X_train.sort_index(inplace=True)
X_test.sort_index(inplace=True)
y_train.sort_index(inplace=True)
assert (X_train.index == y_train.index).all()
# Pre-processing
X_train, y_train = shuffle(X_train, y_train, random_state=42)
# Should not be needed.
# enc = LabelEncoder()
# y_train = enc.fit_transform(y_train)
# Standardize features by removing the mean and scaling to unit variance.
scaler = StandardScaler(copy=False)
scaler.fit_transform(X_train)
scaler.transform(X_test)
# Train the classifier and make predictions.
clf = SVC(kernel='rbf', probability=True)
clf.fit(X_train, y_train)
y_test = clf.predict_proba(X_test)
# Create the submission file.
submission = pd.DataFrame(y_test, X_test.index, clf.classes_)
submission.index.name = 'file_id'
# Be sure that we predicted one and only one genre per track.
np.testing.assert_allclose(submission.sum(axis=1), 1)
print('Predicted tracks per genre:')
print(submission.idxmax(axis=1).value_counts())
submission.to_csv('data/submission_svm.csv', header=True)