-
Notifications
You must be signed in to change notification settings - Fork 15
/
WOA.py
186 lines (159 loc) · 5.72 KB
/
WOA.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import numpy as np
import random
from copy import deepcopy
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import math,time,sys
from matplotlib import pyplot
import pandas as pd
from datetime import datetime
from functools import partial
import seaborn as sns
from sklearn.metrics import roc_auc_score
from sklearn.neural_network import MLPClassifier
from sklearn.naive_bayes import GaussianNB
MaxIter = 70
pop_size = 8
omega = 0.99
def initialise(partCount, dim, trainX, testX, trainy, testy):
population=np.zeros((partCount,dim))
minn = 1
maxx = math.floor(0.5*dim)
if maxx<minn:
maxx = minn + 1
#not(c[i].all())
for i in range(partCount):
random.seed(i**3 + 10 + time.time() )
no = random.randint(minn,maxx)
if no == 0:
no = 1
random.seed(time.time()+ 100)
pos = random.sample(range(0,dim-1),no)
for j in pos:
population[i][j]=1
return population
def fitness(agent, trainX, testX, trainy, testy):
# print(agent)
cols=np.flatnonzero(agent)
# print(cols)
val=1
if np.shape(cols)[0]==0:
return val
clf=KNeighborsClassifier(n_neighbors=5)
#clf=MLPClassifier(alpha=0.001, hidden_layer_sizes=(1000,500,100),max_iter=2000,random_state=4)
train_data=trainX[:,cols]
test_data=testX[:,cols]
clf.fit(train_data,trainy)
val=1-clf.score(test_data,testy)
#in case of multi objective []
set_cnt=sum(agent)
set_cnt=set_cnt/np.shape(agent)[0]
val=omega*val+(1-omega)*set_cnt
return val
def test_accuracy(agent, trainX, testX, trainy, testy):
cols=np.flatnonzero(agent)
val=1
if np.shape(cols)[0]==0:
return val
# clf = RandomForestClassifier(n_estimators=300)
#clf=MLPClassifier(alpha=0.001, hidden_layer_sizes=(1000,500,100),max_iter=2000,random_state=4)
clf=KNeighborsClassifier(n_neighbors=5)
# clf=MLPClassifier( alpha=0.01, max_iterno=1000) #hidden_layer_sizes=(1000,500,100)
#cross=4
#test_size=(1/cross)
#X_train, X_test, y_train, y_test = train_test_split(trainX, trainy, stratify=trainy,test_size=test_size)
train_data=trainX[:,cols]
test_data=testX[:,cols]
clf.fit(train_data,trainy)
val=clf.score(test_data,testy)
return val
def onecnt(agent):
return sum(agent)
def sigmoid(gamma):
if gamma < 0:
return 1 - 1/(1 + math.exp(gamma))
else:
return 1/(1 + math.exp(-gamma))
def WOA(dataset):
df = pd.read_csv(dataset)
a, b = np.shape(df)
data = df.values[:,0:b-1]
label = df.values[:,b-1]
dimension = data.shape[1]
cross = 5
test_size = (1/cross)
trainX, testX, trainy, testy = train_test_split(data, label,stratify=label ,test_size=test_size,random_state=(7+17*int(time.time()%1000)))
clf=KNeighborsClassifier(n_neighbors=5)
clf.fit(trainX,trainy)
val=clf.score(testX,testy)
whole_accuracy = val
print("Total Acc: ",val)
pop = initialise(pop_size, dimension, trainX, testX, trainy, testy)
fit = []
for i in range(pop_size):
fit.append(fitness(pop[i], trainX, testX, trainy, testy))
ind = np.argsort(fit)
gbest = pop[ind[0]].copy()
gbest_fit = fit[ind[0]].copy()
for n in range(MaxIter):
a = 2 - 2 * n / (MaxIter - 1) # linearly decreased from 2 to 0
for j in range(pop_size):
r = np.random.rand()
A = 2 * a * r - a
C = 2 * r
l = np.random.uniform(-1, 1)
p = np.random.rand()
b = 1
if (p < 0.5) :
if np.abs(A) < 1:
D = np.abs(C * gbest - pop[j] )
pop[j] = gbest - A * D
else :
x_rand = pop[np.random.randint(pop_size)]
D = np.abs(C * x_rand - pop[j])
pop[j] = (x_rand - A * D)
else:
D1 = np.abs(gbest - pop[j])
pop[j] = D1 * np.exp(b * l) * np.cos(2 * np.pi * l) + gbest
for i in range(pop_size):
for j in range(dimension):
if (sigmoid(pop[i][j]) > random.random()):
pop[i][j] = 1
else:
pop[i][j] = 0
ind = np.argsort(fit)
bestpop = pop[ind[0]].copy()
bestfit = fit[ind[0]].copy()
testAcc = test_accuracy(bestpop, trainX, testX, trainy, testy)
featCnt = onecnt(bestpop)
#print("best agent: ", bestpop)
print("Test Accuracy: ", testAcc)
print("#Features: ", featCnt)
return testAcc, featCnt, bestpop
datasetlist = ["BreastCancer.csv", "BreastEW.csv", "CongressEW.csv", "Exactly.csv", "Exactly2.csv", "HeartEW.csv", "Ionosphere.csv", "Lymphography.csv", "M-of-n.csv", "PenglungEW.csv", "Sonar.csv", "SpectEW.csv", "Tic-tac-toe.csv", "Vote.csv", "Wine.csv", "Zoo.csv","KrVsKpEW.csv", "WaveformEW.csv" ]
for datasetname in datasetlist:
print(datasetname)
accuArr = []
featArr = []
agenArr = []
#start_time = datetime.now()
for i in range(15):
# print(i)
testAcc, featCnt, gbest = WOA(datasetname)
# print(testAcc)
accuArr.append(testAcc)
featArr.append(featCnt)
agenArr.append(gbest)
#time_required = datetime.now() - start_time
maxx = max(accuArr)
k = np.argsort(accuArr)
bagent = agenArr[k[-1]]
currFeat= 20000
for i in range(np.shape(accuArr)[0]):
if accuArr[i]==maxx and featArr[i] < currFeat:
currFeat = featArr[i]
bagent = agenArr[i]
datasetname = datasetname.split('.')[0]
print(datasetname)
print(maxx,currFeat)
print(bagent)