Self Organising Map

class visigoth.charts.som.SOM(data, width, label=0, colour=1, dimensions=2, gridwidth=10, gridheight=10, iters=100, palette=None, marker_manager=None, trainedSom=None, seed=None, dimension=None, dimensionPalette=None)

Create a Self Organising Map (SOM) chart with cells arranged in a hexagonal layout

Parameters:
  • data (list) – A relational data set (for example, list of dicts/lists/tuples describing each row)
  • width (int) – the width of the plot in pixels
Keyword Arguments:
 
  • colour (str or int) – Identify the column to define the point colour (use palette default colour if not specified)
  • label (str or int) – Identify the column to define the label
  • dimensions (str or int) – Identify the column to define the vector of floats used to define the point dimensions
  • gridwidth (int) – the number of columns in the SOM plot
  • gridheight (int) – the number of rows in the SOM plot
  • iters (int) – the number of training iterations to use when training the SOM
  • palette (visigoth.utils.colour.DiscretePalette) – mapping from category to colour
  • trainedSom (SelfOrganisingMap) – a pre-trained SOM model. If specified, this SOM’s grid width/height will be used
  • marker_manager (object) – a MarkerManager instance to control marker appearance
  • seed (int) – random seed
  • dimension (lambda) – lambda to compute the dimension (from the values vector) value
  • dimensionPalette (ContinuousPalette) – a palette to map dimension values to colours

Example

(Link to open example in new tab):

Source Code (Link to Github):

# -*- coding: utf-8 -*-

import csv

from visigoth import Diagram
from visigoth.charts import SOM
from visigoth.common import Text, Legend
from visigoth.utils.colour import ContinuousPalette


cpalette0 = ContinuousPalette(withIntervals=False)
cpalette0.addColour("#000000",0.0).addColour("red",1.0)

cpalette1 = ContinuousPalette(withIntervals=False)
cpalette1.addColour("#000000",0.0).addColour("red",1.0)

cpalette2 = ContinuousPalette(withIntervals=False)
cpalette2.addColour("#000000",0.0).addColour("blue",1.0)

cpalette3 = ContinuousPalette(withIntervals=False)
cpalette3.addColour("#000000",0.0).addColour("white",1.0)

min_temp_data = []
max_temp_data = []
precipitation_data = []
snowfall_data = []
reader = csv.reader(open("climate_by_city.csv"))
headers_read = False
for line in reader:
    if not headers_read:
        headers_read = True
    else:
        label = line[0]
        cat = ""
        values = list(map(lambda s:float(s),line[1:]))
        min_temp_data.append((label,cat,values[0:12]))
        max_temp_data.append((label,cat,values[12:24]))
        precipitation_data.append((label,cat,values[24:36]))
        snowfall_data.append((label,cat,values[36:48]))

def mean(vec):
    return sum(vec)/len(vec)

d = Diagram(fill="white")

som0 = SOM(min_temp_data,512,dimension=lambda l: mean(l),dimensionPalette=cpalette0)
som1 = SOM(max_temp_data,512,dimension=lambda l: mean(l),dimensionPalette=cpalette1)
som2 = SOM(precipitation_data,512,dimension=lambda l: mean(l),dimensionPalette=cpalette2)
som2.getPalette().setDefaultColour("white")

d.add(Text("Cities clustered by monthly minimum temperature(s)"))
d.add(som0)
d.add(Text("Mean minimum daily temperature (Celsius)",font_height=12))
d.add(Legend(cpalette0,width=500))

d.add(Text("Cities clustered by monthly maximum temperature(s)"))
d.add(som1)
d.add(Text("Mean maximum daily temperature (Celsius)",font_height=12))
d.add(Legend(cpalette1,width=500))

d.add(Text("Cities clustered by precipitation"))
d.add(som2)
d.add(Text("Mean daily precipitation (mm)",font_height=12))
d.add(Legend(cpalette2,width=500))

html = d.draw(format="html")

f = open("example.html", "w")
f.write(html)
f.close()