-
Notifications
You must be signed in to change notification settings - Fork 15
/
HSGW.py
231 lines (190 loc) · 7.4 KB
/
HSGW.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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 = 100
pop_size = 20
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 HSGW(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)
for n in range(MaxIter):
fit = []
for i in range(pop_size):
fit.append(fitness(pop[i], trainX, testX, trainy, testy))
ind = np.argsort(fit)
alpha = pop[ind[0]]
alpha_fit = fit[ind[0]]
beta = pop[ind[1]]
beta_fit = pop[ind[1]]
delta = pop[ind[2]]
delta_fit = fit[ind[2]]
a=2-n*((2)/MaxIter); # a decreases linearly fron 2 to 0
# Update the Position of search agents including omegas
for i in range(pop_size):
for j in range (dimension):
r1=random.random() # r1 is a random number in [0,1]
r2=random.random() # r2 is a random number in [0,1]
A1=2*a*r1-a; # Equation (3.3)
C1=2*r2; # Equation (3.4)
D_alpha=abs(C1*alpha[j]-pop[i,j]); # Equation (3.5)-part 1
X1=alpha[j]-A1*D_alpha; # Equation (3.6)-part 1
r1=random.random()
r2=random.random()
A2=2*a*r1-a; # Equation (3.3)
C2=2*r2; # Equation (3.4)
D_beta=abs(C2*beta[j]-pop[i,j]); # Equation (3.5)-part 2
X2=beta[j]-A2*D_beta; # Equation (3.6)-part 2
r1=random.random()
r2=random.random()
A3=2*a*r1-a; # Equation (3.3)
C3=2*r2; # Equation (3.4)
D_delta=abs(C3*delta[j]-pop[i,j]); # Equation (3.5)-part 3
X3=delta[j]-A3*D_delta; # Equation (3.5)-part 3
pop[i,j]=(X1+X2+X3)/3 # Equation (3.7)
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 * alpha - pop[j] )
pop[j] = alpha - 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(alpha - pop[j])
pop[j] = D1 * np.exp(b * l) * np.cos(2 * np.pi * l) + alpha
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]]
bestfit = fit[ind[0]]
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 = HSGW(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)
with open("HSGW_result.csv","a") as f:
print(datasetname,"%.2f"%(100*maxx),currFeat,sep=',',file=f,end=',')
for x in bagent:
print(int(x),end=' ',file=f)
print('',file=f)