v0.6.0
A map can now be used as a scikit-learn estimator, and the plain-string deprecation from 0.5.0 is
withdrawn. Nothing breaks and no numerical results change: train is untouched, both option spellings
work, and scikit-learn remains optional.
Added
-
python_som.sklearn.SOMEstimator, a scikit-learn estimator, behind a newsklearnextra:pip install "python-som[sklearn]"The methods on
SOMare enough when you call them yourself. They are not enough when scikit-learn
does the calling: since 1.7,Pipeline.predict,GridSearchCVandcross_val_scoreall require
__sklearn_tags__, which in practice means inheritingBaseEstimator. So the integration lives
in its own module and scikit-learn stays optional. Importingpython_somstill pulls in nothing
but NumPy, which a test asserts in a subprocess.input_lenis absent from its constructor, since scikit-learn infers the feature count fromX.
UnlikeSOM.fit, the estimator'sfitstarts over rather than continuing, becauseGridSearchCV
fits one cloned estimator fold after fold, and carrying weights across folds would leak one fold
into the next.All five integration points have tests that call the real library rather than asserting about it:
clone,Pipeline.fit,Pipeline.predict,GridSearchCV,cross_val_score. -
An estimator interface on
SOM:fit,transform,predict,fit_transform,score,
get_paramsandset_params, modelled onKMeans.trainremains the primary way to train and
is unchanged, so nothing existing is affected.predictreturns a flat node index rather than(row, column), because a 1-D label array is
what scorers,confusion_matrixandcross_val_scoreassume;
np.unravel_index(som.predict(X), som.get_shape())recovers the grid position, andwinner()
still returns coordinates.scoreis the negated quantization error, sinceGridSearchCV
maximises and without the sign a search would select the worst map.Fitted attributes follow the convention:
weights_forcluster_centers_,
quantization_error_forinertia_, plusn_features_in_.Two deliberate differences from scikit-learn, both documented:
fitcontinues rather than
resetting, because a SOM's models are its state; and the models exist before training, since
initialization is a separate step.set_paramschanges the rates, radii, decays and distance function. It refuses to change the grid
shape orinput_len, which would leave a map whose models no longer match its own description.
It is whatset_learning_rateandset_neighborhood_radiusbecome; both still work.These names alone are not enough for
Pipeline.predictorGridSearchCV, which since
scikit-learn 1.7 require__sklearn_tags__. A separatepython_som.sklearnadapter follows.
Changed
-
The plain-string deprecation introduced in 0.5.0 is withdrawn. Strings are permanently
supported, theDeprecationWarningis gone, and 1.0.0 will not remove them. If you migrated to
the enums while 0.5.0 was current, nothing you wrote breaks: the enums are staying too.0.5.0 was wrong, and the reason is worth stating rather than quietly reverting. Every comparable
library passes options as plain strings and none export enums: scikit-learn
(KMeans(init="k-means++")), numpy (np.pad(mode="constant")), scipy
(linkage(method="single")), and both SOM libraries, minisom and sompy. Removing the string form
would have made this the only library in its ecosystem to rejectmode="batch".The benefit originally claimed for enums was that a type checker catches typos. That is already
delivered by theLiteralunions added in 0.4.0, with strings still working:mode="bacth"is a
type error whilemode="batch"is not. The deprecation bought nothing that was not already had.The decision was made without checking what comparable libraries do. That check is now part of
planning any future API change.