Self Organizing Map

from future import division
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from sklearn import datasets
from neupy import algorithms, environment
!pip install neupy
environment.reproducible()
GRID_HEIGHT = 40
GRID_WIDTH = 40
digits = datasets.load_digits()
data = digits.data
sofm = algorithms.SOFM(
n_inputs=64,
features_grid=(GRID_HEIGHT, GRID_WIDTH),
# Learning radius defines area within which we find
# winning neuron neighbours. The higher the value
# the more values we will be updated after each iteration.
learning_radius=5,
# Every 20 epochs learning radius will be reduced by 1.
reduce_radius_after=20,
step=0.5,
std=1,
shuffle_data=True,
verbose=True,
)
Output:
Main information [ALGORITHM] SOFM [OPTION] distance = ['euclid', 'neg_euclid_distance'] [OPTION] features_grid = [40, 40] [OPTION] grid_type = ['rectangle', 'find_neighbours_on_rect_grid', 'find_step_scaler_on_rect_grid'] [OPTION] learning_radius = 5 [OPTION] n_inputs = 64 [OPTION] n_outputs = None [OPTION] reduce_radius_after = 20 [OPTION] reduce_std_after = 100 [OPTION] reduce_step_after = 100 [OPTION] show_epoch = 1 [OPTION] shuffle_data = True [OPTION] signals = None [OPTION] std = 1 [OPTION] step = 0.5 [OPTION] verbose = True [OPTION] weight = Normal(mean=0, std=0.01)
sofm.train(data, epochs=100)
clusters = sofm.predict(data).argmax(axis=1)
print("Building visualization…")
plt.figure(figsize=(12, 11))
grid = gridspec.GridSpec(GRID_HEIGHT, GRID_WIDTH)
grid.update(wspace=0, hspace=0)

