-
Notifications
You must be signed in to change notification settings - Fork 0
/
iris_test.py
72 lines (61 loc) · 1.92 KB
/
iris_test.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
from datetime import datetime
from pathlib import Path
from sklearn.datasets import load_iris
from sklearn.metrics import confusion_matrix
from sklearn.model_selection import train_test_split
import pandas as pd
from evofuzzy import FuzzyClassifier
import tensorboardX
"""Script for testing the classifier by running it on the iris dataset.
"""
TO_TENSORBOARD = True # write results and stats to tensorboard?
data = load_iris()
cols = [c.replace(" ", "_").replace("_(cm)", "") for c in data.feature_names]
iris = pd.DataFrame(data.data, columns=cols)
y = pd.Series(data.target)
train_X, test_X, train_y, test_y = train_test_split(iris, y, test_size=50)
classes = {name: val for (name, val) in zip(data.target_names, range(3))}
antecendent_terms = {
col: ["v.narrow", "narrow", "medium", "wide", "v.wide"]
if "width" in col
else ["v.short", "short", "medium", "long", "v.long"]
for col in cols
}
if TO_TENSORBOARD:
logdir = Path(f"tb_logs/iris/{datetime.now().strftime('%Y%m%d-%H%M%S')}")
logdir.mkdir(parents=True, exist_ok=True)
tensorboard_writer = tensorboardX.SummaryWriter(str(logdir))
else:
tensorboard_writer = None
classifier = FuzzyClassifier(
population_size=20,
elite_size=3,
n_iter=5,
mutation_prob=0.5,
crossover_prob=0.5,
min_tree_height=1,
max_tree_height=3,
min_rules=4,
max_rules=6,
whole_rule_prob=0.1,
batch_size=20,
)
classifier.fit(
train_X,
train_y,
classes,
antecedent_terms=antecendent_terms,
tensorboard_writer=tensorboard_writer,
)
print(f"Best Rule: size = {len(classifier.best)}")
print(classifier.best_str)
predictions = classifier.predict(test_X)
confusion = pd.DataFrame(
data=confusion_matrix(test_y, predictions),
columns=data.target_names,
index=data.target_names,
)
print(confusion)
if tensorboard_writer:
tensorboard_writer.add_text("confusion", confusion.to_markdown())
tensorboard_writer.close()