polyergalio 0.1.0__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.
Files changed (82) hide show
  1. polyergalio-0.1.0/CHANGES.md +21 -0
  2. polyergalio-0.1.0/LICENSE +7 -0
  3. polyergalio-0.1.0/MANIFEST.in +3 -0
  4. polyergalio-0.1.0/PKG-INFO +157 -0
  5. polyergalio-0.1.0/README.md +118 -0
  6. polyergalio-0.1.0/pyproject.toml +69 -0
  7. polyergalio-0.1.0/setup.cfg +4 -0
  8. polyergalio-0.1.0/src/polyergalio/__init__.py +12 -0
  9. polyergalio-0.1.0/src/polyergalio/distances.py +112 -0
  10. polyergalio-0.1.0/src/polyergalio/encoders/__init__.py +3 -0
  11. polyergalio-0.1.0/src/polyergalio/encoders/audio_encoders.py +484 -0
  12. polyergalio-0.1.0/src/polyergalio/encoders/categorical_encoders.py +201 -0
  13. polyergalio-0.1.0/src/polyergalio/encoders/chronologic_encoders.py +213 -0
  14. polyergalio-0.1.0/src/polyergalio/encoders/encoder_constants.py +23 -0
  15. polyergalio-0.1.0/src/polyergalio/encoders/encoder_utils.py +294 -0
  16. polyergalio-0.1.0/src/polyergalio/encoders/encoders.py +62 -0
  17. polyergalio-0.1.0/src/polyergalio/encoders/numeric_encoders.py +235 -0
  18. polyergalio-0.1.0/src/polyergalio/encoders/pipeline.py +671 -0
  19. polyergalio-0.1.0/src/polyergalio/encoders/tokenizer.py +622 -0
  20. polyergalio-0.1.0/src/polyergalio/encoders/tokenizer_fitting.py +167 -0
  21. polyergalio-0.1.0/src/polyergalio/generators/__init__.py +4 -0
  22. polyergalio-0.1.0/src/polyergalio/generators/data_generators.py +1012 -0
  23. polyergalio-0.1.0/src/polyergalio/generators/periodic_signal_gen.py +59 -0
  24. polyergalio-0.1.0/src/polyergalio/models/__init__.py +3 -0
  25. polyergalio-0.1.0/src/polyergalio/models/activations.py +200 -0
  26. polyergalio-0.1.0/src/polyergalio/models/clustering/__init__.py +3 -0
  27. polyergalio-0.1.0/src/polyergalio/models/clustering/centroid_network.py +599 -0
  28. polyergalio-0.1.0/src/polyergalio/models/clustering/cluster_metrics.py +340 -0
  29. polyergalio-0.1.0/src/polyergalio/models/clustering/freeplsom_clustering.py +572 -0
  30. polyergalio-0.1.0/src/polyergalio/models/clustering/gplsom_clustering.py +510 -0
  31. polyergalio-0.1.0/src/polyergalio/models/clustering/plsom_clustering.py +685 -0
  32. polyergalio-0.1.0/src/polyergalio/models/clustering/plsom_utils.py +245 -0
  33. polyergalio-0.1.0/src/polyergalio/models/constants.py +75 -0
  34. polyergalio-0.1.0/src/polyergalio/models/embedding/__init__.py +0 -0
  35. polyergalio-0.1.0/src/polyergalio/models/embedding/embedding.py +128 -0
  36. polyergalio-0.1.0/src/polyergalio/models/embedding/positional.py +323 -0
  37. polyergalio-0.1.0/src/polyergalio/models/layers/__init__.py +1 -0
  38. polyergalio-0.1.0/src/polyergalio/models/layers/basal_layers.py +744 -0
  39. polyergalio-0.1.0/src/polyergalio/models/layers/decision_layers.py +495 -0
  40. polyergalio-0.1.0/src/polyergalio/models/layers/fft_layers.py +370 -0
  41. polyergalio-0.1.0/src/polyergalio/models/layers/mixture_layers.py +647 -0
  42. polyergalio-0.1.0/src/polyergalio/models/layers/operator_layers.py +612 -0
  43. polyergalio-0.1.0/src/polyergalio/models/layers/spectre_layers.py +1344 -0
  44. polyergalio-0.1.0/src/polyergalio/models/layers/wavelet_layers.py +453 -0
  45. polyergalio-0.1.0/src/polyergalio/models/model_loss.py +395 -0
  46. polyergalio-0.1.0/src/polyergalio/models/neural_network.py +724 -0
  47. polyergalio-0.1.0/src/polyergalio/models/optimizers.py +142 -0
  48. polyergalio-0.1.0/src/polyergalio/models/supervised/__init__.py +3 -0
  49. polyergalio-0.1.0/src/polyergalio/models/supervised/relative_weights.py +196 -0
  50. polyergalio-0.1.0/src/polyergalio/models/supervised/scg_regression.py +563 -0
  51. polyergalio-0.1.0/src/polyergalio/models/supervised/trees/__init__.py +0 -0
  52. polyergalio-0.1.0/src/polyergalio/models/supervised/trees/tree_models.py +536 -0
  53. polyergalio-0.1.0/src/polyergalio/transforms/__init__.py +0 -0
  54. polyergalio-0.1.0/src/polyergalio/transforms/calibrations.py +654 -0
  55. polyergalio-0.1.0/src/polyergalio/transforms/projections.py +99 -0
  56. polyergalio-0.1.0/src/polyergalio/types.py +245 -0
  57. polyergalio-0.1.0/src/polyergalio/utilities.py +162 -0
  58. polyergalio-0.1.0/src/polyergalio/visuals/__init__.py +3 -0
  59. polyergalio-0.1.0/src/polyergalio/visuals/animation_utils.py +55 -0
  60. polyergalio-0.1.0/src/polyergalio/visuals/cluster_visuals.py +308 -0
  61. polyergalio-0.1.0/src/polyergalio/visuals/nnet_visuals.py +128 -0
  62. polyergalio-0.1.0/src/polyergalio/visuals/supervised_visuals.py +323 -0
  63. polyergalio-0.1.0/src/polyergalio.egg-info/PKG-INFO +157 -0
  64. polyergalio-0.1.0/src/polyergalio.egg-info/SOURCES.txt +80 -0
  65. polyergalio-0.1.0/src/polyergalio.egg-info/dependency_links.txt +1 -0
  66. polyergalio-0.1.0/src/polyergalio.egg-info/requires.txt +18 -0
  67. polyergalio-0.1.0/src/polyergalio.egg-info/top_level.txt +1 -0
  68. polyergalio-0.1.0/tests/test_activations.py +178 -0
  69. polyergalio-0.1.0/tests/test_clustering.py +421 -0
  70. polyergalio-0.1.0/tests/test_decision_layers.py +342 -0
  71. polyergalio-0.1.0/tests/test_embedding.py +439 -0
  72. polyergalio-0.1.0/tests/test_encoders.py +154 -0
  73. polyergalio-0.1.0/tests/test_fixtures.py +149 -0
  74. polyergalio-0.1.0/tests/test_generators.py +523 -0
  75. polyergalio-0.1.0/tests/test_layers.py +565 -0
  76. polyergalio-0.1.0/tests/test_losses.py +160 -0
  77. polyergalio-0.1.0/tests/test_network.py +988 -0
  78. polyergalio-0.1.0/tests/test_optimizers.py +226 -0
  79. polyergalio-0.1.0/tests/test_serialization.py +483 -0
  80. polyergalio-0.1.0/tests/test_supervised.py +146 -0
  81. polyergalio-0.1.0/tests/test_transforms.py +353 -0
  82. polyergalio-0.1.0/tests/test_utilities.py +217 -0
