.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/05_customization/plot_02_distances.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_05_customization_plot_02_distances.py: ================== Distance Functions ================== .. GENERATED FROM PYTHON SOURCE LINES 6-23 .. code-block:: Python from typing import TYPE_CHECKING import numpy as np from sklearn.datasets import load_iris from sklearn.metrics import classification_report from sklearn.metrics.pairwise import pairwise_distances from sklvq.distances import DistanceBaseClass if TYPE_CHECKING: from sklvq.models import LVQBaseClass from sklvq import GLVQ data, labels = load_iris(return_X_y=True) .. GENERATED FROM PYTHON SOURCE LINES 24-30 The sklvq contains already a few distance function. Please see the API reference under Documentation. It has a very similar base class to that of the activation functions. However, the structure in which the distance and especially the gradient with respect to the different parameters need to be returned are important. Furthermore not every distance functions works with every algorithm. Below the `sklvq.distances.SquaredEuclidean`, which is suitable for the GLVQ algorithm. .. GENERATED FROM PYTHON SOURCE LINES 30-70 .. code-block:: Python class CustomSquaredEuclidean(DistanceBaseClass): # The distance implementations use the sklearn pairwise distance function. def __init__(self, **other_kwargs): self.metric_kwargs = {"metric": "euclidean", "squared": True} if other_kwargs is not None: self.metric_kwargs.update(other_kwargs) # The call function needs to return a matrix with the number of X points on the # rows and the columns the distance to the prototypes. def __call__(self, data: np.ndarray, model: "LVQBaseClass") -> np.ndarray: return pairwise_distances( data, model.prototypes_, **self.metric_kwargs, ) # The gradient is slightly more difficult as the gradient (with respect to 1 # prototype) needs to be provided in a vector the size of all the prototypes. # Hence, all values are zero except those of the prototype indicated by the index # i_prototype. In the case of GMLVQ and LGMVLQ distance functions als the gradient # of the omega matrix needs to be returned (in this same vector). See the API # reference under Documentation or github for other distance functions and their # implementation. def gradient(self, data: np.ndarray, model: "LVQBaseClass", i_prototype: int) -> np.ndarray: prototypes = model.get_model_params() (num_samples, num_features) = data.shape distance_gradient = np.zeros((num_samples, prototypes.size)) ip_start = i_prototype * num_features ip_end = ip_start + num_features distance_gradient[:, ip_start:ip_end] = -2 * (data - prototypes[i_prototype, :]) return distance_gradient .. GENERATED FROM PYTHON SOURCE LINES 71-74 The CustomSquaredEuclidean above, accompanied with some tests and documentation, would make a great addition to the sklvq package. However, it can also directly be passed to the algorithm. .. GENERATED FROM PYTHON SOURCE LINES 74-88 .. code-block:: Python model = GLVQ( distance_type=CustomSquaredEuclidean, activation_type="sigmoid", activation_params={"beta": 2}, ) model.fit(data, labels) # Predict the labels using the trained model predicted_labels = model.predict(data) # Print a classification report (sklearn) print(classification_report(labels, predicted_labels)) .. rst-class:: sphx-glr-script-out .. code-block:: none precision recall f1-score support 0 1.00 1.00 1.00 50 1 0.87 0.92 0.89 50 2 0.91 0.86 0.89 50 accuracy 0.93 150 macro avg 0.93 0.93 0.93 150 weighted avg 0.93 0.93 0.93 150 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.505 seconds) .. _sphx_glr_download_auto_examples_05_customization_plot_02_distances.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_distances.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_distances.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_distances.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_