-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm.py
60 lines (40 loc) · 1.5 KB
/
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
import argparse
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
import utils.loader as l
def get_arguments():
"""Gets arguments from the command line.
Returns:
A parser with the input arguments.
"""
# Creates the ArgumentParser
parser = argparse.ArgumentParser(
usage='Loads features, targets .npy files and fits a SVM.')
parser.add_argument(
'features', help='Path to the features .npy file', type=str)
parser.add_argument(
'targets', help='Path to the targets .npy file', type=str)
return parser.parse_args()
if __name__ == "__main__":
# Gathers the input arguments
args = get_arguments()
# Gathering variables from arguments
feature_array = args.features
target_array = args.targets
# Loads the arrays
features = l.load_npy(feature_array)
targets = l.load_npy(target_array)
# Dividing data into training and testing sets
x_train, x_test, y_train, y_test = train_test_split(features, targets, test_size=0.33, random_state=42)
# Instanciating classifier
clf = SVC(C=500, kernel='rbf', probability=True)
# Fitting classifier
clf.fit(x_train, y_train)
# Scoring data with classifier
print(f'Accuracy: {clf.score(x_test, y_test)}')
# Defining a prediction sample
sample = [[0.9, 0.1, 0.8]]
# Predicting new data
pred = clf.predict(sample)
prob = clf.predict_proba(sample)
print(f'Sample: {sample} | Class: {pred} | Probs: {prob}')