dasf.ml.cluster.som =================== .. py:module:: dasf.ml.cluster.som .. autoapi-nested-parse:: Kohonen's Self-Organized Map (SOM) algorithm module. Classes ------- .. autoapisummary:: dasf.ml.cluster.som.SOM Module Contents --------------- .. py:class:: SOM(x, y, input_len, num_epochs=100, sigma=0, sigmaN=1, learning_rate=0.5, learning_rateN=0.01, decay_function='exponential', neighborhood_function='gaussian', std_coeff=0.5, topology='rectangular', activation_distance='euclidean', random_seed=None, n_parallel=0, compact_support=False, **kwargs) Bases: :py:obj:`dasf.ml.cluster.classifier.ClusterClassifier` Initializes a Self Organizing Maps. A rule of thumb to set the size of the grid for a dimensionality reduction task is that it should contain 5*sqrt(N) neurons where N is the number of samples in the dataset to analyze. E.g. if your dataset has 150 samples, 5*sqrt(150) = 61.23 hence a map 8-by-8 should perform well. Parameters ---------- x : int x dimension of the SOM. y : int y dimension of the SOM. input_len : int Number of the elements of the vectors in input. sigma : float, default=min(x,y)/2 Spread of the neighborhood function, needs to be adequate to the dimensions of the map. sigmaN : float, default=0.01 Spread of the neighborhood function at last iteration. learning_rate : float, default=0.5 initial learning rate. learning_rateN : float, default=0.01 final learning rate decay_function : string, default='exponential' Function that reduces learning_rate and sigma at each iteration. Possible values: 'exponential', 'linear', 'aymptotic' neighborhood_function : string, default='gaussian' Function that weights the neighborhood of a position in the map. Possible values: 'gaussian', 'mexican_hat', 'bubble', 'triangle' topology : string, default='rectangular' Topology of the map. Possible values: 'rectangular', 'hexagonal' activation_distance : string, default='euclidean' Distance used to activate the map. Possible values: 'euclidean', 'cosine', 'manhattan' random_seed : int, default=None Random seed to use. n_parallel : uint, default=#max_CUDA_threads or 500*#CPUcores Number of samples to be processed at a time. Setting a too low value may drastically lower performance due to under-utilization, setting a too high value increases memory usage without granting any significant performance benefit. xp : numpy or cupy, default=cupy if can be imported else numpy Use numpy (CPU) or cupy (GPU) for computations. std_coeff: float, default=0.5 Used to calculate gausssian exponent denominator: d = 2*std_coeff**2*sigma**2 compact_support: bool, default=False Cut the neighbor function to 0 beyond neighbor radius sigma Examples -------- >>> from dasf.ml.cluster import SOM >>> import numpy as np >>> X = np.array([[1, 1], [2, 1], [1, 0], ... [4, 7], [3, 5], [3, 6]]) >>> som = SOM(x=3, y=2, input_len=2, ... num_epochs=100).fit(X) >>> som SOM(x=3, y=2, input_len=2, num_epochs=100) Constructor of the class SOM. .. py:attribute:: x .. py:attribute:: y .. py:attribute:: input_len .. py:attribute:: num_epochs .. py:attribute:: sigma .. py:attribute:: sigmaN .. py:attribute:: learning_rate .. py:attribute:: learning_rateN .. py:attribute:: decay_function .. py:attribute:: neighborhood_function .. py:attribute:: std_coeff .. py:attribute:: topology .. py:attribute:: activation_distance .. py:attribute:: random_seed .. py:attribute:: n_parallel .. py:attribute:: compact_support .. py:attribute:: __som_cpu .. py:attribute:: __som_mcpu .. py:method:: _lazy_fit_cpu(X, y=None, sample_weight=None) Fit SOM method using Dask with CPUs only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). sample_weight : array-like of shape (n_samples,), default=None This is just a placeholder to keep the compatibility with other fit methods. This is not used by SOM. Returns ------- self : object Returns a fitted instance of self. .. py:method:: _lazy_fit_gpu(X, y=None, sample_weight=None) Fit SOM method using Dask with GPUs only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). sample_weight : array-like of shape (n_samples,), default=None This is just a placeholder to keep the compatibility with other fit methods. This is not used by SOM. Returns ------- self : object Returns a fitted instance of self. .. py:method:: _fit_cpu(X, y=None, sample_weight=None) Fit SOM method using CPU only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). sample_weight : array-like of shape (n_samples,), default=None This is just a placeholder to keep the compatibility with other fit methods. This is not used by SOM. Returns ------- self : object Returns a fitted instance of self. .. py:method:: _fit_gpu(X, y=None, sample_weight=None) Fit SOM method using GPU only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). sample_weight : array-like of shape (n_samples,), default=None This is just a placeholder to keep the compatibility with other fit methods. This is not used by SOM. Returns ------- self : object Returns a fitted instance of self. .. py:method:: _lazy_fit_predict_cpu(X, y=None, sample_weight=None) Fit SOM and select the winner neurons for the input using Dask with CPUs only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). y : {array-like, sparse matrix} of shape (n_samples). This is just a placeholder to keep the compatibility with other fit_predict methods. SOM does not use labels to verify the input. sample_weight : array-like of shape (n_samples,), default=None This is just a placeholder to keep the compatibility with other fit_predict methods. This is not used by SOM. Returns ------- self : object Returns a fitted instance of self. .. py:method:: _lazy_fit_predict_gpu(X, y=None, sample_weight=None) Fit SOM and select the winner neurons for the input using Dask with GPUs only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). y : {array-like, sparse matrix} of shape (n_samples). This is just a placeholder to keep the compatibility with other fit_predict methods. SOM does not use labels to verify the input. sample_weight : array-like of shape (n_samples,), default=None This is just a placeholder to keep the compatibility with other fit_predict methods. This is not used by SOM. Returns ------- self : object Returns a fitted instance of self. .. py:method:: _fit_predict_cpu(X, y=None, sample_weight=None) Fit SOM and select the winner neurons for the input using CPU only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). y : {array-like, sparse matrix} of shape (n_samples). This is just a placeholder to keep the compatibility with other fit_predict methods. SOM does not use labels to verify the input. sample_weight : array-like of shape (n_samples,), default=None This is just a placeholder to keep the compatibility with other fit_predict methods. This is not used by SOM. Returns ------- self : object Returns a fitted instance of self. .. py:method:: _fit_predict_gpu(X, y=None, sample_weight=None) Fit SOM and select the winner neurons for the input using GPU only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). y : {array-like, sparse matrix} of shape (n_samples). This is just a placeholder to keep the compatibility with other fit_predict methods. SOM does not use labels to verify the input. sample_weight : array-like of shape (n_samples,), default=None This is just a placeholder to keep the compatibility with other fit_predict methods. This is not used by SOM. Returns ------- self : object Returns a fitted instance of self. .. py:method:: _lazy_predict_cpu(X, sample_weight=None) Predict the input using a fitted SOM using Dask with CPUs only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). sample_weight : array-like of shape (n_samples,), default=None This is just a placeholder to keep the compatibility with other fit methods. This is not used by SOM. Returns ------- labels : ndarray of shape (n_samples,) Cluster labels. Noisy samples are given the label -1. .. py:method:: _lazy_predict_gpu(X, sample_weight=None) Predict the input using a fitted SOM using Dask with GPUs only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). sample_weight : array-like of shape (n_samples,), default=None This is just a placeholder to keep the compatibility with other fit methods. This is not used by SOM. Returns ------- labels : ndarray of shape (n_samples,) Cluster labels. Noisy samples are given the label -1. .. py:method:: _predict_cpu(X, sample_weight=None) Predict the input using a fitted SOM using CPU only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). sample_weight : array-like of shape (n_samples,), default=None This is just a placeholder to keep the compatibility with other fit methods. This is not used by SOM. Returns ------- labels : ndarray of shape (n_samples,) Cluster labels. Noisy samples are given the label -1. .. py:method:: _predict_gpu(X, sample_weight=None) Predict the input using a fitted SOM using GPU only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). sample_weight : array-like of shape (n_samples,), default=None This is just a placeholder to keep the compatibility with other fit methods. This is not used by SOM. Returns ------- labels : ndarray of shape (n_samples,) Cluster labels. Noisy samples are given the label -1. .. py:method:: _lazy_quantization_error_cpu(X) Returns the quantization error computed as the average distance between each input sample and its best matching unit using Dask with CPUs only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). Returns ------- error : float The quantization error of the trained SOM. .. py:method:: _lazy_quantization_error_gpu(X) Returns the quantization error computed as the average distance between each input sample and its best matching unit using Dask with GPUs only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). Returns ------- error : float The quantization error of the trained SOM. .. py:method:: _quantization_error_cpu(X) Returns the quantization error computed as the average distance between each input sample and its best matching unit using CPU only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). Returns ------- error : float The quantization error of the trained SOM. .. py:method:: _quantization_error_gpu(X) Returns the quantization error computed as the average distance between each input sample and its best matching unit using GPU only. Parameters ---------- X : {array-like, sparse matrix} of shape (n_samples, n_features). Returns ------- error : float The quantization error of the trained SOM. .. py:method:: quantization_error(X) Generic quantization_error funtion according executor (for SOM method only).