-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvm_classification.py
66 lines (49 loc) · 1.88 KB
/
svm_classification.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
import numpy as np
import os
from model_input import data_preprocessing
from constants import MODEL_INPUT, SEQUENCE_LENGTH, LABELS, OUTPUT_FILE_PATH
from sklearn.model_selection import train_test_split
import tensorflow as tf
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix
from graph import plot_confusion_matrix
# Used to remove some of the errors displayed by tensorflow
tf.get_logger().setLevel('INFO')
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# Gets Model values from constants file
sequence_length = SEQUENCE_LENGTH
num_features = MODEL_INPUT
# Giving the files to the dataset
train_set = []
train_labels = []
folder_path = OUTPUT_FILE_PATH
folder = [filename for filename in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, filename))]
# Extracting the set and labels from the dataset
for i in folder:
a, b = data_preprocessing(i)
train_set.extend(a)
train_labels.extend(b)
train_set = np.array(train_set)
train_labels = np.array(train_labels)
if len(train_labels.shape) > 1:
train_labels = np.argmax(train_labels, axis=1)
X_train, X_test, y_train, y_test = train_test_split(train_set, train_labels, test_size=0.25, random_state=29)
model = SVC()
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
# Evaluate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
# Evaluate precision
precision = precision_score(y_test, y_pred, average='weighted')
print("Precision:", precision)
# Evaluate recall
recall = recall_score(y_test, y_pred, average='weighted')
print("Recall:", recall)
# Evaluate F1-score
f1 = f1_score(y_test, y_pred, average='weighted')
print("F1-score:", f1)
# Confusion Matrix
conf_matrix = confusion_matrix(y_test, y_pred)
print("Confusion Matrix:")
plot_confusion_matrix(conf_matrix, LABELS, normalize=True)