Skip to content

Commit f2ca48c

Browse files
authored
Add files via upload
1 parent 1f7bbf2 commit f2ca48c

File tree

9 files changed

+1009
-0
lines changed

9 files changed

+1009
-0
lines changed

FS/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#

FS/de.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#[1997]-"Differential evolution - A simple and efficient heuristic for global optimization over continuous spaces"
2+
3+
import numpy as np
4+
from numpy.random import rand
5+
from FS.functionHO import Fun
6+
7+
8+
def init_position(lb, ub, N, dim):
9+
X = np.zeros([N, dim], dtype='float')
10+
for i in range(N):
11+
for d in range(dim):
12+
X[i,d] = lb[0,d] + (ub[0,d] - lb[0,d]) * rand()
13+
14+
return X
15+
16+
17+
def binary_conversion(X, thres, N, dim):
18+
Xbin = np.zeros([N, dim], dtype='int')
19+
for i in range(N):
20+
for d in range(dim):
21+
if X[i,d] > thres:
22+
Xbin[i,d] = 1
23+
else:
24+
Xbin[i,d] = 0
25+
26+
return Xbin
27+
28+
29+
def boundary(x, lb, ub):
30+
if x < lb:
31+
x = lb
32+
if x > ub:
33+
x = ub
34+
35+
return x
36+
37+
38+
def jfs(xtrain, ytrain, opts):
39+
# Parameters
40+
ub = 1
41+
lb = 0
42+
thres = 0.5
43+
CR = 0.9 # crossover rate
44+
F = 0.5 # factor
45+
46+
N = opts['N']
47+
max_iter = opts['T']
48+
if 'CR' in opts:
49+
CR = opts['CR']
50+
if 'F' in opts:
51+
F = opts['F']
52+
53+
# Dimension
54+
dim = np.size(xtrain, 1)
55+
if np.size(lb) == 1:
56+
ub = ub * np.ones([1, dim], dtype='int')
57+
lb = lb * np.ones([1, dim], dtype='int')
58+
59+
# Initialize position & velocity
60+
X = init_position(lb, ub, N, dim)
61+
62+
# Binary conversion
63+
Xbin = binary_conversion(X, thres, N, dim)
64+
65+
# Fitness at first iteration
66+
fit = np.zeros([N, 1], dtype='float')
67+
fitG = float('inf')
68+
for i in range(N):
69+
fit[i,0] = Fun(xtrain, ytrain, Xbin[i,:], opts)
70+
if fit[i,0] < fitG:
71+
Xgb = X[i,:]
72+
fitG = fit[i,0]
73+
Gbin = Xbin[i,:]
74+
75+
# Pre
76+
V = np.zeros([N, dim], dtype='float')
77+
U = np.zeros([N, dim], dtype='float')
78+
curve = np.zeros([1, max_iter], dtype='float')
79+
t = 0
80+
81+
curve[0,t] = fitG
82+
print("Generation:", t + 1)
83+
print("Best (DE):", curve[0,t])
84+
t += 1
85+
86+
while t < max_iter:
87+
for i in range(N):
88+
# Choose r1, r2, r3 randomly, but not equal to i
89+
RN = np.random.permutation(N)
90+
for j in range(N):
91+
if RN[j] == i:
92+
RN = np.delete(RN, j)
93+
break
94+
95+
r1 = RN[0]
96+
r2 = RN[1]
97+
r3 = RN[2]
98+
# mutation (2)
99+
for d in range(dim):
100+
V[i,d] = X[r1,d] + F * (X[r2,d] - X[r3,d])
101+
# Boundary
102+
V[i,d] = boundary(V[i,d], lb[0,d], ub[0,d])
103+
104+
# Random one dimension from 1 to dim
105+
index = np.random.randint(low = 0, high = dim)
106+
# crossover (3-4)
107+
for d in range(dim):
108+
if rand() <= CR or d == index:
109+
U[i,d] = V[i,d]
110+
else:
111+
U[i,d] = X[i,d]
112+
113+
# Binary conversion
114+
Ubin = binary_conversion(U, thres, N, dim)
115+
# Selection
116+
for i in range(N):
117+
fitU = Fun(xtrain, ytrain, Ubin[i,:], opts)
118+
if fitU <= fit[i,0]:
119+
X[i,:] = U[i,:]
120+
fit[i,0] = fitU
121+
if fitU < fitG:
122+
Xgb = U[i,:]
123+
fitG = fitU
124+
Gbin = Ubin[i,:]
125+
126+
127+
# Store result
128+
curve[0,t] = fitG
129+
print("Generation:", t + 1)
130+
print("Best (DE):", curve[0,t])
131+
t += 1
132+
133+
134+
# Best feature subset
135+
pos = np.asarray(range(0, dim))
136+
sel_index = pos[Gbin == 1]
137+
num_feat = len(sel_index)
138+
# Create dictionary
139+
de_data = {'sf': sel_index, 'c': curve, 'nf': num_feat}
140+
141+
return de_data
142+
143+
144+
145+
146+

