-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraditionalMethods.py
116 lines (99 loc) · 4.69 KB
/
traditionalMethods.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix, ConfusionMatrixDisplay
from sklearn.neighbors import KNeighborsClassifier
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.neural_network import MLPClassifier
from sklearn.svm import SVC
from sklearn import preprocessing
LABELS = ["Blues", "Classical", "Country", "Disco", "HipHop", "Jazz", "Metal", "Pop", "Reggae", "Rock"]
def getMetrics(y_pred, y_test, modelName, returnRow=False):
acc = accuracy_score(y_pred, y_test)
precision = precision_score(y_test, y_pred, average='macro')
recall = recall_score(y_test, y_pred, average='macro')
f1 = f1_score(y_test, y_pred, average='macro')
if returnRow:
return [modelName, round(acc,4), round(precision,4), round(recall,4), round(f1,4)]
else:
print(f"Model: {modelName}")
print(f"- CA: {round(acc,4)}")
print(f"- Precision: {round(precision,4)}")
print(f"- Recall: {round(recall,4)}")
print(f"- F1: {round(f1,4)}")
def vizConfusionMat(y_pred, y_test, currentModel, save=False):
cm = confusion_matrix(y_test, y_pred)
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=LABELS, )
disp.plot()
plt.title(currentModel)
plt.xticks(rotation = 90)
plt.tight_layout()
plt.savefig(f"./GTZAN/results/{currentModel}.jpg") if save else plt.show()
def evalBaseline(y_test):
y_pred = np.array([0]*y_test.shape[0])
return y_pred
def evalKnn(X_train, X_test, y_train, k=5):
knn = KNeighborsClassifier(n_neighbors=k)
model = knn.fit(X_train, y_train)
y_pred = model.predict(X_test)
return y_pred
def evalRandomForest(X_train, X_test, y_train, n=100):
rf = RandomForestClassifier(n_estimators=n, random_state=42)
model = rf.fit(X_train, y_train)
y_pred = model.predict(X_test)
return y_pred
def evalLogRegression(X_train, X_test, y_train):
logReg = LogisticRegression(random_state=42)
model = logReg.fit(X_train, y_train)
y_pred = model.predict(X_test)
return y_pred
def evalSVM(X_train, X_test, y_train, kernel="rbf",c=1):
svm = SVC(C=c, kernel=kernel, random_state=42)
model = svm.fit(X_train, y_train)
y_pred = model.predict(X_test)
return y_pred
def evalGB(X_train, X_test, y_train, n=100, rate=1.0):
xgb = GradientBoostingClassifier(n_estimators=n, learning_rate=rate, random_state=42)
model = xgb.fit(X_train, y_train)
y_pred = model.predict(X_test)
return y_pred
def evalPerceptron(X_train, X_test, y_train, hLayerSize=30, numIter=200):
mp = MLPClassifier(hidden_layer_sizes=(hLayerSize), max_iter=numIter, random_state=42)
model = mp.fit(X_train, y_train)
y_pred = model.predict(X_test)
return y_pred
if __name__ == "__main__":
# Load data
df = pd.read_pickle("./GTZAN/rawFeaturesGTZAN.pkl")
# Split into train and test
data = df.drop(["path", "class"], axis=1)
X = data.to_numpy()
y = df["class"].to_numpy()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42, shuffle=True, stratify=y)
gaussScaler = preprocessing.PowerTransformer(method='yeo-johnson', standardize=True).fit(X_train)
X_train = gaussScaler.transform(X_train)
X_test = gaussScaler.transform(X_test)
models = ["baseline","kNN","randomForest","logRegression","SVM","GradientBoosting","MultilayerPerceptron"]
results = pd.DataFrame(columns=["modelName","acc","precision","recall","f1"])
for currentModel in tqdm(models):
if currentModel == "baseline":
y_pred = evalBaseline(y_test)
elif currentModel == "kNN":
y_pred = evalKnn(X_train, X_test, y_train, 11)
elif currentModel == "randomForest":
y_pred = evalRandomForest(X_train, X_test, y_train, 200)
elif currentModel == "logRegression":
y_pred = evalLogRegression(X_train, X_test, y_train)
elif currentModel == "SVM":
y_pred = evalSVM(X_train, X_test, y_train, kernel="rbf", c=4.4)
elif currentModel == "GradientBoosting":
y_pred = evalGB(X_train, X_test, y_train, rate=0.5)
elif currentModel == "MultilayerPerceptron":
y_pred = evalPerceptron(X_train, X_test, y_train, hLayerSize=30, numIter=200)
vizConfusionMat(y_pred, y_test, currentModel, save=True)
newRow = getMetrics(y_pred, y_test, currentModel, returnRow=True)
results.loc[len(results)] = newRow
results.to_csv("./GTZAN/results/tabel.csv", index=False)