-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.py
195 lines (171 loc) · 7.14 KB
/
program.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
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
plt.rcParams.update({'figure.max_open_warning': 0})
import numpy as np
import math
import os
import shutil
import imageio
from sklearn import preprocessing
from scipy.spatial.distance import euclidean as distance
class Dataset:
def __init__(self, file, nuber_of_neurons, usecols):
self.data = np.loadtxt(file, delimiter=',', usecols=usecols)
self.scale()
self.number_of_neurons = nuber_of_neurons
self.length = len(self.data)
self.generate_neurons()
def random_point(self):
point = self.data[np.random.randint(self.length)]
return point
def generate_neurons(self):
neurons = []
for i in range(self.number_of_neurons):
neurons.append(self.random_point())
self.neurons = np.asarray(neurons)
def scale(self):
self.data = preprocessing.scale(self.data)
def print_dataset(self):
print(self.data)
def count_groups(self):
counter = np.zeros(self.neurons.shape[0])
for row in self.data:
minn = 0
min = distance(row, self.neurons[0])
for n in range(1, self.number_of_neurons):
dist = distance(row, self.neurons[n])
if dist < min:
min = dist
minn = n
counter[minn] += 1
print(counter)
def sorted_neurons_by_distance(self, point):
distances = []
for i in range(self.number_of_neurons):
distances.append(distance(self.neurons[i], point))
distances = np.reshape(distances, (self.number_of_neurons, 1))
self.neurons = np.hstack((self.neurons, distances))
self.neurons = self.neurons[np.argsort(self.neurons[:, -1])]
self.neurons = np.delete(self.neurons, -1, 1)
return self.neurons
def get_nearest_n(self, point):
nearest = distance(point, self.neurons[0])
nearestn = 0
for n in range(self.number_of_neurons):
dist = distance(point, self.neurons[n])
if dist < nearest:
nearest = dist
nearestn = n
return nearestn
def get_nearest_neuron(self, point):
return self.neurons[self.get_nearest_n(point)]
def generate_plot(self, col1, col2, filename):
plt.ion()
plt.clf()
colors = ['blue', 'yellow', 'green', 'purple', 'orange', 'black', 'brown']
if not os.path.isdir('tmp'):
os.makedirs('tmp')
filename = str(filename).rjust(4, '0')
self.neurons = self.sorted_neurons_by_distance(np.average(self.neurons))
for row in self.data:
n = self.get_nearest_n(row)
plt.plot(row[col1], row[col2], marker='o', markersize=3, color=colors[n % 7])
for neuron in self.neurons:
plt.plot(neuron[col1], neuron[col2], marker='x', markersize=5, color='red')
plt.show()
plt.pause(0.2)
filename = 'tmp/' + filename + '.png'
plt.savefig(filename)
def shuffle(self):
np.random.shuffle(self.data)
def create_animation(self, name):
list = os.listdir('tmp')
with imageio.get_writer(name, mode='I', duration=0.1) as writer:
for filename in list:
image = imageio.imread('tmp/' + filename)
writer.append_data(image)
writer.close()
shutil.rmtree('tmp')
class NeuralGas:
def __init__(self, dataset, lamb, epochs, lrate, generate_plot=False, col1=None, col2=None):
lrate_i = 0.002
for e in range(epochs):
if generate_plot:
dataset.generate_plot(col1, col2, e)
dataset.shuffle()
for row in dataset.data:
dataset.neurons = dataset.sorted_neurons_by_distance(row)
for n in range(dataset.number_of_neurons):
dataset.neurons[n] += lrate * pow(math.e, -n / lamb) * (row - dataset.neurons[n])
if lrate - lrate_i > 0:
lrate -= lrate_i
dataset.create_animation('neuralgas.gif')
dataset.count_groups()
dataset.generate_neurons()
class Kohonen:
def __init__(self, dataset, lamb, epochs, lrate, generate_plot=False, col1=None, col2=None):
lrate_i = 0.002
for e in range(epochs):
if generate_plot:
dataset.generate_plot(col1, col2, e)
dataset.shuffle()
for row in dataset.data:
for n in range(dataset.number_of_neurons):
nearest = dataset.get_nearest_neuron(row)
dist = distance(dataset.neurons[n], nearest)
dataset.neurons[n] += lrate * self.gaussian_neighborhood(dist, lamb) * (row - dataset.neurons[n])
if lrate - lrate_i > 0:
lrate -= lrate_i
dataset.create_animation('kohonen.gif')
dataset.count_groups()
dataset.generate_neurons()
def gaussian_neighborhood(self, distance, lamb):
return np.exp(-(pow(distance, 2) / (2.0 * pow(lamb, 2))))
class Kmeans:
def __init__(self, dataset, generate_plot=False, col1=None, col2=None):
self.i = 0
self.q_error = self.quantization_error(dataset)
self.neurons = dataset.neurons
for i in range(10):
dataset.generate_neurons()
self.kmeans(dataset, generate_plot, col1, col2)
dataset.neurons = self.neurons
self.kmeans(dataset, generate_plot, col1, col2)
dataset.create_animation('kmeans.gif')
dataset.count_groups()
dataset.generate_neurons()
def kmeans(self, dataset, generate_plot=False, col1=None, col2=None):
start_neurons = dataset.neurons
while True:
previous_neurons = dataset.neurons.copy()
if generate_plot:
dataset.generate_plot(col1, col2, self.i)
self.i += 1
for g in range(dataset.number_of_neurons):
group = np.empty(shape=(0, len(dataset.data[0])))
for r in range(dataset.length):
distances = np.zeros(shape=dataset.number_of_neurons)
for n in range(dataset.number_of_neurons):
distances[n] = distance(dataset.neurons[n], dataset.data[r])
index = np.where(distances == min(distances))
index = int(index[0][0])
if g == index:
group = np.vstack((group, dataset.data[r]))
dataset.neurons[g] = np.mean(group, axis=0)
if np.array_equal(dataset.neurons, previous_neurons):
break
error = self.quantization_error(dataset)
if error < self.q_error:
self.q_error = error
self.neurons = start_neurons
def quantization_error(self, dataset):
sum = 0
for row in dataset.data:
neuron = dataset.get_nearest_neuron(row)
sum += distance(row, neuron)
return sum
dataset = Dataset('iris.data', 3, (0, 1, 2, 3))
neuralgas = NeuralGas(dataset, 0.33, 100, 0.1, True, 2, 3)
kohonen = Kohonen(dataset, 2, 100, 0.1, True, 2, 3)
kmeans = Kmeans(dataset, True, 2, 3)