In [1]:
# import karate graph
import networkx as nx

# import karate club graph
G=nx.karate_club_graph()

#distance matrix
dist = nx.all_pairs_shortest_path_length(G)

# number of nodes in G
num_nodes = len(G.nodes())

# lattice size
lattice_size_x = 2
lattice_size_y = 1

#initial neighborhood size
sigma_0 = 3

#initial learning rate
eta_0 = 0.01

#number of training epochs
num_epochs = 1000

# num_inputs to train on per epoch
N = num_nodes

In [2]:
import som_functions as som

## MAIN PROGRAM

network = som.initialise_network(G, lattice_size_x, lattice_size_y)

print("initialised network")

network = som.train_network(G, network, dist, num_epochs, eta_0, sigma_0, N)

print("trained network")


initialised network
trained network

In [3]:
import som_functions as som


#visualise based on som clusters
som.visualise_graph(G, network, dist)


[[ 0.09383843  0.67943629  0.48716627]
 [ 0.51942488  0.75796674  0.65868739]]
[array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.51942488,  0.75796674,  0.65868739]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.51942488,  0.75796674,  0.65868739]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.51942488,  0.75796674,  0.65868739]), array([ 0.51942488,  0.75796674,  0.65868739]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.51942488,  0.75796674,  0.65868739]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.51942488,  0.75796674,  0.65868739]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.51942488,  0.75796674,  0.65868739]), array([ 0.51942488,  0.75796674,  0.65868739]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.51942488,  0.75796674,  0.65868739]), array([ 0.51942488,  0.75796674,  0.65868739]), array([ 0.09383843,  0.67943629,  0.48716627]), array([ 0.51942488,  0.75796674,  0.65868739]), array([ 0.09383843,  0.67943629,  0.48716627])]
0 Mr. Hi 0
1 Mr. Hi 0
2 Mr. Hi 1
3 Mr. Hi 0
4 Mr. Hi 0
5 Mr. Hi 0
6 Mr. Hi 0
7 Mr. Hi 0
8 Mr. Hi 1
9 Officer 0
10 Mr. Hi 0
11 Mr. Hi 0
12 Mr. Hi 0
13 Mr. Hi 0
14 Officer 1
15 Officer 1
16 Mr. Hi 0
17 Mr. Hi 0
18 Officer 1
19 Mr. Hi 0
20 Officer 1
21 Mr. Hi 0
22 Officer 1
23 Officer 1
24 Officer 0
25 Officer 0
26 Officer 0
27 Officer 0
28 Officer 0
29 Officer 1
30 Officer 1
31 Officer 0
32 Officer 1
33 Officer 0

In [3]:
## visualise karate graph actual classes

#draw with networkx draw
import matplotlib.pyplot as plt

# get class labels
clubs = nx.get_node_attributes(G, 'club')

# divide nodes into clubs
mr_hi = [k for k,v in clubs.items() if v == 'Mr. Hi']
officer = [k for k,v in clubs.items() if v == 'Officer']

# graph layout
pos = nx.fruchterman_reingold_layout(G)

# draw nodes -- colouring by club
nx.draw_networkx_nodes(G, pos, nodelist = mr_hi, node_color = 'r')
nx.draw_networkx_nodes(G, pos, nodelist = officer, node_color = 'b')

#draw edges
nx.draw_networkx_edges(G, pos)

# draw labels
nx.draw_networkx_labels(G, pos)

#show plot
plt.show()



In [4]:
import numpy as np
import math
import som_functions as som
import matplotlib.pyplot as plt

# colour G based on what lattice node it is closest to

#assign nodes
node_assignments = som.assign_node(G, network)

# random colour for each output neuron
cluster_colours = np.random.rand(max(node_assignment), 3);
    
## visualise karate graph

#draw with networkx draw


# graph layout
pos = nx.fruchterman_reingold_layout(G)

# draw nodes -- colouring by club
nx.draw_networkx_nodes(G, pos, node_color = node_colours)

#draw edges
nx.draw_networkx_edges(G, pos)

# draw labels
nx.draw_networkx_labels(G, pos)

#show plot
plt.show()

# print node labels and classification 
for n in G.nodes():
    print(n, nx.get_node_attributes(G, 'club')[n], node_assignments[n])


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-4-01ffb836043a> in <module>()
     23 
     24             # calculate distance to that reference vector
---> 25             d = distance_function(n, network[i][j])
     26 
     27             if d < min_distance:

NameError: name 'distance_function' is not defined