pyclustering  0.10.1
pyclustring is a Python, C++ data mining library.
pyclustering.nnet.som.som Class Reference

Represents self-organized feature map (SOM). More...

Public Member Functions

def size (self)
 Return size of self-organized map that is defined by total number of neurons. More...
 
def weights (self)
 Return weight of each neuron. More...
 
def awards (self)
 Return amount of captured objects by each neuron after training. More...
 
def capture_objects (self)
 Returns indexes of captured objects by each neuron. More...
 
def __init__ (self, rows, cols, conn_type=type_conn.grid_eight, parameters=None, ccore=True)
 Constructor of self-organized map. More...
 
def __del__ (self)
 Destructor of the self-organized feature map.
 
def __len__ (self)
 Returns size of the network that defines by amount of neuron in it. More...
 
def __getstate__ (self)
 
def __setstate__ (self, som_state)
 
def train (self, data, epochs, autostop=False)
 Trains self-organized feature map (SOM). More...
 
def simulate (self, input_pattern)
 Processes input pattern (no learining) and returns index of neuron-winner. More...
 
def get_winner_number (self)
 Calculates number of winner at the last step of learning process. More...
 
def show_distance_matrix (self)
 Shows gray visualization of U-matrix (distance matrix). More...
 
def get_distance_matrix (self)
 Calculates distance matrix (U-matrix). More...
 
def show_density_matrix (self, surface_divider=20.0)
 Show density matrix (P-matrix) using kernel density estimation. More...
 
def get_density_matrix (self, surface_divider=20.0)
 Calculates density matrix (P-Matrix). More...
 
def show_winner_matrix (self)
 Show a winner matrix where each element corresponds to neuron and value represents amount of won objects from input data-space at the last training iteration. More...
 
def show_network (self, awards=False, belongs=False, coupling=True, dataset=True, marker_type='o')
 Shows neurons in the dimension of data. More...
 

Detailed Description

Represents self-organized feature map (SOM).

The self-organizing feature map (SOM) method is a powerful tool for the visualization of of high-dimensional data. It converts complex, nonlinear statistical relationships between high-dimensional data into simple geometric relationships on a low-dimensional display.

ccore option can be specified in order to control using C++ implementation of pyclustering library. By default C++ implementation is on. C++ implementation improves performance of the self-organized feature map.

Example:

import random
from pyclustering.utils import read_sample
from pyclustering.nnet.som import som, type_conn, type_init, som_parameters
from pyclustering.samples.definitions import FCPS_SAMPLES
# read sample 'Lsun' from file
sample = read_sample(FCPS_SAMPLES.SAMPLE_LSUN)
# create SOM parameters
parameters = som_parameters()
# create self-organized feature map with size 7x7
rows = 10 # five rows
cols = 10 # five columns
structure = type_conn.grid_four; # each neuron has max. four neighbors.
network = som(rows, cols, structure, parameters)
# train network on 'Lsun' sample during 100 epouchs.
network.train(sample, 100)
# simulate trained network using randomly modified point from input dataset.
index_point = random.randint(0, len(sample) - 1)
point = sample[index_point] # obtain randomly point from data
point[0] += random.random() * 0.2 # change randomly X-coordinate
point[1] += random.random() * 0.2 # change randomly Y-coordinate
index_winner = network.simulate(point)
# check what are objects from input data are much close to randomly modified.
index_similar_objects = network.capture_objects[index_winner]
# neuron contains information of encoded objects
print("Point '%s' is similar to objects with indexes '%s'." % (str(point), str(index_similar_objects)))
print("Coordinates of similar objects:")
for index in index_similar_objects: print("\tPoint:", sample[index])
# result visualization:
# show distance matrix (U-matrix).
network.show_distance_matrix()
# show density matrix (P-matrix).
network.show_density_matrix()
# show winner matrix.
network.show_winner_matrix()
# show self-organized map.
network.show_network()

There is a visualization of 'Target' sample that was done by the self-organized feature map:

Definition at line 97 of file som.py.

Constructor & Destructor Documentation

◆ __init__()

def pyclustering.nnet.som.som.__init__ (   self,
  rows,
  cols,
  conn_type = type_conn.grid_eight,
  parameters = None,
  ccore = True 
)

Constructor of self-organized map.

Parameters
[in]rows(uint): Number of neurons in the column (number of rows).
[in]cols(uint): Number of neurons in the row (number of columns).
[in]conn_type(type_conn): Type of connection between oscillators in the network (grid four, grid eight, honeycomb, function neighbour).
[in]parameters(som_parameters): Other specific parameters.
[in]ccore(bool): If True simulation is performed by CCORE library (C++ implementation of pyclustering).

Definition at line 227 of file som.py.

Member Function Documentation

◆ __getstate__()

def pyclustering.nnet.som.som.__getstate__ (   self)
@brief Returns state of SOM network that can be used to store network.

Definition at line 305 of file som.py.

◆ __len__()

def pyclustering.nnet.som.som.__len__ (   self)

Returns size of the network that defines by amount of neuron in it.

Returns
(uint) Size of self-organized map (amount of neurons).

Definition at line 295 of file som.py.

◆ __setstate__()

