-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
68 lines (45 loc) · 1.67 KB
/
main.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
import argparse
from typing import Tuple
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.utils import shuffle
from tsk.classifier import Classifier
parser = argparse.ArgumentParser(description='Takagi-Sugeno fuzzy system for FEHI')
parser.add_argument('--dataset', type=str, help='Dataset to use in the experiment')
parser.add_argument('--n_cluster', type=int, help='Number of clusters for C-Means clustering')
def parse_dataset(path: str) -> Tuple:
"""
Load CSV file from storage and parse the data inside.
:param path: (str) Path to the CSV file
:return: (Tuple) X and y values extracted from the CSV file
"""
with open(path, 'r') as f:
data = f.readlines()
clean_rows = [row.strip().split(',') for row in data]
clean_rows = np.array([list(map(float, row)) for row in clean_rows])
return clean_rows[:, :-1], clean_rows[:, -1]
def main():
"""Entry point of the application"""
flags = parser.parse_args()
# load and shuffle data
x, y = parse_dataset(flags.dataset)
x, y = shuffle(x, y)
print(f'Loaded dataset from: {flags.dataset}')
# prepare train/test split
x_train = x[:125]
y_train = y[:125]
x_test = x[125:]
y_test = y[125:]
print(f'Number of training samples: {len(x_train)}')
print(f'Number of test samples: {len(x_test)}')
# fit the fuzzy classifier
cls = Classifier()
print('Fitting classifier to data:')
cls.fit(x_train, y_train)
# predict
print('Predicting unseen data:')
y_pred = cls.predict(x_test)
accuracy = accuracy_score(y_test, y_pred)
print(f'\taccuracy: {accuracy}')
if __name__ == '__main__':
main()