FS/functionHO.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import numpy as np
2+
from sklearn.neighbors import KNeighborsClassifier
3+
4+
5+
# error rate
6+
def error_rate(xtrain, ytrain, x, opts):
7+
# parameters
8+
k = opts['k']
9+
fold = opts['fold']
10+
xt = fold['xt']
11+
yt = fold['yt']
12+
xv = fold['xv']
13+
yv = fold['yv']
14+
15+
# Number of instances
16+
num_train = np.size(xt, 0)
17+
num_valid = np.size(xv, 0)
18+
# Define selected features
19+
xtrain = xt[:, x == 1]
20+
ytrain = yt.reshape(num_train) # Solve bug
21+
xvalid = xv[:, x == 1]
22+
yvalid = yv.reshape(num_valid) # Solve bug
23+
# Training
24+
mdl = KNeighborsClassifier(n_neighbors = k)
25+
mdl.fit(xtrain, ytrain)
26+
# Prediction
27+
pred = mdl.predict(xvalid)
28+
correct = 0
29+
for i in range(num_valid):
30+
if pred[i] == yvalid[i]:
31+
correct += 1
32+
33+
accuracy = correct / num_valid
34+
error = 1 - accuracy
35+
36+
return error
37+
38+
39+
# Error rate & Feature size
40+
def Fun(xtrain, ytrain, x, opts):
41+
# Parameters
42+
alpha = 0.99
43+
beta = 1 - alpha
44+
# Original feature size
45+
max_feat = len(x)
46+
# Number of selected features
47+
num_feat = sum(x == 1)
48+
# Solve if no feature selected
49+
if num_feat == 0:
50+
cost = 1
51+
else:
52+
# Get error rate
53+
error = error_rate(xtrain, ytrain, x, opts)
54+
# Objective function
55+
cost = alpha * error + beta * (num_feat / max_feat)
56+
57+
return cost
58+

FS/ga.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import numpy as np
2+
from numpy.random import rand
3+
from FS.functionHO import Fun
4+
5+
6+
def init_position(N, dim):
7+
X = np.zeros([N, dim], dtype='int')
8+
for i in range(N):
9+
for d in range(dim):
10+
if rand() > 0.5:
11+
X[i,d] = 1
12+
else:
13+
X[i,d] = 0
14+
15+
return X
16+
17+
18+
def roulette_wheel(prob):
19+
num = len(prob)
20+
C = np.cumsum(prob)
21+
P = rand()
22+
for i in range(num):
23+
if C[i] > P:
24+
index = i;
25+
break
26+
27+
return index
28+
29+
30+
def jfs(xtrain, ytrain, opts):
31+
# Parameters
32+
CR = 0.8 # crossover rate
33+
MR = 0.01 # mutation rate
34+
35+
N = opts['N']
36+
max_iter = opts['T']
37+
if 'CR' in opts:
38+
CR = opts['CR']
39+
if 'MR' in opts:
40+
MR = opts['MR']
41+
42+
# Dimension
43+
dim = np.size(xtrain, 1)
44+
# Initialize position & velocity
45+
X = init_position(N, dim)
46+
47+
# Fitness at first iteration
48+
fit = np.zeros([N, 1], dtype='float')
49+
fitG = float('inf')
50+
for i in range(N):
51+
fit[i,0] = Fun(xtrain, ytrain, X[i,:], opts)
52+
if fit[i,0] < fitG:
53+
Xgb = X[i,:]
54+
fitG = fit[i,0]
55+
56+
# Pre
57+
curve = np.zeros([1, max_iter], dtype='float')
58+
t = 0
59+
60+
curve[0,t] = fitG
61+
print("Generation:", t + 1)
62+
print("Best (GA):", curve[0,t])
63+
t += 1
64+
65+
while t < max_iter:
66+
# Probability
67+
inv_fit = 1 / (1 + fit)
68+
prob = inv_fit / np.sum(inv_fit)
69+
70+
# Number of crossovers
71+
Nc = 0
72+
for i in range(N):
73+
if rand() < CR:
74+
Nc += 1
75+
76+
x1 = np.zeros([Nc, dim], dtype='int')
77+
x2 = np.zeros([Nc, dim], dtype='int')
78+
for i in range(Nc):
79+
# Parent selection
80+
k1 = roulette_wheel(prob)
81+
k2 = roulette_wheel(prob)
82+
P1 = X[k1,:]
83+
P2 = X[k2,:]
84+
# Random one dimension from 1 to dim
85+
index = np.random.randint(low = 1, high = dim)
86+
# Crossover
87+
x1[i,:] = np.concatenate((P1[0:index] , P2[index:]))
88+
x2[i,:] = np.concatenate((P2[0:index] , P1[index:]))
89+
# Mutation
90+
for d in range(dim):
91+
if rand() < MR:
92+
x1[i,d] = 1 - x1[i,d]
93+
94+
if rand() < MR:
95+
x2[i,d] = 1 - x2[i,d]
96+
97+
98+
# Merge two group into one
99+
Xnew = np.concatenate((x1 , x2), axis=0)
100+
101+
# Fitness
102+
Fnew = np.zeros([2 * Nc, 1], dtype='float')
103+
for i in range(2 * Nc):
104+
Fnew[i,0] = Fun(xtrain, ytrain, Xnew[i,:], opts)
105+
if Fnew[i,0] < fitG:
106+
Xgb = Xnew[i,:]
107+
fitG = Fnew[i,0]
108+
109+
# Store result
110+
curve[0,t] = fitG
111+
print("Generation:", t + 1)
112+
print("Best (GA):", curve[0,t])
113+
t += 1
114+
115+
# Elitism
116+
XX = np.concatenate((X , Xnew), axis=0)
117+
FF = np.concatenate((fit , Fnew), axis=0)
118+
# Sort in ascending order
119+
ind = np.argsort(FF, axis=0)
120+
for i in range(N):
121+
X[i,:] = XX[ind[i,0],:]
122+
fit[i,0] = FF[ind[i,0]]
123+
124+
125+
# Best feature subset
126+
pos = np.asarray(range(0, dim))
127+
sel_index = pos[Xgb == 1]
128+
num_feat = len(sel_index)
129+
# Create dictionary
130+
ga_data = {'sf': sel_index, 'c': curve, 'nf': num_feat}
131+
132+
return ga_data
133+
134+
135+
136+
137+
138+
139+
140+
141+

0 commit comments

Comments
 (0)