python-som 0.1.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- python_som-0.1.1/.gitignore +129 -0
- python_som-0.1.1/LICENSE +21 -0
- python_som-0.1.1/PKG-INFO +142 -0
- python_som-0.1.1/README.md +121 -0
- python_som-0.1.1/SOM_atualizado.py +758 -0
- python_som-0.1.1/pyproject.toml +29 -0
- python_som-0.1.1/python_som/__init__.py +825 -0
- python_som-0.1.1/requirements.txt +3 -0
- python_som-0.1.1/setup.cfg +2 -0
- python_som-0.1.1/setup.py +38 -0
- python_som-0.1.1/test.py +74 -0
- python_som-0.1.1/test_iris.ipynb +408 -0
- python_som-0.1.1/test_output_iris.png +0 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
pip-wheel-metadata/
|
|
24
|
+
share/python-wheels/
|
|
25
|
+
*.egg-info/
|
|
26
|
+
.installed.cfg
|
|
27
|
+
*.egg
|
|
28
|
+
MANIFEST
|
|
29
|
+
|
|
30
|
+
# PyInstaller
|
|
31
|
+
# Usually these files are written by a python script from a template
|
|
32
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
33
|
+
*.manifest
|
|
34
|
+
*.spec
|
|
35
|
+
|
|
36
|
+
# Installer logs
|
|
37
|
+
pip-log.txt
|
|
38
|
+
pip-delete-this-directory.txt
|
|
39
|
+
|
|
40
|
+
# Unit test / coverage reports
|
|
41
|
+
htmlcov/
|
|
42
|
+
.tox/
|
|
43
|
+
.nox/
|
|
44
|
+
.coverage
|
|
45
|
+
.coverage.*
|
|
46
|
+
.cache
|
|
47
|
+
nosetests.xml
|
|
48
|
+
coverage.xml
|
|
49
|
+
*.cover
|
|
50
|
+
*.py,cover
|
|
51
|
+
.hypothesis/
|
|
52
|
+
.pytest_cache/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
target/
|
|
76
|
+
|
|
77
|
+
# Jupyter Notebook
|
|
78
|
+
.ipynb_checkpoints
|
|
79
|
+
|
|
80
|
+
# IPython
|
|
81
|
+
profile_default/
|
|
82
|
+
ipython_config.py
|
|
83
|
+
|
|
84
|
+
# pyenv
|
|
85
|
+
.python-version
|
|
86
|
+
|
|
87
|
+
# pipenv
|
|
88
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
89
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
90
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
91
|
+
# install all needed dependencies.
|
|
92
|
+
#Pipfile.lock
|
|
93
|
+
|
|
94
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
95
|
+
__pypackages__/
|
|
96
|
+
|
|
97
|
+
# Celery stuff
|
|
98
|
+
celerybeat-schedule
|
|
99
|
+
celerybeat.pid
|
|
100
|
+
|
|
101
|
+
# SageMath parsed files
|
|
102
|
+
*.sage.py
|
|
103
|
+
|
|
104
|
+
# Environments
|
|
105
|
+
.env
|
|
106
|
+
.venv
|
|
107
|
+
env/
|
|
108
|
+
venv/
|
|
109
|
+
ENV/
|
|
110
|
+
env.bak/
|
|
111
|
+
venv.bak/
|
|
112
|
+
|
|
113
|
+
# Spyder project settings
|
|
114
|
+
.spyderproject
|
|
115
|
+
.spyproject
|
|
116
|
+
|
|
117
|
+
# Rope project settings
|
|
118
|
+
.ropeproject
|
|
119
|
+
|
|
120
|
+
# mkdocs documentation
|
|
121
|
+
/site
|
|
122
|
+
|
|
123
|
+
# mypy
|
|
124
|
+
.mypy_cache/
|
|
125
|
+
.dmypy.json
|
|
126
|
+
dmypy.json
|
|
127
|
+
|
|
128
|
+
# Pyre type checker
|
|
129
|
+
.pyre/
|
python_som-0.1.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 André Moreira Souza
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: python-som
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Python implementation of the Self-Organizing Map
|
|
5
|
+
Project-URL: Homepage, https://github.com/andremsouza/python-som
|
|
6
|
+
Project-URL: Issues, https://github.com/andremsouza/python-som/issues
|
|
7
|
+
Author-email: André Moreira Souza <msouza.andre@hotmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
License-File: LICENSE
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
14
|
+
Classifier: Operating System :: OS Independent
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Requires-Python: >=3.9
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# python-som
|
|
23
|
+
|
|
24
|
+
Implementation of the 2D self-organizing map, with support for NumPy arrays and Pandas DataFrames.
|
|
25
|
+
Most features were implemented using NumPy, with Scikit-learn for standardization and PCA operations.
|
|
26
|
+
|
|
27
|
+
## Features
|
|
28
|
+
|
|
29
|
+
* Stepwise and batch training
|
|
30
|
+
* Random weight initialization
|
|
31
|
+
* Random sampling weight initialization
|
|
32
|
+
* Linear weight initialization (with PCA)
|
|
33
|
+
* Automatic selection of map size ratio (with PCA)
|
|
34
|
+
* Support for cyclic arrays, for toroidal or spherical maps
|
|
35
|
+
* Gaussian and Bubble neighborhood functions
|
|
36
|
+
* Support for custom decay functions
|
|
37
|
+
* Support for visualization (U-matrix, activation matrix)
|
|
38
|
+
* Support for supervised learning (label map)
|
|
39
|
+
* Support for NumPy arrays, Pandas DataFrames and regular lists of values
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
In the following code excerpt (also available in [test.py](./test.py)) is an example of instantiation and training of a SOM with the Iris dataset:
|
|
43
|
+
```python
|
|
44
|
+
# Import python_som
|
|
45
|
+
import python_som
|
|
46
|
+
# Import NumPy and Pandas for storing data
|
|
47
|
+
import numpy as np
|
|
48
|
+
import pandas as pd
|
|
49
|
+
# Import libraries for plotting results
|
|
50
|
+
import matplotlib.pyplot as plt
|
|
51
|
+
import seaborn as sns
|
|
52
|
+
|
|
53
|
+
# Load Iris dataset and columns of features and labels
|
|
54
|
+
iris = sns.load_dataset('iris')
|
|
55
|
+
target = iris.iloc[:, -1].to_numpy()
|
|
56
|
+
iris = iris.iloc[:, :-1].to_numpy()
|
|
57
|
+
# Transform labels into numeric codes for plotting
|
|
58
|
+
tg = np.zeros(len(target), dtype=int)
|
|
59
|
+
tg[target == 'setosa'] = 0
|
|
60
|
+
tg[target == 'versicolor'] = 1
|
|
61
|
+
tg[target == 'virginica'] = 2
|
|
62
|
+
|
|
63
|
+
# Instantiate SOM from python_som
|
|
64
|
+
# Selecting shape automatically (providing dataset for constructor)
|
|
65
|
+
# Using default decay and distance functions
|
|
66
|
+
# Using gaussian neighborhood function
|
|
67
|
+
# Using cyclic arrays in the vertical and horizontal directions
|
|
68
|
+
som = python_som.SOM(x=20, y=None, input_len=iris.shape[1], learning_rate=0.5, neighborhood_radius=1.0,
|
|
69
|
+
neighborhood_function='gaussian', cyclic_x=True, cyclic_y=True, data=iris)
|
|
70
|
+
|
|
71
|
+
# Initialize weights of the SOM with linear initialization
|
|
72
|
+
som.weight_initialization(mode='linear', data=iris)
|
|
73
|
+
|
|
74
|
+
# Training SOM with default number of iterations
|
|
75
|
+
# Using batch learning process
|
|
76
|
+
som.train(data=iris, n_iteration=len(iris), mode='batch', verbose=True)
|
|
77
|
+
|
|
78
|
+
# Calculating distance matrix for plotting
|
|
79
|
+
umatrix = som.distance_matrix().T
|
|
80
|
+
|
|
81
|
+
# Plotting U-matrix with seaborn/matplotlib
|
|
82
|
+
plt.figure(figsize=som.get_shape())
|
|
83
|
+
plt.pcolor(umatrix, cmap='bone_r')
|
|
84
|
+
|
|
85
|
+
markers = ['o', 's', 'D']
|
|
86
|
+
colors = ['C0', 'C1', 'C2']
|
|
87
|
+
for cnt, xx in enumerate(iris):
|
|
88
|
+
w = som.winner(xx) # getting the winner
|
|
89
|
+
plt.plot(w[0] + .5, w[1] + .5, markers[tg[cnt]], markerfacecolor='None',
|
|
90
|
+
markeredgecolor=colors[tg[cnt]], markersize=12, markeredgewidth=2)
|
|
91
|
+
plt.axis([0, som.get_shape()[0], 0, som.get_shape()[1]])
|
|
92
|
+
plt.show()
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Test output
|
|
97
|
+
The following image is generated from the previous test code, with the U-matrix of the trained SOM, and the distribution of the instances from the Iris dataset.
|
|
98
|
+
In this graph, the instances are mapped to the self-organizing map, with color codes for each different label:
|
|
99
|
+
* Setosa: blue circle
|
|
100
|
+
* Versicolor: orange square
|
|
101
|
+
* Virginica: green diamond
|
|
102
|
+
|
|
103
|
+

|
|
104
|
+
|
|
105
|
+
## Public methods and functions
|
|
106
|
+
The following are lists of public methods and functions currently available in the SOM class. The full documentation of each method can be found in the [source code](./python_som/__init__.py):
|
|
107
|
+
|
|
108
|
+
### Utility functions
|
|
109
|
+
* _asymptotic_decay
|
|
110
|
+
* _linear_decay
|
|
111
|
+
* _exponential_decay
|
|
112
|
+
* _inverse_decay
|
|
113
|
+
* _euclidean_distance
|
|
114
|
+
|
|
115
|
+
### SOM public methods
|
|
116
|
+
* SOM
|
|
117
|
+
* SOM.get_shape
|
|
118
|
+
* SOM.get_weights
|
|
119
|
+
* SOM.set_learning_rate
|
|
120
|
+
* SOM.set_neighborhood_radius
|
|
121
|
+
* SOM.activate
|
|
122
|
+
* SOM.winner
|
|
123
|
+
* SOM.quantization
|
|
124
|
+
* SOM.quantization_error
|
|
125
|
+
* SOM.distance_matrix
|
|
126
|
+
* SOM.activation_matrix
|
|
127
|
+
* SOM.winner_map
|
|
128
|
+
* SOM.label_map
|
|
129
|
+
* SOM.train
|
|
130
|
+
* SOM.weight_initialization
|
|
131
|
+
|
|
132
|
+
## References
|
|
133
|
+
This implemetation was based on the following paper, by Professor Teuvo Kohonen:
|
|
134
|
+
|
|
135
|
+
Teuvo Kohonen,
|
|
136
|
+
Essentials of the self-organizing map,
|
|
137
|
+
Neural Networks,
|
|
138
|
+
Volume 37,
|
|
139
|
+
2013,
|
|
140
|
+
Pages 52-65,
|
|
141
|
+
ISSN 0893-6080,
|
|
142
|
+
https://doi.org/10.1016/j.neunet.2012.09.018.
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# python-som
|
|
2
|
+
|
|
3
|
+
Implementation of the 2D self-organizing map, with support for NumPy arrays and Pandas DataFrames.
|
|
4
|
+
Most features were implemented using NumPy, with Scikit-learn for standardization and PCA operations.
|
|
5
|
+
|
|
6
|
+
## Features
|
|
7
|
+
|
|
8
|
+
* Stepwise and batch training
|
|
9
|
+
* Random weight initialization
|
|
10
|
+
* Random sampling weight initialization
|
|
11
|
+
* Linear weight initialization (with PCA)
|
|
12
|
+
* Automatic selection of map size ratio (with PCA)
|
|
13
|
+
* Support for cyclic arrays, for toroidal or spherical maps
|
|
14
|
+
* Gaussian and Bubble neighborhood functions
|
|
15
|
+
* Support for custom decay functions
|
|
16
|
+
* Support for visualization (U-matrix, activation matrix)
|
|
17
|
+
* Support for supervised learning (label map)
|
|
18
|
+
* Support for NumPy arrays, Pandas DataFrames and regular lists of values
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
In the following code excerpt (also available in [test.py](./test.py)) is an example of instantiation and training of a SOM with the Iris dataset:
|
|
22
|
+
```python
|
|
23
|
+
# Import python_som
|
|
24
|
+
import python_som
|
|
25
|
+
# Import NumPy and Pandas for storing data
|
|
26
|
+
import numpy as np
|
|
27
|
+
import pandas as pd
|
|
28
|
+
# Import libraries for plotting results
|
|
29
|
+
import matplotlib.pyplot as plt
|
|
30
|
+
import seaborn as sns
|
|
31
|
+
|
|
32
|
+
# Load Iris dataset and columns of features and labels
|
|
33
|
+
iris = sns.load_dataset('iris')
|
|
34
|
+
target = iris.iloc[:, -1].to_numpy()
|
|
35
|
+
iris = iris.iloc[:, :-1].to_numpy()
|
|
36
|
+
# Transform labels into numeric codes for plotting
|
|
37
|
+
tg = np.zeros(len(target), dtype=int)
|
|
38
|
+
tg[target == 'setosa'] = 0
|
|
39
|
+
tg[target == 'versicolor'] = 1
|
|
40
|
+
tg[target == 'virginica'] = 2
|
|
41
|
+
|
|
42
|
+
# Instantiate SOM from python_som
|
|
43
|
+
# Selecting shape automatically (providing dataset for constructor)
|
|
44
|
+
# Using default decay and distance functions
|
|
45
|
+
# Using gaussian neighborhood function
|
|
46
|
+
# Using cyclic arrays in the vertical and horizontal directions
|
|
47
|
+
som = python_som.SOM(x=20, y=None, input_len=iris.shape[1], learning_rate=0.5, neighborhood_radius=1.0,
|
|
48
|
+
neighborhood_function='gaussian', cyclic_x=True, cyclic_y=True, data=iris)
|
|
49
|
+
|
|
50
|
+
# Initialize weights of the SOM with linear initialization
|
|
51
|
+
som.weight_initialization(mode='linear', data=iris)
|
|
52
|
+
|
|
53
|
+
# Training SOM with default number of iterations
|
|
54
|
+
# Using batch learning process
|
|
55
|
+
som.train(data=iris, n_iteration=len(iris), mode='batch', verbose=True)
|
|
56
|
+
|
|
57
|
+
# Calculating distance matrix for plotting
|
|
58
|
+
umatrix = som.distance_matrix().T
|
|
59
|
+
|
|
60
|
+
# Plotting U-matrix with seaborn/matplotlib
|
|
61
|
+
plt.figure(figsize=som.get_shape())
|
|
62
|
+
plt.pcolor(umatrix, cmap='bone_r')
|
|
63
|
+
|
|
64
|
+
markers = ['o', 's', 'D']
|
|
65
|
+
colors = ['C0', 'C1', 'C2']
|
|
66
|
+
for cnt, xx in enumerate(iris):
|
|
67
|
+
w = som.winner(xx) # getting the winner
|
|
68
|
+
plt.plot(w[0] + .5, w[1] + .5, markers[tg[cnt]], markerfacecolor='None',
|
|
69
|
+
markeredgecolor=colors[tg[cnt]], markersize=12, markeredgewidth=2)
|
|
70
|
+
plt.axis([0, som.get_shape()[0], 0, som.get_shape()[1]])
|
|
71
|
+
plt.show()
|
|
72
|
+
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Test output
|
|
76
|
+
The following image is generated from the previous test code, with the U-matrix of the trained SOM, and the distribution of the instances from the Iris dataset.
|
|
77
|
+
In this graph, the instances are mapped to the self-organizing map, with color codes for each different label:
|
|
78
|
+
* Setosa: blue circle
|
|
79
|
+
* Versicolor: orange square
|
|
80
|
+
* Virginica: green diamond
|
|
81
|
+
|
|
82
|
+

