som-9.0.4: Self-Organising Maps.

Copyright (c) Amy de Buitléir 2012-2016 BSD-style amy@nualeargais.ie experimental portable Safe Haskell2010

Data.Datamining.Clustering.SOM

Description

A Kohonen Self-organising Map (SOM). A SOM maps input patterns onto a regular grid (usually two-dimensional) where each node in the grid is a model of the input data, and does so using a method which ensures that any topological relationships within the input data are also represented in the grid. This implementation supports the use of non-numeric patterns.

In layman's terms, a SOM can be useful when you you want to discover the underlying structure of some data. A tutorial is available at https://github.com/mhwombat/som/wiki.

NOTES:

• Version 5.0 fixed a bug in the decayingGaussian function. If you use defaultSOM (which uses this function), your SOM should now learn more quickly.
• The gaussian function has been removed because it is not as useful for SOMs as I originally thought. It was originally designed to be used as a factor in a learning function. However, in most cases the user will want to introduce a time decay into the exponent, rather than simply multiply by a factor.

References:

• Kohonen, T. (1982). Self-organized formation of topologically correct feature maps. Biological Cybernetics, 43 (1), 59–69.

Synopsis

# Construction

data SOM t d gm x k p Source #

A Self-Organising Map (SOM).

Although SOM implements GridMap, most users will only need the interface provided by Data.Datamining.Clustering.Classifier. If you chose to use the GridMap functions, please note:

1. The functions adjust, and adjustWithKey do not increment the counter. You can do so manually with incrementCounter.
2. The functions map and mapWithKey are not implemented (they just return an error). It would be problematic to implement them because the input SOM and the output SOM would have to have the same Metric type.

