-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-iris.py
executable file
·89 lines (77 loc) · 2.12 KB
/
example-iris.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
# Iris Flower Data Set via
# http://archive.ics.uci.edu/ml/datasets/Iris
import numpy as np
import pso
data = []
labels = {
"Iris-setosa": 1.0,
"Iris-versicolor": 2.0,
"Iris-virginica": 3.0,
}
with open("iris.txt") as file:
for l in file.readlines():
l = l.rstrip().split(",")
for i in range(4):
l[i] = float(l[i])
l[4] = labels[l[4]]
data.append(np.array(l))
training = []
testing = []
for i in range(3):
for j in range(50):
idx = j + 50*i
if np.random.random() < 0.50:
training.append(data[idx])
else:
testing.append(data[idx])
def plot(d):
import matplotlib.pyplot as plt
c = ["red", "green", "blue"]
for di in d:
plt.scatter(di[0], di[2], color=c[int(di[4]-1)], marker="o")
plt.scatter(di[1], di[3], color=c[int(di[4]-1)], marker="x")
plt.scatter(di[0], di[3], color=c[int(di[4]-1)], marker="^")
plt.scatter(di[1], di[2], color=c[int(di[4]-1)], marker="v")
plt.show()
def softmax(x):
e = np.exp(x)
s = e / sum(e)
return s
def nn(w, x):
# 4 inputs + bias; 4 nodes + bias; 3 outputs
# 5*4 + 5*3 = 35 weights
x = np.append(x, 1)
h0 = np.tanh(np.dot(w[0:5], x))
h1 = np.tanh(np.dot(w[5:10], x))
h2 = np.tanh(np.dot(w[10:15], x))
h3 = np.tanh(np.dot(w[15:20], x))
h = np.array([h0, h1, h2, h3, 1.0])
o0 = np.tanh(np.dot(w[20:25], h))
o1 = np.tanh(np.dot(w[25:30], h))
o2 = np.tanh(np.dot(w[30:35], h))
return softmax(np.array([o0, o1, o2]))
def train(w):
score = 0
for i in range(len(training)):
correct = int(training[i][4] - 1)
predicted = nn(w, training[i][:4])
score -= predicted[correct]
return score
def test(w):
score = 0
for i in range(len(testing)):
correct = int(testing[i][4] - 1)
predicted = nn(w, testing[i][:4])
predictedIndex = np.argmax(predicted)
print predicted, predictedIndex, correct
if predictedIndex == correct:
score += 1
print "%d / %d"%(score, len(testing))
bounds = [[-1.0, 1.0]]*35
optimizer = pso.Optimizer(train, bounds)
print optimizer.populationSize
optimizer.stepUntil(1e-6, logging=True, waitIterations=20)
print optimizer.globalBestFitness
print optimizer.globalBestValue
print len(optimizer.globalBestHistory)
test(optimizer.globalBestValue)