- Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathDemo_PSO.py
61 lines (49 loc) · 1.65 KB
/
Demo_PSO.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
importnumpyasnp
importpandasaspd
fromsklearn.neighborsimportKNeighborsClassifier
fromsklearn.model_selectionimporttrain_test_split
fromFS.psoimportjfs# change this to switch algorithm
importmatplotlib.pyplotasplt
# load data
data=pd.read_csv('ionosphere.csv')
data=data.values
feat=np.asarray(data[:, 0:-1])
label=np.asarray(data[:, -1])
# split data into train & validation (70 -- 30)
xtrain, xtest, ytrain, ytest=train_test_split(feat, label, test_size=0.3, stratify=label)
fold= {'xt':xtrain, 'yt':ytrain, 'xv':xtest, 'yv':ytest}
# parameter
k=5# k-value in KNN
N=10# number of particles
T=100# maximum number of iterations
opts= {'k':k, 'fold':fold, 'N':N, 'T':T}
# perform feature selection
fmdl=jfs(feat, label, opts)
sf=fmdl['sf']
# model with selected features
num_train=np.size(xtrain, 0)
num_valid=np.size(xtest, 0)
x_train=xtrain[:, sf]
y_train=ytrain.reshape(num_train) # Solve bug
x_valid=xtest[:, sf]
y_valid=ytest.reshape(num_valid) # Solve bug
mdl=KNeighborsClassifier(n_neighbors=k)
mdl.fit(x_train, y_train)
# accuracy
y_pred=mdl.predict(x_valid)
Acc=np.sum(y_valid==y_pred) /num_valid
print("Accuracy:", 100*Acc)
# number of selected features
num_feat=fmdl['nf']
print("Feature Size:", num_feat)
# plot convergence
curve=fmdl['c']
curve=curve.reshape(np.size(curve,1))
x=np.arange(0, opts['T'], 1.0) +1.0
fig, ax=plt.subplots()
ax.plot(x, curve, 'o-')
ax.set_xlabel('Number of Iterations')
ax.set_ylabel('Fitness')
ax.set_title('PSO')
ax.grid()
plt.show()