Skip to main content

A simple planar self organizing map

Project description

sklearn-som

A simple, planar self-organizing map with methods similar to clustering methods in Scikit Learn.

sklearn-som is a minimalist, simple implementation of a Kohonen self organizing map with a planar (rectangular) topology. It is used for clustering data and performing dimensionality reduction. For a brief, all-around introduction to self organizing maps, check out this helpful article from Rubik's Code.

Why another SOM package?

There are already a handful of useful SOM packages available in your machine learning framework of choice. So why make another one? Well, sklearn-som, as the name suggests, is written to interface just like a clustering method you would find in Scikit Learn. It has the advantage of only having one dependency (numpy) and if you are already familiar with Scikit Learn's machine learning API, you will find it easy to get right up to speed with sklearn-som.

How to use

Using sklearn-som couldn't be easier. First, import the SOM class from the som.som module:

from sklearn_som.som import SOM

Now you will have to create an instance of SOM to cluster data, but first let's get some data. For this part we will use sklearn's Iris Dataset, but you do not need sklearn to use SOM. If you have data from another source, you will not need it. But we are going to use it, so let's grab it. We will also use only the first two features so our results are easier to visualize:

from sklearn import datasets

iris = datasets.load_iris()
iris_data = iris.data[:, :2]
iris_label = iris.target

Now, just like with any classifier right from sklearn, we will have to build an SOM instance and call .fit() on our data to fit the SOM. We already know that there are 3 classes in the Iris Dataset, so we will use a 3 by 1 structure for our self organizing map, but in practice you may have to try different structures to find what works best for your data. Let's build and fit the som:

iris_som = SOM(m=3, n=1, dim=2)
iris_som.fit(iris_data)

Note that when building the instance of SOM, we specify m and n to get an m by n matrix of neurons in the self organizing map.

Now also like in sklearn, let's assign each datapoint to a predicted cluster using the .predict() method:

predictions = iris_som.predict(iris_data)

And let's take a look at how we did:

Iris Data Results

Not bad! For the full example code, including the code to reproduce that plot, see example/example.py at https://github.com/rileypsmith/sklearn-som.

Documentation

For full documentation, visit the project page on ReadTheDocs.

Contributing

If you would like to contribute to sklearn-som, feel free to drop me a line or just submit a pull request and I'll take a look. Ideas for future expansion include adding the ability to make higher dimensional self-organizing maps and to use a hexagonal grid rather than a rectangular one. I may get to those expansions at some point.

Also, if you find a bug, please open an issue on GitHub!!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sklearn-som-1.0.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

sklearn_som-1.0.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file sklearn-som-1.0.1.tar.gz.

File metadata

  • Download URL: sklearn-som-1.0.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for sklearn-som-1.0.1.tar.gz
Algorithm Hash digest
SHA256 54088d71ca063cf387563bd18119a260ca3df9f476ce99477c9b58245677adaf
MD5 da7b43dda25f0ced8818fd02b77b4984
BLAKE2b-256 02d6d6abdf3f678fe97f71332e00024c3105045a68b22cac5bf3cb6dfe52f54d

See more details on using hashes here.

File details

Details for the file sklearn_som-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: sklearn_som-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.5

File hashes

Hashes for sklearn_som-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9956476405f6b2f133699e1b4106ecf47840a81baddd843675cbe17a00d934ed
MD5 55a4d31f39a3fe5e8b9bd929735b1157
BLAKE2b-256 03fdef7e4da6b1a2b40b8d9fe2271eacdd962f4d0053fe447e4577997d4fa1d5

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page