|
|
83
|
+
|
|
84
|
+
## Public methods and functions
|
|
85
|
+
The following are lists of public methods and functions currently available in the SOM class. The full documentation of each method can be found in the [source code](./python_som/__init__.py):
|
|
86
|
+
|
|
87
|
+
### Utility functions
|
|
88
|
+
* _asymptotic_decay
|
|
89
|
+
* _linear_decay
|
|
90
|
+
* _exponential_decay
|
|
91
|
+
* _inverse_decay
|
|
92
|
+
* _euclidean_distance
|
|
93
|
+
|
|
94
|
+
### SOM public methods
|
|
95
|
+
* SOM
|
|
96
|
+
* SOM.get_shape
|
|
97
|
+
* SOM.get_weights
|
|
98
|
+
* SOM.set_learning_rate
|
|
99
|
+
* SOM.set_neighborhood_radius
|
|
100
|
+
* SOM.activate
|
|
101
|
+
* SOM.winner
|
|
102
|
+
* SOM.quantization
|
|
103
|
+
* SOM.quantization_error
|
|
104
|
+
* SOM.distance_matrix
|
|
105
|
+
* SOM.activation_matrix
|
|
106
|
+
* SOM.winner_map
|
|
107
|
+
* SOM.label_map
|
|
108
|
+
* SOM.train
|
|
109
|
+
* SOM.weight_initialization
|
|
110
|
+
|
|
111
|
+
## References
|
|
112
|
+
This implemetation was based on the following paper, by Professor Teuvo Kohonen:
|
|
113
|
+
|
|
114
|
+
Teuvo Kohonen,
|
|
115
|
+
Essentials of the self-organizing map,
|
|
116
|
+
Neural Networks,
|
|
117
|
+
Volume 37,
|
|
118
|
+
2013,
|
|
119
|
+
Pages 52-65,
|
|
120
|
+
ISSN 0893-6080,
|
|
121
|
+
https://doi.org/10.1016/j.neunet.2012.09.018.
|