Output:
#1 : [1 sec] train: 2.421919
#2 : [1 sec] train: 2.148798
#3 : [1 sec] train: 2.097239
#4 : [1 sec] train: 2.085253
#5 : [1 sec] train: 2.082984
#6 : [1 sec] train: 2.059884
#7 : [1 sec] train: 2.070770
#8 : [1 sec] train: 2.065354
#9 : [1 sec] train: 2.067114
#10 : [1 sec] train: 2.061239
#11 : [1 sec] train: 2.050616
#12 : [1 sec] train: 2.058361
#13 : [1 sec] train: 2.045913
#14 : [1 sec] train: 2.043559
#15 : [1 sec] train: 2.047993
#16 : [1 sec] train: 2.052609
#17 : [1 sec] train: 2.040919
#18 : [1 sec] train: 2.035039
#19 : [1 sec] train: 2.045693
#20 : [1 sec] train: 1.905370
#21 : [1 sec] train: 1.899415
#22 : [1 sec] train: 1.900669
#23 : [1 sec] train: 1.896967
#24 : [1 sec] train: 1.882780
#25 : [1 sec] train: 1.891311
#26 : [1 sec] train: 1.884189
#27 : [1 sec] train: 1.879071
#28 : [1 sec] train: 1.883792
#29 : [1 sec] train: 1.881719
#30 : [1 sec] train: 1.880335
#31 : [1 sec] train: 1.880402
#32 : [1 sec] train: 1.882312
#33 : [1 sec] train: 1.877617
#34 : [1 sec] train: 1.881437
#35 : [1 sec] train: 1.865803
#36 : [1 sec] train: 1.872404
#37 : [1 sec] train: 1.855602
#38 : [1 sec] train: 1.865701
#39 : [1 sec] train: 1.868459
#40 : [1 sec] train: 1.730191
#41 : [1 sec] train: 1.726304
#42 : [1 sec] train: 1.722028
#43 : [1 sec] train: 1.722532
#44 : [1 sec] train: 1.718585
#45 : [1 sec] train: 1.717671
#46 : [1 sec] train: 1.710691
#47 : [1 sec] train: 1.714256
#48 : [1 sec] train: 1.712904
#49 : [1 sec] train: 1.708359
#50 : [1 sec] train: 1.704105
#51 : [1 sec] train: 1.705918
#52 : [1 sec] train: 1.703657
#53 : [1 sec] train: 1.706233
#54 : [1 sec] train: 1.703441
#55 : [1 sec] train: 1.707770
#56 : [1 sec] train: 1.705442
#57 : [1 sec] train: 1.705437
#58 : [1 sec] train: 1.707295
#59 : [1 sec] train: 1.701726
#60 : [1 sec] train: 1.514154
#61 : [1 sec] train: 1.506607
#62 : [1 sec] train: 1.509722
#63 : [1 sec] train: 1.507263
#64 : [1 sec] train: 1.507916
#65 : [1 sec] train: 1.504989
#66 : [1 sec] train: 1.504033
#67 : [1 sec] train: 1.500114
#68 : [1 sec] train: 1.498317
#69 : [1 sec] train: 1.501532
#70 : [1 sec] train: 1.502702
#71 : [1 sec] train: 1.498716
#72 : [1 sec] train: 1.497286
#73 : [1 sec] train: 1.496793
#74 : [1 sec] train: 1.498559
#75 : [1 sec] train: 1.497040
#76 : [1 sec] train: 1.500158
#77 : [1 sec] train: 1.497116
#78 : [1 sec] train: 1.495015
#79 : [1 sec] train: 1.492936
#80 : [1 sec] train: 1.286402
#81 : [1 sec] train: 1.253091
#82 : [1 sec] train: 1.246552
#83 : [1 sec] train: 1.243445
#84 : [1 sec] train: 1.243801
#85 : [1 sec] train: 1.244216
#86 : [1 sec] train: 1.239484
#87 : [1 sec] train: 1.238685
#88 : [1 sec] train: 1.236747
#89 : [1 sec] train: 1.240268
#90 : [1 sec] train: 1.237948
#91 : [1 sec] train: 1.237003
#92 : [1 sec] train: 1.237841
#93 : [1 sec] train: 1.235904
#94 : [1 sec] train: 1.236283
#95 : [1 sec] train: 1.235824
#96 : [1 sec] train: 1.237230
#97 : [1 sec] train: 1.235673
#98 : [1 sec] train: 1.234011
#99 : [1 sec] train: 1.233396
#100 : [1 sec] train: 0.812889 Building visualization...
<Figure size 864x792 with 0 Axes>

import matplotlib.inline
for row_id in range(GRID_HEIGHT):
print("Progress: {:.2%}".format(row_id / GRID_HEIGHT))
for col_id in range(GRID_WIDTH):
index = row_id * GRID_HEIGHT + col_id
clustered_samples = data[clusters == index]
if len(clustered_samples) > 0: # We take the first sample, but it can be any # sample from this cluster sample = clustered_samples[0] else: # If we don't have samples in cluster then # it means that there is a gap in space sample = np.zeros(64) plt.subplot(grid[index]) plt.imshow(sample.reshape((8, 8)), cmap='Greys') plt.axis('off')
print("Visualization has been built succesfully")
plt.show()

Output:
Progress: 0.00% Progress: 2.50% Progress: 5.00% Progress: 7.50% Progress: 10.00% Progress: 12.50% Progress: 15.00% Progress: 17.50% Progress: 20.00% Progress: 22.50% Progress: 25.00% Progress: 27.50% Progress: 30.00% Progress: 32.50% Progress: 35.00% Progress: 37.50% Progress: 40.00% Progress: 42.50% Progress: 45.00% Progress: 47.50% Progress: 50.00% Progress: 52.50% Progress: 55.00% Progress: 57.50% Progress: 60.00% Progress: 62.50% Progress: 65.00% Progress: 67.50% Progress: 70.00% Progress: 72.50% Progress: 75.00% Progress: 77.50% Progress: 80.00% Progress: 82.50% Progress: 85.00% Progress: 87.50% Progress: 90.00% Progress: 92.50% Progress: 95.00% Progress: 97.50% Visualization has been built succesfully


Leave a Reply