Constructors

 SOM FieldsgridMap :: gm pMaps patterns to tiles in a regular grid. In the context of a SOM, the tiles are called "nodes"learningRate :: t -> d -> xA function which determines the how quickly the SOM learns. For example, if the function is f, then f t d returns the learning rate for a node. The parameter t indicates how many patterns (or pattern batches) have previously been presented to the classifier. Typically this is used to make the learning rate decay over time. The parameter d is the grid distance from the node being updated to the BMU (Best Matching Unit). The output is the learning rate for that node (the amount by which the node's model should be updated to match the target). The learning rate should be between zero and one.difference :: p -> p -> xA function which compares two patterns and returns a non-negative number representing how different the patterns are. A result of 0 indicates that the patterns are identical.makeSimilar :: p -> x -> p -> pA function which updates models. If this function is f, then f target amount pattern returns a modified copy of pattern that is more similar to target than pattern is. The magnitude of the adjustment is controlled by the amount parameter, which should be a number between 0 and 1. Larger values for amount permit greater adjustments. If amount=1, the result should be identical to the target. If amount=0, the result should be the unmodified pattern.counter :: tA counter used as a "time" parameter. If you create the SOM with a counter value 0, and don't directly modify it, then the counter will represent the number of patterns that this SOM has classified.

Instances

 (GridMap gm p, (~) * k (Index (BaseGrid gm p)), Grid (gm p), GridMap gm x, (~) * k (Index (gm p)), (~) * k (Index (BaseGrid gm x)), Num t, Ord x, Num x, Num d) => Classifier (SOM t d gm) x k p Source # MethodstoList :: SOM t d gm x k p -> [(k, p)] Source #numModels :: SOM t d gm x k p -> Int Source #models :: SOM t d gm x k p -> [p] Source #differences :: SOM t d gm x k p -> p -> [(k, x)] Source #classify :: SOM t d gm x k p -> p -> k Source #train :: SOM t d gm x k p -> p -> SOM t d gm x k p Source #trainBatch :: SOM t d gm x k p -> [p] -> SOM t d gm x k p Source #classifyAndTrain :: SOM t d gm x k p -> p -> (k, SOM t d gm x k p) Source #diffAndTrain :: SOM t d gm x k p -> p -> ([(k, x)], SOM t d gm x k p) Source #reportAndTrain :: SOM t d gm x k p -> p -> (k, [(k, x)], SOM t d gm x k p) Source # Foldable gm => Foldable (SOM t d gm x k) Source # Methodsfold :: Monoid m => SOM t d gm x k m -> m #foldMap :: Monoid m => (a -> m) -> SOM t d gm x k a -> m #foldr :: (a -> b -> b) -> b -> SOM t d gm x k a -> b #foldr' :: (a -> b -> b) -> b -> SOM t d gm x k a -> b #foldl :: (b -> a -> b) -> b -> SOM t d gm x k a -> b #foldl' :: (b -> a -> b) -> b -> SOM t d gm x k a -> b #foldr1 :: (a -> a -> a) -> SOM t d gm x k a -> a #foldl1 :: (a -> a -> a) -> SOM t d gm x k a -> a #toList :: SOM t d gm x k a -> [a] #null :: SOM t d gm x k a -> Bool #length :: SOM t d gm x k a -> Int #elem :: Eq a => a -> SOM t d gm x k a -> Bool #maximum :: Ord a => SOM t d gm x k a -> a #minimum :: Ord a => SOM t d gm x k a -> a #sum :: Num a => SOM t d gm x k a -> a #product :: Num a => SOM t d gm x k a -> a # (Foldable gm, GridMap gm p, Grid (BaseGrid gm p)) => GridMap (SOM t d gm x k) p Source # Associated Typestype BaseGrid (SOM t d gm x k :: * -> *) p :: * # Methods(!) :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), Ord k) => SOM t d gm x k p -> k -> p #toMap :: (* ~ k) (Index (BaseGrid (SOM t d gm x k) p)) => SOM t d gm x k p -> Map k p #toGrid :: SOM t d gm x k p -> BaseGrid (SOM t d gm x k) p #toList :: (* ~ k) (Index (BaseGrid (SOM t d gm x k) p)) => SOM t d gm x k p -> [(k, p)] #lookup :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), Ord k) => k -> SOM t d gm x k p -> Maybe p #insert :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), Ord k) => k -> p -> SOM t d gm x k p -> SOM t d gm x k p #insertWith :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), Ord k) => (p -> p -> p) -> k -> p -> SOM t d gm x k p -> SOM t d gm x k p #insertWithKey :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), Ord k) => (k -> p -> p -> p) -> k -> p -> SOM t d gm x k p -> SOM t d gm x k p #insertLookupWithKey :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), Ord k) => (k -> p -> p -> p) -> k -> p -> SOM t d gm x k p -> (Maybe p, SOM t d gm x k p) #delete :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), Ord k) => k -> SOM t d gm x k p -> SOM t d gm x k p #adjust :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), Ord k) => (p -> p) -> k -> SOM t d gm x k p -> SOM t d gm x k p #adjustWithKey :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), Ord k) => (k -> p -> p) -> k -> SOM t d gm x k p -> SOM t d gm x k p #alter :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), Ord k) => (Maybe p -> Maybe p) -> k -> SOM t d gm x k p -> SOM t d gm x k p #findWithDefault :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), Ord k) => p -> k -> SOM t d gm x k p -> p #keys :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), Ord k) => SOM t d gm x k p -> [k] #elems :: SOM t d gm x k p -> [p] #map :: (GridMap (SOM t d gm x k) v2, (* ~ Index (BaseGrid (SOM t d gm x k) p)) (Index (BaseGrid (SOM t d gm x k) v2))) => (p -> v2) -> SOM t d gm x k p -> SOM t d gm x k v2 #mapWithKey :: ((* ~ k) (Index (BaseGrid (SOM t d gm x k) p)), (* ~ k) (Index (BaseGrid (SOM t d gm x k) v2)), GridMap (SOM t d gm x k) v2) => (k -> p -> v2) -> SOM t d gm x k p -> SOM t d gm x k v2 #filter :: (p -> Bool) -> SOM t d gm x k p -> SOM t d gm x k p #filterWithKey :: (* ~ k) (Index (BaseGrid (SOM t d gm x k) p)) => (k -> p -> Bool) -> SOM t d gm x k p -> SOM t d gm x k p # Generic (SOM t d gm x k p) Source # Associated Typestype Rep (SOM t d gm x k p) :: * -> * # Methodsfrom :: SOM t d gm x k p -> Rep (SOM t d gm x k p) x #to :: Rep (SOM t d gm x k p) x -> SOM t d gm x k p # (NFData (gm p), NFData t) => NFData (SOM t d gm x k p) Source # Methodsrnf :: SOM t d gm x k p -> () # Grid (gm p) => Grid (SOM t d gm x k p) Source # Associated Typestype Index (SOM t d gm x k p) :: * #type Direction (SOM t d gm x k p) :: * # Methodsindices :: SOM t d gm x k p -> [Index (SOM t d gm x k p)] #distance :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> Index (SOM t d gm x k p) -> Int #minDistance :: SOM t d gm x k p -> [Index (SOM t d gm x k p)] -> Index (SOM t d gm x k p) -> Int #neighbours :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> [Index (SOM t d gm x k p)] #neighboursOfSet :: SOM t d gm x k p -> [Index (SOM t d gm x k p)] -> [Index (SOM t d gm x k p)] #neighbour :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> Direction (SOM t d gm x k p) -> Maybe (Index (SOM t d gm x k p)) #numNeighbours :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> Int #contains :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> Bool #tileCount :: SOM t d gm x k p -> Int #null :: SOM t d gm x k p -> Bool #nonNull :: SOM t d gm x k p -> Bool #edges :: SOM t d gm x k p -> [(Index (SOM t d gm x k p), Index (SOM t d gm x k p))] #viewpoint :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> [(Index (SOM t d gm x k p), Int)] #isAdjacent :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> Index (SOM t d gm x k p) -> Bool #adjacentTilesToward :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> Index (SOM t d gm x k p) -> [Index (SOM t d gm x k p)] #minimalPaths :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> Index (SOM t d gm x k p) -> [[Index (SOM t d gm x k p)]] #directionTo :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> Index (SOM t d gm x k p) -> [Direction (SOM t d gm x k p)] #defaultMinDistance :: SOM t d gm x k p -> [Index (SOM t d gm x k p)] -> Index (SOM t d gm x k p) -> Int #defaultNeighbours :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> [Index (SOM t d gm x k p)] #defaultNeighboursOfSet :: SOM t d gm x k p -> [Index (SOM t d gm x k p)] -> [Index (SOM t d gm x k p)] #defaultNeighbour :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> Direction (SOM t d gm x k p) -> Maybe (Index (SOM t d gm x k p)) #defaultTileCount :: SOM t d gm x k p -> Int #defaultEdges :: SOM t d gm x k p -> [(Index (SOM t d gm x k p), Index (SOM t d gm x k p))] #defaultIsAdjacent :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> Index (SOM t d gm x k p) -> Bool #defaultAdjacentTilesToward :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> Index (SOM t d gm x k p) -> [Index (SOM t d gm x k p)] #defaultMinimalPaths :: SOM t d gm x k p -> Index (SOM t d gm x k p) -> Index (SOM t d gm x k p) -> [[Index (SOM t d gm x k p)]] # type BaseGrid (SOM t d gm x k) p Source # type BaseGrid (SOM t d gm x k) p = BaseGrid gm p type Rep (SOM t d gm x k p) Source # type Rep (SOM t d gm x k p) = D1 (MetaData "SOM" "Data.Datamining.Clustering.SOMInternal" "som-9.0.4-FayMKLEE0GK5Ud8i4mMs21" False) (C1 (MetaCons "SOM" PrefixI True) ((:*:) ((:*:) (S1 (MetaSel (Just Symbol "gridMap") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (gm p))) (S1 (MetaSel (Just Symbol "learningRate") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (t -> d -> x)))) ((:*:) (S1 (MetaSel (Just Symbol "difference") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (p -> p -> x))) ((:*:) (S1 (MetaSel (Just Symbol "makeSimilar") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (p -> x -> p -> p))) (S1 (MetaSel (Just Symbol "counter") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 t)))))) type Direction (SOM t d gm x k p) Source # type Direction (SOM t d gm x k p) = Direction (gm p) type Index (SOM t d gm x k p) Source # type Index (SOM t d gm x k p) = Index (gm p)

# Deconstruction

toGridMap :: GridMap gm p => SOM t d gm x k p -> gm p Source #

Extracts the grid and current models from the SOM. A synonym for gridMap.

# Learning functions

decayingGaussian :: Floating x => x -> x -> x -> x -> x -> x -> x -> x Source #

A typical learning function for classifiers. decayingGaussian r0 rf w0 wf tf returns a bell curve-shaped function. At time zero, the maximum learning rate (applied to the BMU) is r0, and the neighbourhood width is w0. Over time the bell curve shrinks and the learning rate tapers off, until at time tf, the maximum learning rate (applied to the BMU) is rf, and the neighbourhood width is wf. Normally the parameters should be chosen such that:

• 0 < rf << r0 < 1
• 0 < wf << w0
• 0 < tf

where << means "is much smaller than" (not the Haskell << operator!)

stepFunction :: (Num d, Fractional x, Eq d) => x -> t -> d -> x Source #

A learning function that only updates the BMU and has a constant learning rate.

constantFunction :: x -> t -> d -> x Source #

A learning function that updates all nodes with the same, constant learning rate. This can be useful for testing.

Trains the specified node and the neighbourood around it to better match a target. Most users should use train, which automatically determines the BMU and trains it and its neighbourhood.