-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVM.py
More file actions
36 lines (27 loc) · 1.11 KB
/
SVM.py
File metadata and controls
36 lines (27 loc) · 1.11 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
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import StandardScaler
from Utils import RawData, Plotter
# Taking clean data from RawData() class
df = RawData().df
# Separating features and label
X = df[RawData().feature_cols] # Features
y = df.music_genre # Target variable
# Separating train and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=1)
# Initializing SVM modals with rbf algorithm
clf = svm.SVC(kernel='rbf') # ‘linear’, ‘poly’, ‘rbf’, ‘sigmoid’, ‘precomputed’
# Applying standardization
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
# Training model with train sets
clf.fit(X_train, y_train)
# Testing model with test sets
y_pred = clf.predict(X_test)
# Printing accuracy score
print("Accuracy:", accuracy_score(y_test, y_pred))
# Plot Common Graphs
Plotter().plot_cofusion_matrix(y_test, y_pred, "Support Vector Machine")
Plotter().plot_traning_curves(X, y, clf, "Support Vector Machine")