def pyclustering.nnet.som.som.__setstate__ (   self,
  som_state 
)
@brief Set state of SOM network that can be used to load network.

Definition at line 316 of file som.py.

◆ awards()

def pyclustering.nnet.som.som.awards (   self)

Return amount of captured objects by each neuron after training.

Returns
(list) Amount of captured objects by each neuron.
See also
train()

Definition at line 194 of file som.py.

◆ capture_objects()

def pyclustering.nnet.som.som.capture_objects (   self)

Returns indexes of captured objects by each neuron.

For example, a network with size 2x2 has been trained on a sample with five objects. Suppose neuron #1 won an object with index 1, neuron #2 won objects 0, 3, 4, neuron #3 did not won anything and finally neuron #4 won an object with index 2. Thus, for this example we will have the following output [[1], [0, 3, 4], [], [2]].

Returns
(list) Indexes of captured objects by each neuron.

Definition at line 210 of file som.py.

◆ get_density_matrix()

def pyclustering.nnet.som.som.get_density_matrix (   self,
  surface_divider = 20.0 
)

Calculates density matrix (P-Matrix).

Parameters
[in]surface_divider(double): Divider in each dimension that affect radius for density measurement.
Returns
(list) Density matrix (P-Matrix).
See also
get_distance_matrix()

Definition at line 767 of file som.py.

Referenced by pyclustering.nnet.som.som.show_density_matrix().

◆ get_distance_matrix()

def pyclustering.nnet.som.som.get_distance_matrix (   self)

Calculates distance matrix (U-matrix).

The U-Matrix visualizes based on the distance in input space between a weight vector and its neighbors on map.

Returns
(list) Distance matrix (U-matrix).
See also
show_distance_matrix()
get_density_matrix()

Definition at line 717 of file som.py.

Referenced by pyclustering.nnet.som.som.show_distance_matrix().

◆ get_winner_number()

def pyclustering.nnet.som.som.get_winner_number (   self)

Calculates number of winner at the last step of learning process.

Returns
(uint) Number of winner.

Definition at line 685 of file som.py.

◆ show_density_matrix()

def pyclustering.nnet.som.som.show_density_matrix (   self,
  surface_divider = 20.0 
)

Show density matrix (P-matrix) using kernel density estimation.

Parameters
[in]surface_divider(double): Divider in each dimension that affect radius for density measurement.
See also
show_distance_matrix()

Definition at line 751 of file som.py.

◆ show_distance_matrix()

def pyclustering.nnet.som.som.show_distance_matrix (   self)

Shows gray visualization of U-matrix (distance matrix).

See also
get_distance_matrix()

Definition at line 703 of file som.py.

◆ show_network()

def pyclustering.nnet.som.som.show_network (   self,
  awards = False,
  belongs = False,
  coupling = True,
  dataset = True,
  marker_type = 'o' 
)

Shows neurons in the dimension of data.

Parameters
[in]awards(bool): If True - displays how many objects won each neuron.
[in]belongs(bool): If True - marks each won object by according index of neuron-winner (only when dataset is displayed too).
[in]coupling(bool): If True - displays connections between neurons (except case when function neighbor is used).
[in]dataset(bool): If True - displays inputs data set.
[in]marker_type(string): Defines marker that is used to denote neurons on the plot.

Definition at line 846 of file som.py.

◆ show_winner_matrix()

def pyclustering.nnet.som.som.show_winner_matrix (   self)

Show a winner matrix where each element corresponds to neuron and value represents amount of won objects from input data-space at the last training iteration.

See also
show_distance_matrix()

Definition at line 818 of file som.py.

◆ simulate()

def pyclustering.nnet.som.som.simulate (   self,
  input_pattern 
)

Processes input pattern (no learining) and returns index of neuron-winner.

Using index of neuron winner catched object can be obtained using property capture_objects.

Parameters
[in]input_pattern(list): Input pattern.
Returns
(uint) Returns index of neuron-winner.
See also
capture_objects

Definition at line 642 of file som.py.

◆ size()

def pyclustering.nnet.som.som.size (   self)

Return size of self-organized map that is defined by total number of neurons.

Returns
(uint) Size of self-organized map (number of neurons).

Definition at line 166 of file som.py.

◆ train()

def pyclustering.nnet.som.som.train (   self,
  data,
  epochs,
  autostop = False 
)

Trains self-organized feature map (SOM).

Parameters
[in]data(list): Input data - list of points where each point is represented by list of features, for example coordinates.
[in]epochs(uint): Number of epochs for training.
[in]autostop(bool): Automatic termination of learning process when adaptation is not occurred.
Returns
(uint) Number of learning iterations.

Definition at line 580 of file som.py.

◆ weights()

def pyclustering.nnet.som.som.weights (   self)

Return weight of each neuron.

Returns
(list) Weights of each neuron.

Definition at line 180 of file som.py.


The documentation for this class was generated from the following file:
pyclustering.nnet.som
Neural Network: Self-Organized Feature Map.
Definition: som.py:1
pyclustering.utils
Utils that are used by modules of pyclustering.
Definition: __init__.py:1
pyclustering.utils.read_sample
def read_sample(filename)
Returns data sample from simple text file.
Definition: __init__.py:30