API reference¶
python_som ¶
Python implementation of Kohonen's 2-D self-organizing map.
The public surface is :class:SOM, plus the decay, distance and neighborhood functions that can be
passed to it or used directly.
import numpy as np, python_som data = np.random.default_rng(0).normal(size=(150, 4)) som = python_som.SOM(x=10, y=10, input_len=4, random_seed=0) som.weight_initialization(mode="linear", data=data) error = som.train(data, n_iteration=100, mode="batch")
Internally, :mod:python_som._core holds every numeric decision as pure functions over NumPy
arrays and imports nothing else; a thin shell around it handles conversion, state and I/O.
Reference: Teuvo Kohonen, Essentials of the self-organizing map, Neural Networks 37 (2013) 52-65, ISSN 0893-6080, https://doi.org/10.1016/j.neunet.2012.09.018
SOM ¶
A 2-D self-organizing map over NumPy arrays, pandas DataFrames or plain lists.
Reference: Teuvo Kohonen, Essentials of the self-organizing map, Neural Networks 37 (2013) 52-65, https://doi.org/10.1016/j.neunet.2012.09.018
Source code in src/python_som/_som.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 | |
last_report
property
¶
last_report
What the most recent :meth:train call did, or None if it has not been called.
Returns:
| Type | Description |
|---|---|
TrainingReport | None
|
The report for the last training run. |
weights_
property
¶
weights_
The models, under the trailing-underscore name the estimator convention uses.
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Models of shape |
n_features_in_
property
¶
n_features_in_
Number of features the map accepts, as KMeans reports it.
Returns:
| Type | Description |
|---|---|
int
|
Number of features. |
quantization_error_
property
¶
quantization_error_
Quantization error of the last training run, or None before the first.
The counterpart of KMeans.inertia_.
Returns:
| Type | Description |
|---|---|
float | None
|
The error, or None. |
__init__ ¶
__init__(
x,
y,
input_len,
learning_rate=0.5,
learning_rate_decay=asymptotic_decay,
neighborhood_radius=1.0,
neighborhood_radius_decay=asymptotic_decay,
neighborhood_function=Neighborhood.GAUSSIAN,
distance_function=euclidean_distance,
cyclic_x=False,
cyclic_y=False,
random_seed=None,
data=None,
*,
min_neighborhood_radius=0.5,
)
Construct a self-organizing map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
int | None
|
Number of rows. If None, chosen from |
required |
y
|
int | None
|
Number of columns. If None, chosen from |
required |
input_len
|
int
|
Number of features per input vector. |
required |
learning_rate
|
float
|
Initial learning rate. Irrelevant for batch training. |
0.5
|
learning_rate_decay
|
DecayFunction
|
Decay function for the learning rate. |
asymptotic_decay
|
neighborhood_radius
|
float
|
Initial neighborhood radius. |
1.0
|
neighborhood_radius_decay
|
DecayFunction
|
Decay function for the neighborhood radius. |
asymptotic_decay
|
neighborhood_function
|
Neighborhood | NeighborhoodStr
|
One of |
GAUSSIAN
|
distance_function
|
DistanceFunction
|
Dissimilarity between an input vector and the models. |
euclidean_distance
|
cyclic_x
|
bool
|
Whether the map wraps around vertically (toroidal). |
False
|
cyclic_y
|
bool
|
Whether the map wraps around horizontally (toroidal). |
False
|
random_seed
|
int | None
|
Seed for this instance's random generator. |
None
|
data
|
DataLike | None
|
Dataset used for automatic sizing. Required when |
None
|
min_neighborhood_radius
|
float
|
Floor applied to the decayed radius during training. Kohonen (2013) Section 4.2 warns that "the final value of sigma shall not go to zero, because otherwise the process loses its ordering power. It should always remain, say, above half of the grid spacing." Defaults to 0.5, that half-spacing. |
0.5
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the dimensions, the neighborhood function name, or
|
Source code in src/python_som/_som.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
get_shape ¶
get_shape()
Return the shape of the network.
Source code in src/python_som/_som.py
276 277 278 | |
get_weights ¶
get_weights()
Return the weight matrix of the network.
Source code in src/python_som/_som.py
280 281 282 | |
get_random_seed ¶
get_random_seed()
Return the seed of this instance's random generator.
Source code in src/python_som/_som.py
284 285 286 | |
set_learning_rate ¶
set_learning_rate(learning_rate)
Set the learning rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
learning_rate
|
float
|
New learning rate. |
required |
Source code in src/python_som/_som.py
288 289 290 291 292 293 | |
set_neighborhood_radius ¶
set_neighborhood_radius(neighborhood_radius)
Set the neighborhood radius.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
neighborhood_radius
|
float
|
New neighborhood radius. |
required |
Source code in src/python_som/_som.py
295 296 297 298 299 300 | |
neighborhood ¶
neighborhood(c, sigma)
Evaluate the neighborhood function centred on c.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c
|
tuple[int, int]
|
Coordinates of the winner. |
required |
sigma
|
float
|
Neighborhood radius. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Neighborhood weights, with the shape of the network. |
Source code in src/python_som/_som.py
302 303 304 305 306 307 308 309 | |
activate ¶
activate(x)
Return the distance from x to every model of the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
Input vector. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Distances, with the shape of the network. |
Source code in src/python_som/_som.py
313 314 315 316 317 318 319 | |
winner ¶
winner(x)
Return the coordinates of the best-matching unit for x.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ArrayLike
|
Input vector. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
Coordinates of the winner. |
Source code in src/python_som/_som.py
321 322 323 324 325 326 327 | |
quantization ¶
quantization(data)
Return the distance from each sample to its best-matching model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataLike
|
Dataset of shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
One distance per sample. |
Source code in src/python_som/_som.py
329 330 331 332 333 334 335 | |
quantization_error ¶
quantization_error(data)
Return the mean distance from each sample to its best-matching model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataLike
|
Dataset of shape |
required |
Returns:
| Type | Description |
|---|---|
float
|
Quantization error. |
Source code in src/python_som/_som.py
337 338 339 340 341 342 343 | |
distance_matrix ¶
distance_matrix(normalize=False)
Return the U-matrix: the summed distance from each model to its immediate neighbours.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalize
|
bool
|
Whether to rescale the result to |
False
|
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
U-matrix, with the shape of the network. |
Source code in src/python_som/_som.py
347 348 349 350 351 352 353 354 355 356 357 358 359 | |
activation_matrix ¶
activation_matrix(data)
Return how many samples map to each node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataLike
|
Dataset of shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Counts, with the shape of the network. |
Source code in src/python_som/_som.py
361 362 363 364 365 366 367 368 369 | |
winner_map ¶
winner_map(data)
Return, for each node, the samples that map to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataLike
|
Dataset of shape |
required |
Returns:
| Type | Description |
|---|---|
dict[tuple[int, int], list[NDArray[Any]]]
|
Mapping from node coordinates to the samples assigned to that node. |
Source code in src/python_som/_som.py
371 372 373 374 375 376 377 | |
label_map ¶
label_map(data, labels)
Return, for each node, the frequency of each label mapped to it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataLike
|
Dataset of shape |
required |
labels
|
DataLike
|
One label per sample, in the same order as |
required |
Returns:
| Type | Description |
|---|---|
dict[tuple[int, int], Counter[Any]]
|
Mapping from node coordinates to a label counter. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/python_som/_som.py
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | |
train ¶
train(
data,
n_iteration=None,
mode=TrainingMode.RANDOM,
verbose=False,
)
Train the map and return the resulting quantization error.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataLike
|
Training dataset of shape |
required |
n_iteration
|
int | None
|
Number of iterations. Defaults to 1000 per sample for the stepwise modes and 10 per sample for batch. |
None
|
mode
|
TrainingMode | TrainingModeStr
|
One of |
RANDOM
|
verbose
|
bool
|
Whether to report progress. Emits a tqdm progress bar when tqdm is
installed, and logs at INFO level on the |
False
|
Returns:
| Type | Description |
|---|---|
float
|
Quantization error after training. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/python_som/_som.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | |
fit ¶
fit(
X,
y=None,
*,
n_iteration=None,
mode=TrainingMode.RANDOM,
verbose=False,
)
Train the map and return it, so calls can be chained.
y is accepted and ignored, which is what lets Pipeline call every step the same way.
Training options are keyword arguments here rather than constructor arguments;
:mod:python_som.sklearn takes them at construction, because get_params must expose
them to GridSearchCV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataLike
|
Training dataset of shape |
required |
y
|
object
|
Ignored. |
None
|
n_iteration
|
int | None
|
Number of iterations. Defaults as for :meth: |
None
|
mode
|
TrainingMode | TrainingModeStr
|
Training mode. |
RANDOM
|
verbose
|
bool
|
Whether to show a progress bar. |
False
|
Returns:
| Type | Description |
|---|---|
SOM
|
This map, trained. |
Source code in src/python_som/_som.py
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | |
transform ¶
transform(X)
Return the distance from each sample to every model.
The counterpart of KMeans.transform, which "transforms X to a cluster-distance space".
Here the space has one dimension per node, so the result is (n_samples, x * y) with the
grid flattened in C order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataLike
|
Dataset of shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Distances of shape |
Source code in src/python_som/_som.py
526 527 528 529 530 531 532 533 534 535 536 537 | |
fit_transform ¶
fit_transform(X, y=None, **kwargs)
Train on X and return its distances to every model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataLike
|
Training dataset. |
required |
y
|
object
|
Ignored. |
None
|
kwargs
|
Any
|
Passed to :meth: |
{}
|
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Distances of shape |
Source code in src/python_som/_som.py
539 540 541 542 543 544 545 546 547 548 549 550 551 552 | |
predict ¶
predict(X)
Return the index of the best-matching node for each sample.
A flat index, not a (row, column) pair, because that is what scorers and
confusion_matrix assume. Recover the grid position with
np.unravel_index(som.predict(X), som.get_shape()); :meth:winner still returns
coordinates for a single sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataLike
|
Dataset of shape |
required |
Returns:
| Type | Description |
|---|---|
NDArray[integer]
|
One flat node index per sample. |
Source code in src/python_som/_som.py
554 555 556 557 558 559 560 561 562 563 564 565 | |
score ¶
score(X, y=None)
Return the negated quantization error, so that larger is better.
The sign is the convention every scikit-learn scorer follows: GridSearchCV maximises,
and quantization error is something to minimise. Without the negation a parameter search
would confidently select the worst map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
DataLike
|
Dataset to score. |
required |
y
|
object
|
Ignored. |
None
|
Returns:
| Type | Description |
|---|---|
float
|
Negated mean quantization error. |
Source code in src/python_som/_som.py
567 568 569 570 571 572 573 574 575 576 577 578 | |
get_params ¶
get_params(*, deep=True)
Return the constructor arguments that describe this map.
Strategies come back as the callables or names that were passed in, so that
SOM(**som.get_params()) rebuilds an equivalent map. That is what clone relies on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deep
|
bool
|
Accepted for signature compatibility; this estimator holds no sub-estimators. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Constructor arguments by name. |
Source code in src/python_som/_som.py
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 | |
set_params ¶
set_params(**params)
Set constructor-level parameters in place and return this map.
Only what can change without rebuilding the models: the rates, radii and decays. The grid
shape and input_len raise, rather than leaving a map whose models do not match its own
description. Replaces :meth:set_learning_rate and :meth:set_neighborhood_radius, which
still work and are removed in 1.0.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
params
|
Any
|
Parameters to set. |
{}
|
Returns:
| Type | Description |
|---|---|
SOM
|
This map. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a parameter is unknown or cannot be changed after construction. |
Source code in src/python_som/_som.py
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 | |
config ¶
config()
Describe this map in a form that can be written to a file.
Strategies appear by name. A callable that is not one of the package's registered functions
is recorded as its __name__, which preserves the provenance but cannot be resolved back
into a callable; :meth:load_npz says so explicitly rather than guessing.
Returns:
| Type | Description |
|---|---|
SOMConfig
|
The configuration. |
Source code in src/python_som/_som.py
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 | |
save_npz ¶
save_npz(path)
Write the models and their provenance to a single .npz file.
Weights as an array, everything else as JSON beside them: configuration, seed, generator state and the last training report. No pickle on either side.
The generator state is saved as well as the seed, which is what lets :meth:load_npz
resume the same stream rather than restarting it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
Destination file. |
required |
Source code in src/python_som/_som.py
690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 | |
load_npz
classmethod
¶
load_npz(
path,
*,
neighborhood_function=None,
learning_rate_decay=None,
neighborhood_radius_decay=None,
distance_function=None,
)
Rebuild a map saved by :meth:save_npz, models, generator state and all.
Continuing to train a loaded map gives the same weights as never having stopped, which is what the saved generator state is for.
The four keyword arguments are for maps trained with a function this package cannot resolve by name. Passing one the file did not need is harmless.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | PathLike[str]
|
File to read. |
required |
neighborhood_function
|
NeighborhoodFunction | None
|
Replacement for a neighborhood that cannot be resolved. |
None
|
learning_rate_decay
|
DecayFunction | None
|
Replacement for a learning-rate decay that cannot be resolved. |
None
|
neighborhood_radius_decay
|
DecayFunction | None
|
Replacement for a radius decay that cannot be resolved. |
None
|
distance_function
|
DistanceFunction | None
|
Replacement for a distance that cannot be resolved. |
None
|
Returns:
| Type | Description |
|---|---|
SOM
|
The reconstructed map. |
Raises:
| Type | Description |
|---|---|
ArtifactError
|
If the file is not a readable artifact, or names a function that cannot be resolved and was not supplied. |
Source code in src/python_som/_som.py
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 | |
weight_initialization ¶
weight_initialization(mode=WeightInit.RANDOM, **kwargs)
Initialize the models of the network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mode
|
WeightInit | WeightInitStr
|
One of |
RANDOM
|
kwargs
|
Any
|
Passed through to the chosen initializer. |
{}
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/python_som/_som.py
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 | |
Neighborhood functions¶
python_som._core._neighborhood ¶
Neighborhood functions: how the winner's correction spreads over the grid.
Kohonen (2013) Eq. (5) defines a neighborhood as a function of sqdist(c, i), the squared grid
distance between two nodes, so it must depend on that distance and not on the two axis offsets
separately. See :doc:/explanation/why-isotropy-matters for why an outer product of two 1-D
profiles is wrong for anything but the gaussian.
References: Teuvo Kohonen, Essentials of the self-organizing map, Neural Networks 37 (2013) 52-65, https://doi.org/10.1016/j.neunet.2012.09.018
O. J. Vrieze, Kohonen network, in: Artificial Neural Networks, Lecture Notes in Computer Science, vol. 931, Springer, 1995, pp. 83-100, https://doi.org/10.1007/BFb0027024
gaussian ¶
gaussian(shape, c, sigma, cyclic)
Gaussian neighborhood, exp(-sqdist(c, i) / (2 * sigma**2)).
Eq. (5) of Kohonen (2013) with the learning rate factored out, so h(c, c) == 1. Strictly
positive and monotonically decreasing with distance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
Grid
|
Shape of the network. |
required |
c
|
Coordinates
|
Coordinates of the winner. |
required |
sigma
|
float
|
Neighborhood radius. Must be finite and positive. |
required |
cyclic
|
tuple[bool, bool]
|
Whether each axis wraps around. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Neighborhood weights, with the shape of the network. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the radius is not a finite positive number. |
Source code in src/python_som/_core/_neighborhood.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
bubble ¶
bubble(shape, c, sigma, cyclic)
Flat neighborhood: 1 for nodes within sigma of the winner, 0 elsewhere.
The truncated inner lobe of the mexican hat, which Vrieze (1995) p. 85 calls "just as effective and sometimes even better" than a distance-dependent one.
The metric is Chebyshev, not Euclidean, so the region is a square: a node is included when
max(|dx|, |dy|) <= round(sigma). This follows Vrieze's appendix, where Kohonen's "up to a
certain radius" (Section 4.1) reads as Euclidean; the two sources differ, and
:doc:/explanation/why-isotropy-matters covers the consequence, that a Chebyshev ball is not
isotropic under the Euclidean metric.
A radius of zero is admissible here and not for the other two: it selects the winner alone, where they would divide by zero.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
Grid
|
Shape of the network. |
required |
c
|
Coordinates
|
Coordinates of the winner. |
required |
sigma
|
float
|
Neighborhood radius, rounded to the nearest integer. Must be finite and non-negative. |
required |
cyclic
|
tuple[bool, bool]
|
Whether each axis wraps around. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Neighborhood weights, with the shape of the network. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the radius is not a finite non-negative number. |
Source code in src/python_som/_core/_neighborhood.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | |
mexican_hat ¶
mexican_hat(shape, c, sigma, cyclic)
Mexican hat neighborhood, (1 - u) * exp(-u) over u = sqdist(c, i) / (2 * sigma**2).
The Ricker wavelet, or Laplacian of Gaussian: excitatory near the winner, inhibitory beyond it,
vanishing with distance (Vrieze 1995, Fig. 3). Normalized so h(c, c) == 1, zero at
sqrt(2) * sigma, minimum -exp(-2) at 2 * sigma.
Not an outer product of two 1-D Ricker wavelets, which is a different and wrong function; see
:doc:/explanation/why-isotropy-matters. Signed, so batch training rejects it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
Grid
|
Shape of the network. |
required |
c
|
Coordinates
|
Coordinates of the winner. |
required |
sigma
|
float
|
Neighborhood radius. Must be finite and positive. |
required |
cyclic
|
tuple[bool, bool]
|
Whether each axis wraps around. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Neighborhood weights, with the shape of the network. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the radius is not a finite positive number. |
Source code in src/python_som/_core/_neighborhood.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
axis_offsets ¶
axis_offsets(length, center, *, cyclic)
Signed offsets from center to every coordinate along one axis.
On a cyclic axis the minimum-image convention folds each offset into [-length/2, length/2).
Both tails must be folded: an offset of -9 on an axis of length 10 is a distance of 1, not 9.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
Number of nodes along the axis. |
required |
center
|
int
|
Coordinate of the winner along the axis. |
required |
cyclic
|
bool
|
Whether the axis wraps around. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Signed offsets, one per coordinate. |
Source code in src/python_som/_core/_neighborhood.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | |
axis_matrix ¶
axis_matrix(length, sigma, *, cyclic, profile)
Build H[a, c] = profile(a - c) for every pair of coordinates on one axis.
Contracting sums against one of these per axis evaluates Eq. (8) for every node at once. The
cyclic fold is the minimum-image convention of :func:axis_offsets, on pairwise offsets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
length
|
int
|
Number of nodes along the axis. |
required |
sigma
|
float
|
Neighborhood radius. |
required |
cyclic
|
bool
|
Whether the axis wraps around. |
required |
profile
|
AxisProfile
|
Per-axis factor to evaluate, from :data: |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Weights of shape |
Source code in src/python_som/_core/_neighborhood.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
gaussian_axis_profile ¶
gaussian_axis_profile(d, sigma)
Per-axis factor of the gaussian, exp(-d^2 / (2 sigma^2)).
Its product over the two axes is :func:gaussian, because the exponential factors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
NDArray[floating]
|
Offsets along one axis. |
required |
sigma
|
float
|
Neighborhood radius. Must be finite and positive. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Weights for those offsets. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the radius is not a finite positive number. |
Source code in src/python_som/_core/_neighborhood.py
245 246 247 248 249 250 251 252 253 254 255 256 | |
bubble_axis_profile ¶
bubble_axis_profile(d, sigma)
Per-axis factor of the bubble, the indicator |d| <= round(sigma).
Its product over the two axes is :func:bubble. It factors because the metric is Chebyshev; a
Euclidean disc would not.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
NDArray[floating]
|
Offsets along one axis. |
required |
sigma
|
float
|
Neighborhood radius, rounded to the nearest integer. Must be finite and non-negative. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Weights for those offsets. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the radius is not a finite non-negative number. |
Source code in src/python_som/_core/_neighborhood.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
resolve_axis_profile ¶
resolve_axis_profile(name)
Look up the per-axis factor of a neighborhood function by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the neighborhood function. |
required |
Returns:
| Type | Description |
|---|---|
AxisProfile
|
The corresponding axis profile. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the function has no axis profile, and so is not separable. |
Source code in src/python_som/_core/_neighborhood.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
squared_grid_distance ¶
squared_grid_distance(shape, c, cyclic)
Squared geometric distance from c to every node, i.e. sqdist(c, i) of Eq. (5).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
Grid
|
Shape of the network. |
required |
c
|
Coordinates
|
Coordinates of the winner. |
required |
cyclic
|
tuple[bool, bool]
|
Whether each axis wraps around. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Squared distances, with the shape of the network. |
Source code in src/python_som/_core/_neighborhood.py
81 82 83 84 85 86 87 88 89 90 91 92 93 | |
resolve ¶
resolve(name)
Look up a neighborhood function by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the neighborhood function. |
required |
Returns:
| Type | Description |
|---|---|
NeighborhoodFunction
|
The corresponding function. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the name is not recognised. |
Source code in src/python_som/_core/_neighborhood.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |
Decay functions¶
python_som._core._decay ¶
Decay functions for the learning rate and the neighborhood radius.
Each function maps an initial value, the current iteration and the total number of iterations to the
current value. They share one signature so that any of them, or a user-supplied equivalent, can be
passed as learning_rate_decay or neighborhood_radius_decay.
Kohonen (2013) Section 4.1 prescribes no particular form: "The true mathematical form of sigma(t) is not crucial, as long as its value is fairly large in the beginning of the process ... whereafter it is gradually reduced to a fraction of it in about 1000 steps".
DECAY_FUNCTIONS
module-attribute
¶
DECAY_FUNCTIONS = {
"asymptotic_decay": asymptotic_decay,
"linear_decay": linear_decay,
"exponential_decay": exponential_decay,
"inverse_decay": inverse_decay,
}
Decay functions by name, so a saved map can name the one it used.
Each key is the function's own name. These are written into artifacts, so they are public API from 0.4.0 and fixed at 1.0.0. A caller may pass any callable; what a name buys is restoring it from a file, and the loader says so when it cannot.
asymptotic_decay ¶
asymptotic_decay(x, t, max_t)
Decay x hyperbolically, reaching x / 2 at the halfway point.
Never reaches zero, which suits Kohonen's warning that the neighborhood radius should not decay all the way down (Section 4.2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Initial value. |
required |
t
|
int
|
Current iteration. |
required |
max_t
|
int
|
Total number of iterations. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Value of |
Source code in src/python_som/_core/_decay.py
29 30 31 32 33 34 35 36 37 38 39 40 | |
linear_decay ¶
linear_decay(x, t, max_t)
Decay x linearly to zero at t == max_t.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Initial value. |
required |
t
|
int
|
Current iteration. |
required |
max_t
|
int
|
Total number of iterations. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Value of |
Source code in src/python_som/_core/_decay.py
43 44 45 46 47 48 49 50 51 | |
exponential_decay ¶
exponential_decay(x, t, max_t, factor=2.0)
Decay x geometrically by factor / max_t per iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Initial value. |
required |
t
|
int
|
Current iteration. |
required |
max_t
|
int
|
Total number of iterations. |
required |
factor
|
float
|
Decay factor. Defaults to 2.0. |
2.0
|
Returns:
| Type | Description |
|---|---|
float
|
Value of |
Source code in src/python_som/_core/_decay.py
54 55 56 57 58 59 60 61 62 63 | |
inverse_decay ¶
inverse_decay(x, t, max_t)
Decay x inversely with t, scaled so the shape is independent of max_t.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Initial value. |
required |
t
|
int
|
Current iteration. |
required |
max_t
|
int
|
Total number of iterations. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Value of |
Source code in src/python_som/_core/_decay.py
66 67 68 69 70 71 72 73 74 | |
resolve_decay ¶
resolve_decay(name)
Look up a decay function by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the decay function. |
required |
Returns:
| Type | Description |
|---|---|
DecayFunction
|
The corresponding function. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the name is not recognised. |
Source code in src/python_som/_core/_decay.py
91 92 93 94 95 96 97 98 99 100 101 102 103 | |
Distance functions¶
python_som._core._distance ¶
Distance measures between input vectors and the models of the network.
DISTANCE_FUNCTIONS
module-attribute
¶
DISTANCE_FUNCTIONS = {
"euclidean_distance": euclidean_distance
}
Distance functions by name, so a saved map can name the one it used.
One entry, because one is what the package ships. The registry exists for the same reason
:data:DECAY_FUNCTIONS does -- a name is what makes a map reloadable -- and a dictionary with one
key is the honest shape for that rather than a special case in the loader.
euclidean_distance ¶
euclidean_distance(a, b)
Euclidean distance between the elements of the last axis of a and b.
Both arguments may be n-dimensional as long as their shapes broadcast. The result takes the
shape of the broadcast minus its last axis, so passing one input vector and the whole
(x, y, input_len) weight array yields an (x, y) map of distances.
Kohonen (2013) Section 3.3 notes that the Euclidean distance, applied to normalized data, "is already applicable to most practical studies".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
ArrayLike
|
Array-like of values. Must not be a scalar. |
required |
b
|
ArrayLike
|
Array-like of values. Must not be a scalar. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Distances between |
Source code in src/python_som/_core/_distance.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | |
resolve_distance ¶
resolve_distance(name)
Look up a distance function by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the distance function. |
required |
Returns:
| Type | Description |
|---|---|
DistanceFunction
|
The corresponding function. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the name is not recognised. |
Source code in src/python_som/_core/_distance.py
45 46 47 48 49 50 51 52 53 54 55 56 57 | |
Options¶
python_som._enums ¶
Names for the string-valued options, so a typo is a type error rather than a runtime one.
Every option is also accepted as a plain string, permanently. mode=TrainingMode.BATCH and
mode="batch" are interchangeable, compare equal, hash equal and serialise identically, because
each member is a str. 0.5.0 briefly deprecated the string form and 0.6.0 withdrew that; the
changelog has the reasoning.
The type-checking benefit comes from the Literal unions below rather than from the enums:
mode="bacth" is a type error while mode="batch" is not.
On the base class. enum.StrEnum needs Python 3.11 and this package supports 3.10, so
:class:_StrEnum reproduces it. A bare class X(str, Enum) is not equivalent: its str()
returns 'X.MEMBER' rather than the value, which would put the wrong text into any f-string or
filename built from a member.
TrainingMode ¶
Bases: _StrEnum
How :meth:~python_som.SOM.train presents the data.
RANDOM and SEQUENTIAL are the stepwise algorithm of Kohonen (2013) Eq. (3), differing
only in the order samples arrive. BATCH is Eq. (8), which updates every model concurrently.
Source code in src/python_som/_enums.py
45 46 47 48 49 50 51 52 53 54 | |
Neighborhood ¶
Bases: _StrEnum
Which neighborhood function spreads the winner's correction over the grid.
MEXICAN_HAT takes negative values and so cannot be used with :attr:TrainingMode.BATCH; see
:data:~python_som.SIGNED_NEIGHBORHOODS. The legacy spelling "mexicanhat" remains accepted
as a plain string and resolves to the same function, but is not given a member of its own: one
canonical spelling per option is the point of having an enum.
Source code in src/python_som/_enums.py
57 58 59 60 61 62 63 64 65 66 67 68 | |
WeightInit ¶
Bases: _StrEnum
How :meth:~python_som.SOM.weight_initialization seeds the models.
LINEAR and SAMPLE both need a dataset; RANDOM does not.
Source code in src/python_som/_enums.py
71 72 73 74 75 76 77 78 79 | |
SampleMode ¶
Bases: _StrEnum
Which distribution :attr:WeightInit.RANDOM draws from.
Source code in src/python_som/_enums.py
82 83 84 85 86 | |
Strategy protocols¶
python_som._core._protocols ¶
Contracts for the strategies a caller can replace.
Protocols rather than bare Callable aliases, so mypy checks the shape of the call against a
named contract instead of only the argument count.
Every parameter is positional-only. Without the /, a Protocol also requires the parameter
names to match, so a user's def my_decay(rate, step, total) would fail against a protocol that
named them differently.
Structural, so nothing inherits from them: this adds checking, not a requirement. See
:doc:/how-to/use-a-custom-strategy.
NeighborhoodFunction ¶
Bases: Protocol
Weights the winner's correction across the grid, as a function of grid distance.
Kohonen (2013) Eq. (5) requires this to depend on sqdist(c, i) alone, not on the two axis
offsets separately. A separable product satisfies the signature and is only correct for the
gaussian.
Source code in src/python_som/_core/_protocols.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
__call__ ¶
__call__(shape, c, sigma, cyclic)
Evaluate the neighborhood centred on c.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int]
|
Shape of the network. |
required |
c
|
tuple[int, int]
|
Coordinates of the winner. |
required |
sigma
|
float
|
Neighborhood radius. |
required |
cyclic
|
tuple[bool, bool]
|
Whether each axis wraps around. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Weights with the shape of the network. |
Source code in src/python_som/_core/_protocols.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | |
DecayFunction ¶
Bases: Protocol
Reduces a learning rate or a neighborhood radius as training proceeds.
Source code in src/python_som/_core/_protocols.py
60 61 62 63 64 65 66 67 68 69 70 71 72 | |
__call__ ¶
__call__(value, step, total)
Return the decayed value for this step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
float
|
The initial value being decayed. |
required |
step
|
int
|
Current iteration, counted from zero. |
required |
total
|
int
|
Total number of iterations. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The value to use at this step. |
Source code in src/python_som/_core/_protocols.py
64 65 66 67 68 69 70 71 72 | |
DistanceFunction ¶
Bases: Protocol
Dissimilarity between an input vector and one or many models.
Called both with a single model and with the whole (x, y, n_features) array, so an
implementation must broadcast over leading axes.
Source code in src/python_som/_core/_protocols.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | |
__call__ ¶
__call__(x, weights)
Return the distance from x to each of weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Any
|
Input vector. |
required |
weights
|
Any
|
One model, or an array of them. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Distances, with the leading shape of |
Source code in src/python_som/_core/_protocols.py
83 84 85 86 87 88 89 90 | |
BmuKernel ¶
Bases: Protocol
An accelerated best-matching-unit search, supplied from outside the core.
Optional. python_som._accelerate provides one with the fast extra installed; otherwise
the NumPy path in :func:~python_som._core._match.bmu_indices runs. Passed as an argument
rather than imported, so the core stays numpy-only.
Both arrays arrive already shifted by a common vector, which is what stops the expanded norm cancelling far from the origin. A kernel must not shift them again.
Source code in src/python_som/_core/_protocols.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
__call__ ¶
__call__(centred_data, centred_models, squared)
Return the index of the nearest model for each sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centred_data
|
NDArray[floating]
|
Samples, shifted, of shape |
required |
centred_models
|
NDArray[floating]
|
Models, shifted, of shape |
required |
squared
|
NDArray[floating]
|
Squared norm of each centred model. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[intp]
|
One flat node index per sample, ties going to the lowest index. |
Source code in src/python_som/_core/_protocols.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
AxisProfile ¶
Bases: Protocol
The per-axis factor of a separable neighborhood, over offsets along one axis.
Defined only where the factorisation is an identity, which is the gaussian and the bubble. Not a
general way to build a neighborhood: :class:NeighborhoodFunction remains the definition.
Source code in src/python_som/_core/_protocols.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
__call__ ¶
__call__(d, sigma)
Evaluate the factor over offsets along one axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
d
|
NDArray[floating]
|
Offsets along the axis. |
required |
sigma
|
float
|
Neighborhood radius. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Weights for those offsets. |
Source code in src/python_som/_core/_protocols.py
130 131 132 133 134 135 136 137 | |
KernelFunction ¶
Bases: Protocol
A neighborhood evaluated over every offset at once, independent of any particular winner.
.. deprecated:: 0.7.0
Batch training now contracts an :class:AxisProfile per axis, and nothing in the package
produces a kernel. Retained because it is part of the public surface; it will be removed at
1.0.0.
Source code in src/python_som/_core/_protocols.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
__call__ ¶
__call__(shape, sigma, cyclic)
Evaluate the neighborhood over every reachable offset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int]
|
Shape of the network. |
required |
sigma
|
float
|
Neighborhood radius. |
required |
cyclic
|
tuple[bool, bool]
|
Whether each axis wraps around. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[floating]
|
Weights of shape |
Source code in src/python_som/_core/_protocols.py
150 151 152 153 154 155 156 157 158 159 160 | |
Artifacts¶
python_som._artifact ¶
Saving and loading a trained map, with the provenance needed to defend a result.
Wilson et al., Best Practices for Scientific Computing: a result should carry its inputs, parameters and versions.
One file. A single .npz holding the models as an array and the metadata as a JSON string.
Provenance that can be separated from its artifact will be.
Callables are stored by name. A map holds four: the neighborhood, two decays and the distance.
Each is resolved on load through the registries in :mod:python_som._core, so a map built from the
shipped functions round-trips completely. One built with a caller's own function records the name
and refuses to load silently, naming the argument to pass it back through.
No pickle. allow_pickle=False on load, so a crafted file is refused rather than executed,
and no name from a file is ever imported. See :doc:/explanation/artifact-safety for the limits of
that guarantee.
SOMConfig
dataclass
¶
Everything needed to rebuild a map, with strategies recorded by name.
Frozen, because it describes a map that has already been trained: changing it after the fact would describe something that never ran.
Source code in src/python_som/_artifact.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 | |
unresolvable ¶
unresolvable()
Return the strategies whose names are not in a registry.
These are the ones a caller has to pass back in by hand, because a name is all that was saved and it does not correspond to anything this package can look up.
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Field name to the saved function name, for each strategy that cannot be restored. |
Source code in src/python_som/_artifact.py
112 113 114 115 116 117 118 119 120 121 122 123 124 | |
TrainingReport
dataclass
¶
What one training run did, recorded so a figure can be traced back to it.
wall_time_seconds is excluded from equality: it is provenance rather than an input, and two
identical runs should compare equal.
Source code in src/python_som/_artifact.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
ArtifactError ¶
Bases: ValueError
Raised when a file cannot be read as a python-som artifact.
A subclass of :class:ValueError so that except ValueError in existing code still catches
it, and distinct so that a caller can tell "this file is not ours" from "this map used a custom
function".
Source code in src/python_som/_artifact.py
74 75 76 77 78 79 80 | |