-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenetic Algorithm.txt
More file actions
119 lines (101 loc) · 3.73 KB
/
Copy pathGenetic Algorithm.txt
File metadata and controls
119 lines (101 loc) · 3.73 KB
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
117
118
119
# Function to get selected features
def get_selected_features(selected_features, all_features):
selected_f = []
for sample in all_features:
s_f = []
for i in range(len(selected_features)):
if selected_features[i] == 1:
s_f.append(sample[i])
selected_f.append(s_f)
return selected_f
# Function to get predicted labels
def get_predicted_labels(class_prob):
predicted_labels = []
for p in class_prob:
predicted_labels.append(p.argmax() + 1)
return predicted_labels
# Function to calculate misclassification percentages for each class
def get_class_miss_percentages(true_labels, predicted_labels):
c1_missed = 0
c1_tot = 0
c2_missed = 0
c2_tot = 0
c3_missed = 0
c3_tot = 0
for i in range(len(true_labels)):
if true_labels[i] == 1:
c1_tot += 1
if true_labels[i] != predicted_labels[i]:
c1_missed += 1
if true_labels[i] == 2:
c2_tot += 1
if true_labels[i] != predicted_labels[i]:
c2_missed += 1
if true_labels[i] == 3:
c3_tot += 1
if true_labels[i] != predicted_labels[i]:
c3_missed += 1
c1_miss_percent = (100.00 * c1_missed) / c1_tot
c2_miss_percent = (100.00 * c2_missed) / c2_tot
c3_miss_percent = (100.00 * c3_missed) / c3_tot
if c1_miss_percent <= 1:
c1_miss_percent = 1
if c2_miss_percent <= 1:
c2_miss_percent = 1
if c3_miss_percent <= 1:
c3_miss_percent = 1
print("Missed samples for each class:", c1_missed, c2_missed, c3_missed)
return c1_miss_percent, c2_miss_percent, c3_miss_percent
# Function to load data from files and convert data types
def load_data():
train = open("ann-train.data", "r")
training_data = []
for line in train:
training_data.append(line.strip().split(" "))
convert_types(training_data)
test = open("ann-test.data", "r")
test_data = []
for line in test:
test_data.append(line.strip().split(" "))
convert_types(test_data)
training_features = []
training_labels = []
for sample in training_data:
training_features.append(sample[:-1])
training_labels.append(sample[-1])
test_features = []
test_labels = []
for sample in test_data:
test_features.append(sample[:-1])
test_labels.append(sample[-1])
cost = open("ann-thyroid.cost", "r")
costs = []
for line in cost:
costs.append(float(line.strip().split(":")[1]))
costs.append(0) # 21st feature is a comb. of 19th and 20th features
return training_features, training_labels, test_features, test_labels, costs
# Function to convert data types to the appropriate format
def convert_types(data):
for row in data:
row[0] = float(row[0].strip())
row[1] = int(row[1].strip())
row[2] = int(row[2].strip())
row[3] = int(row[3].strip())
row[4] = int(row[4].strip())
row[5] = int(row[5].strip())
row[6] = int(row[6].strip())
row[7] = int(row[7].strip())
row[8] = int(row[8].strip())
row[9] = int(row[9].strip())
row[10] = int(row[10].strip())
row[11] = int(row[11].strip())
row[12] = int(row[12].strip())
row[13] = int(row[13].strip())
row[14] = int(row[14].strip())
row[15] = int(row[15].strip())
row[16] = float(row[16].strip())
row[17] = float(row[17].strip())
row[18] = float(row[18].strip())
row[19] = float(row[19].strip())
row[20] = float(row[20].strip())
row[21] = int(row[21].strip())