@@ -0,0 +1,21 @@
1
+ 0.1.0
2
+ =====================
3
+ Renamed the package from ml_tools to polyergalio and prepared for PyPI
4
+ publication:
5
+ - src/ml_tools became src/polyergalio, all imports updated
6
+ - pyproject.toml: dynamic version resolution, explicit src-layout
7
+ package discovery
8
+ - removed import-time logging
9
+
10
+ 0.0.1
11
+ =====================
12
+ Version algorithm aggregations - serialization and unserialize functions added
13
+ - Clusterings, SOM, PLSOM, free-SOM and CentNN modified
14
+ - Classification models updated with
15
+ - NNet layers, network and DAG
16
+ - Generator "shapes patterns" added for clustering and images
17
+
18
+ 0.0.0
19
+ =====================
20
+ Version zero - collecting algorithms and tools from my other repos and
21
+ projects.
@@ -0,0 +1,7 @@
1
+ Copyright 2025
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,3 @@
1
+ include LICENSE
2
+ include README.md
3
+ include CHANGES.md
@@ -0,0 +1,157 @@
1
+ Metadata-Version: 2.4
2
+ Name: polyergalio
3
+ Version: 0.1.0
4
+ Summary: From-scratch machine learning methods -- encoding, supervised learning, and clustering -- built on NumPy and SciPy
5
+ Author-email: David Graey <graeyband@gmail.com>, Dr Charles Anderson <Chuck.Anderson@colostate.edu>
6
+ License: MIT
7
+ Project-URL: Homepage, https://github.com/Davidgraey/ml-tools
8
+ Project-URL: Repository, https://github.com/Davidgraey/ml-tools
9
+ Project-URL: Changelog, https://github.com/Davidgraey/ml-tools/blob/main/CHANGES.md
10
+ Project-URL: Bug Tracker, https://github.com/Davidgraey/ml-tools/issues
11
+ Keywords: machine-learning,numpy,clustering,self-organizing-map,neural-network
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Intended Audience :: Science/Research
14
+ Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
21
+ Requires-Python: >=3.10
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: matplotlib
25
+ Requires-Dist: numpy
26
+ Requires-Dist: scipy
27
+ Provides-Extra: gui
28
+ Requires-Dist: matplotlib; extra == "gui"
29
+ Provides-Extra: audio
30
+ Requires-Dist: librosa; extra == "audio"
31
+ Provides-Extra: test
32
+ Requires-Dist: pytest; extra == "test"
33
+ Provides-Extra: dev
34
+ Requires-Dist: pytest; extra == "dev"
35
+ Requires-Dist: coverage; extra == "dev"
36
+ Requires-Dist: build; extra == "dev"
37
+ Requires-Dist: twine; extra == "dev"
38
+ Dynamic: license-file
39
+
40
+ # PolyerGalio
41
+ ![Python](https://img.shields.io/badge/python-3.10%2B-blue.svg)
42
+ ![NumPy](https://img.shields.io/badge/numpy-%3E%3D1.20-blue)
43
+ ![SciPy](https://img.shields.io/badge/scipy-%3E%3D1.7-blue)
44
+ ![License](https://img.shields.io/github/license/davidgraey/ml-tools)
45
+ ![Last Commit](https://img.shields.io/github/last-commit/davidgraey/ml-tools)
46
+ ![Repo Size](https://img.shields.io/github/repo-size/davidgraey/ml-tools)
47
+ ![Stars](https://img.shields.io/github/stars/davidgraey/ml-tools?style=social)
48
+
49
+ ---
50
+
51
+ ## Overview
52
+
53
+ **PolyerGalio** (Greek for "multiple tools") is a collection of implemented machine learning methods ranging from
54
+ data encoding and processing pipelines to supervised learning and clustering.
55
+
56
+ The focus of this repository is:
57
+ - **Classic and alternative ML algorithms implemented with a unified
58
+ interface**
59
+ - **Numerical stability and performance**
60
+ - **Novel extensions and original research contributions**
61
+
62
+ All algorithms are implemented in **NumPy** and **SciPy**, with minimal external dependencies.
63
+
64
+ ---
65
+ ## Installation
66
+
67
+ ```bash
68
+ pip install polyergalio
69
+ ```
70
+
71
+ For local development:
72
+
73
+ ```bash
74
+ pip install -e ".[test]"
75
+ ```
76
+
77
+ ---
78
+ ## Implemented Methods
79
+
80
+ ### 🔹 Encoding and Embedding Creation
81
+ `src/polyergalio/encoders/*`
82
+ - Categorical variable pipeline
83
+ - Chronological variable (cyclical and absolute) pipeline
84
+ - Numeric (normalized and raw) pipeline
85
+ - Trainable Fourier Embedding pipeline
86
+ - Trainable Text embedding pipeline
87
+ - tokenization with sentencepiece
88
+
89
+ ### 🔹 Toy Dataset Generation
90
+ `src/polyergalio/generators/*`
91
+
92
+ ### 🔹 Supervised Learning
93
+ `src/polyergalio/models/supervised/*`
94
+ #### Scaled Conjugate Gradient (SCG)
95
+ - SCG for gradient descent applied to regression and logistic regression
96
+ *(Møller); (Anderson)*
97
+ - SCG regression with **Elastic Net regularization** *(novel)*
98
+ - SCG classification:
99
+ - Binary
100
+ - Multinomial
101
+ - Multilabel
102
+
103
+ #### Relative Weights (RW)
104
+ - Johnson’s Relative Weights regression *(Johnson)*
105
+ - Relative Weights applied to logistic regression
106
+ *(Solís & Pasquier); (Tonidandel & LeBreton)*
107
+
108
+ #### Tree Algorithms (EBM / EBTM)
109
+ - Tree algorithms - Explainable Boosted-Tree Model (EBM)
110
+
111
+ ### 🔹 Unsupervised Learning & Clustering
112
+ `src/polyergalio/models/clustering/*`
113
+ #### Self-Organizing Maps
114
+ - Self Organizing Maps, Parameterless Self-Organizing Maps - PLSOM
115
+ *(Kohonen); (Berglund & Sitte)*
116
+ - Clustering and dimensionality reduction without hyperparameter adjustment
117
+ - Growing Self Organizing Maps, Parameterless (grid)
118
+ - Grid-free Growing Parameterless Self Organizing Maps
119
+ - FreeSOM - grows and shrinks under conditional updates
120
+ - *(novel)* fusion of Neural Gas and PLSOM
121
+
122
+
123
+ #### Centroid Neural Networks (CENTNN)
124
+ - Novel **Centroid Neural Network** for fast clustering and optimization
125
+ *(Park, Dong-Chul)*
126
+ - CENTNN with **N-dimensional density modeling**
127
+ - *(novel)*
128
+
129
+ ---
130
+ ## Status
131
+
132
+ **Active research / experimental**
133
+ APIs may change as methods are refined and extended.
134
+
135
+ ---
136
+ ## Authors and Contributors
137
+ - "David Graey", "graeyband@gmail.com"
138
+ - "Dr Charles Anderson", "Chuck.Anderson@colostate.edu"
139
+
140
+ ---
141
+ ## References
142
+
143
+ Primary academic references are cited inline.
144
+ Full bibliographic references may be added in `/docs` in the future.
145
+
146
+ https://packaging.python.org/en/latest/tutorials/packaging-projects/
147
+
148
+ https://packaging.python.org/en/latest/tutorials/creating-documentation/
149
+
150
+
151
+ # Visuals & Diagrams
152
+ https://mermaid.js.org/config/Tutorials.html
153
+ ```mermaid
154
+ flowchart LR;
155
+ A --> B;
156
+ A --> C;
157
+ ```
@@ -0,0 +1,118 @@
1
+ # PolyerGalio
2
+ ![Python](https://img.shields.io/badge/python-3.10%2B-blue.svg)
3
+ ![NumPy](https://img.shields.io/badge/numpy-%3E%3D1.20-blue)
4
+ ![SciPy](https://img.shields.io/badge/scipy-%3E%3D1.7-blue)
5
+ ![License](https://img.shields.io/github/license/davidgraey/ml-tools)
6
+ ![Last Commit](https://img.shields.io/github/last-commit/davidgraey/ml-tools)
7
+ ![Repo Size](https://img.shields.io/github/repo-size/davidgraey/ml-tools)
8
+ ![Stars](https://img.shields.io/github/stars/davidgraey/ml-tools?style=social)
9
+
10
+ ---
11
+
12
+ ## Overview
13
+
14
+ **PolyerGalio** (Greek for "multiple tools") is a collection of implemented machine learning methods ranging from
15
+ data encoding and processing pipelines to supervised learning and clustering.
16
+
17
+ The focus of this repository is:
18
+ - **Classic and alternative ML algorithms implemented with a unified
19
+ interface**
20
+ - **Numerical stability and performance**
21
+ - **Novel extensions and original research contributions**
22
+
23
+ All algorithms are implemented in **NumPy** and **SciPy**, with minimal external dependencies.
24
+
25
+ ---
26
+ ## Installation
27
+
28
+ ```bash
29
+ pip install polyergalio
30
+ ```
31
+
32
+ For local development:
33
+
34
+ ```bash
35
+ pip install -e ".[test]"
36
+ ```
37
+
38
+ ---
39
+ ## Implemented Methods
40
+
41
+ ### 🔹 Encoding and Embedding Creation
42
+ `src/polyergalio/encoders/*`
43
+ - Categorical variable pipeline
44
+ - Chronological variable (cyclical and absolute) pipeline
45
+ - Numeric (normalized and raw) pipeline
46
+ - Trainable Fourier Embedding pipeline
47
+ - Trainable Text embedding pipeline
48
+ - tokenization with sentencepiece
49
+
50
+ ### 🔹 Toy Dataset Generation
51
+ `src/polyergalio/generators/*`
52
+
53
+ ### 🔹 Supervised Learning
54
+ `src/polyergalio/models/supervised/*`
55
+ #### Scaled Conjugate Gradient (SCG)
56
+ - SCG for gradient descent applied to regression and logistic regression
57
+ *(Møller); (Anderson)*
58
+ - SCG regression with **Elastic Net regularization** *(novel)*
59
+ - SCG classification:
60
+ - Binary
61
+ - Multinomial
62
+ - Multilabel
63
+
64
+ #### Relative Weights (RW)
65
+ - Johnson’s Relative Weights regression *(Johnson)*
66
+ - Relative Weights applied to logistic regression
67
+ *(Solís & Pasquier); (Tonidandel & LeBreton)*
68
+
69
+ #### Tree Algorithms (EBM / EBTM)
70
+ - Tree algorithms - Explainable Boosted-Tree Model (EBM)
71
+
72
+ ### 🔹 Unsupervised Learning & Clustering
73
+ `src/polyergalio/models/clustering/*`
74
+ #### Self-Organizing Maps
75
+ - Self Organizing Maps, Parameterless Self-Organizing Maps - PLSOM
76
+ *(Kohonen); (Berglund & Sitte)*
77
+ - Clustering and dimensionality reduction without hyperparameter adjustment
78
+ - Growing Self Organizing Maps, Parameterless (grid)
79
+ - Grid-free Growing Parameterless Self Organizing Maps
80
+ - FreeSOM - grows and shrinks under conditional updates
81
+ - *(novel)* fusion of Neural Gas and PLSOM
82
+
83
+
84
+ #### Centroid Neural Networks (CENTNN)
85
+ - Novel **Centroid Neural Network** for fast clustering and optimization
86
+ *(Park, Dong-Chul)*
87
+ - CENTNN with **N-dimensional density modeling**
88
+ - *(novel)*
89
+
90
+ ---
91
+ ## Status
92
+
93
+ **Active research / experimental**
94
+ APIs may change as methods are refined and extended.
95
+
96
+ ---
97
+ ## Authors and Contributors
98
+ - "David Graey", "graeyband@gmail.com"
99
+ - "Dr Charles Anderson", "Chuck.Anderson@colostate.edu"
100
+
101
+ ---
102
+ ## References
103
+
104
+ Primary academic references are cited inline.
105
+ Full bibliographic references may be added in `/docs` in the future.
106
+
107
+ https://packaging.python.org/en/latest/tutorials/packaging-projects/
108
+
109
+ https://packaging.python.org/en/latest/tutorials/creating-documentation/
110
+
111
+
112
+ # Visuals & Diagrams
113
+ https://mermaid.js.org/config/Tutorials.html
114
+ ```mermaid
115
+ flowchart LR;
116
+ A --> B;
117
+ A --> C;
118
+ ```
@@ -0,0 +1,69 @@
1
+ [build-system]
2
+ requires = ["setuptools >= 61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "polyergalio"
7
+ dynamic = ["version"]
8
+ authors = [
9
+ {name = "David Graey", email = "graeyband@gmail.com"},
10
+ {name = "Dr Charles Anderson", email = "Chuck.Anderson@colostate.edu"}
11
+ ]
12
+ dependencies = [
13
+ "matplotlib",
14
+ "numpy",
15
+ "scipy"
16
+ ]
17
+
18
+ description = "From-scratch machine learning methods -- encoding, supervised learning, and clustering -- built on NumPy and SciPy"
19
+ readme = "README.md"
20
+ requires-python = ">=3.10"
21
+ license = {text = "MIT"}
22
+ keywords = ["machine-learning", "numpy", "clustering", "self-organizing-map", "neural-network"]
23
+
24
+ classifiers = [
25
+ "Development Status :: 3 - Alpha",
26
+ "Intended Audience :: Science/Research",
27
+ "License :: OSI Approved :: MIT License",
28
+ "Operating System :: OS Independent",
29
+ "Programming Language :: Python :: 3",
30
+ "Programming Language :: Python :: 3.10",
31
+ "Programming Language :: Python :: 3.11",
32
+ "Programming Language :: Python :: 3.12",
33
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
34
+ ]
35
+
36
+ [project.optional-dependencies]
37
+ gui = ["matplotlib"]
38
+ audio = ["librosa"]
39
+ test = ["pytest"]
40
+ dev = ["pytest", "coverage", "build", "twine"]
41
+
42
+ [tool.setuptools.dynamic]
43
+ version = {attr = "polyergalio.__version__"}
44
+
45
+ [tool.setuptools.packages.find]
46
+ where = ["src"]
47
+
48
+ [tool.coverage.run]
49
+ source = ["src/polyergalio"]
50
+ branch = true
51
+
52
+ [tool.coverage.report]
53
+ show_missing = true
54
+ skip_covered = true
55
+
56
+ [tool.pytest.ini_options]
57
+ testpaths = ["tests"]
58
+ # src layout, so tests import polyergalio without an editable install
59
+ pythonpath = ["src", "tests"]
60
+ addopts = "--strict-markers"
61
+ markers = [
62
+ "slow: finite-difference gradient checks, run with -m 'not slow' to skip",
63
+ ]
64
+
65
+ [project.urls]
66
+ Homepage = "https://github.com/Davidgraey/ml-tools"
67
+ Repository = "https://github.com/Davidgraey/ml-tools"
68
+ Changelog = "https://github.com/Davidgraey/ml-tools/blob/main/CHANGES.md"
69
+ "Bug Tracker" = "https://github.com/Davidgraey/ml-tools/issues"
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,12 @@
1
+ """
2
+ polyergalio: implemented machine learning methods -- encoding,
3
+ supervised learning, and clustering -- built on NumPy and SciPy.
4
+ """
5
+ import logging
6
+
7
+ __version__ = "0.1.0"
8
+
9
+ log = logging.getLogger(__name__)
10
+ log.addHandler(logging.NullHandler())
11
+
12
+ __all__ = ["__version__"]
@@ -0,0 +1,112 @@
1
+ import numpy as np
2
+ from numpy.typing import NDArray
3
+ from scipy.spatial.distance import cdist, pdist
4
+ from polyergalio.utilities import preformat_expected_shapes
5
+ from polyergalio.models.constants import EPSILON
6
+
7
+
8
+ ########### DISTANCES ###########
9
+ def manhattan_distance(x: NDArray, y: NDArray, summed: bool = True) -> NDArray:
10
+ """
11
+ aka Chebyshev
12
+ :param x:
13
+ :param y:
14
+ :return:
15
+ """
16
+ x, y = preformat_expected_shapes(x, y)
17
+ # sum(np.abs(x-y), axis=?)
18
+ if summed:
19
+ return np.sum(np.abs(x - y), axis=-1)
20
+ else:
21
+ return np.abs(x - y)
22
+
23
+
24
+ def grid_manhattan_distance(row_a, col_a, row_b, col_b):
25
+ return abs(row_a - row_b) + abs(col_a - col_b)
26
+
27
+
28
+ def euclidian_distance(x: NDArray, y: NDArray) -> NDArray:
29
+ """
30
+ for size (5000, 100), per sample, np.sqrtsum method took 1.07ms
31
+ linalg.norm took 1.21ms
32
+ numpy 1.20.3
33
+ :param x:
34
+ :param y:
35
+ :return:
36
+ """
37
+ x, y = preformat_expected_shapes(x, y)
38
+ return np.sqrt(np.sum((x - y) ** 2, axis=-1)) # + EPSILON
39
+ # return cdist(x, y, metric='euclidean')
40
+
41
+
42
+ def norm_euclidian_distance(x: NDArray, y: NDArray) -> NDArray:
43
+ # numerator = (np.linalg.norm((x - np.mean(x)) - (y - np.mean(y))) ** 2)
44
+ # denom = (np.linalg.norm(x - np.mean(x)) ** 2 + np.linalg.norm(y - np.mean(y)) ** 2)
45
+ # return 0.5 * (numerator / denom)
46
+
47
+ vx = np.var(x, axis=-1)
48
+ vy = np.var(y, axis=-1)
49
+ return 0.5 * (vx / (vy + vx))
50
+
51
+
52
+ def mahalonobis_distance(x: NDArray, y: NDArray) -> NDArray:
53
+ """
54
+
55
+ multivariate equivilant of euclidian distance, comparing point to distribution
56
+ :param x:
57
+ :param y:
58
+ :return:
59
+ """
60
+ stacked_vecs = np.vstack([x, y])
61
+ covariance = np.cov(stacked_vecs.T)
62
+ inv_covariance = np.linalg.inv(covariance)
63
+ delta = x - y
64
+ # may have to fix reshape for multi-dim...
65
+ return np.sqrt(np.einsum('nj,jk,nk->n', delta, inv_covariance, delta)).reshape(x.shape[0], -1)
66
+
67
+
68
+ def hamming_distance(x: NDArray, y: NDArray) -> NDArray:
69
+ """
70
+ :param x:
71
+ :param y:
72
+ :return:
73
+ """
74
+ # axis?
75
+ return np.count_nonzero(x != y, axis=-1)
76
+
77
+
78
+ def cosine_distance(x: NDArray, y: NDArray) -> NDArray:
79
+ """
80
+ inverse cosine similarity
81
+ :param x:
82
+ :param y:
83
+ :return:
84
+ """
85
+ x, y = preformat_expected_shapes(x, y)
86
+ return 1 - cosine_similarity(x, y)
87
+
88
+
89
+ # +---------Similarity---------
90
+ def cosine_similarity(x: NDArray, y: NDArray) -> NDArray:
91
+ """
92
+ requires numpy v1.7+ for 'where' in np.divide()
93
+ numpy array is [samples, dimension_1, dimension_2, ...]
94
+ we will compute cosine distance per samples dimension
95
+ input array is [500, 5] - output will be [500, 1]
96
+ :param x:
97
+ :param y:
98
+ :return:
99
+ """
100
+ dot = np.sum(x * y, axis=-1)
101
+ x_norm = np.sqrt(np.sum(x * x, axis=-1))
102
+ y_norm = np.sqrt(np.sum(y * y, axis=-1))
103
+
104
+ denom = x_norm * y_norm
105
+ return dot / np.maximum(denom, EPSILON)
106
+
107
+
108
+ def jaccard_similarity(x: NDArray, y: NDArray) -> NDArray:
109
+ """aka Jaccard Index
110
+ for use in boolean / on-hot / binary array
111
+ """
112
+ return np.bitwise_and(x, y).sum(axis=-1) / np.bitwise_or(x, y).sum(axis=-1)
@@ -0,0 +1,3 @@
1
+ import logging
2
+
3
+ log = logging.getLogger(__name__)