sklvq.models.GLVQ¶
-
class
sklvq.models.GLVQ(distance_type: Union[str, type] = 'squared-euclidean', distance_params: Optional[dict] = None, activation_type: Union[str, type] = 'sigmoid', activation_params: Optional[dict] = None, discriminant_type: Union[str, type] = 'relative-distance', discriminant_params: Optional[dict] = None, solver_type: Union[str, type] = 'steepest-gradient-descent', solver_params: Optional[dict] = None, prototype_init: str = 'class-conditional-mean', prototype_n_per_class: Union[int, numpy.ndarray] = 1, random_state: Optional[Union[int, numpy.random.mtrand.RandomState]] = None, force_all_finite: Union[str, bool] = True)[source]¶ Generalized Learning Vector Quantization
This model uses the
sklvq.objectives.GeneralizedLearningObjectiveas its objective function [1].- Parameters
- distance_type{“squared-euclidean”, “euclidean”} or Class, default=”squared-euclidean”
The distance function. Can be one from the following list or a custom class:
- “squared-euclidean”
- “euclidean”
- distance_paramsdict, optional, default=None
Parameters passed to init of distance class.
- activation_type{“identity”, “sigmoid”, “soft+”, “swish”} or Class, default=”sigmoid”
The activation function used in the objective function. Can be any of the activation function in the list or custom class.
- “identity”
- “sigmoid”
- “soft+”
- “swish”
- activation_paramsdict, default=None
Parameters passed to init of activation function. See the documentation of the activation functions for parameters and defaults.
- discriminant_type“relative-distance” or Class
The discriminant function. Note that different discriminant type may require to rewrite the
decision_functionandpredict_probamethods.- “relative-distance”
- discriminant_paramsdict, default=None
Parameters passed to init of discriminant callable. See the documentation of the discriminant functions for parameters and defaults.
- solver_type{“sgd”, “wgd”, “adam”, “lbfgs”, “bfgs”},
The solver used for optimization
- “sgd” or “steepest-gradient-descent”
- “wgd” or “waypoint-gradient-descent”
- “adam” or “adaptive-moment-estimation”
- “bfgs” or “broyden-fletcher-goldfarb-shanno”
- “lbfgs” or “limited-memory-bfgs”
See
skvlq.solvers.LimitedMemoryBfgs
- solver_paramsdict, default=None
Parameters passed to init of solvers. See the documentation of the solvers relevant parameters and defaults.
- prototype_init: “class-conditional-mean” or ndarray, default=”class-conditional-mean”
Default will initiate the prototypes to the class conditional mean with a small random offset. Custom numpy array can be passed to change the initial positions of the prototypes.
- prototype_n_per_class: int or np.ndarray, optional, default=1
Default will generate single prototype per class. In the case of unequal number of prototypes per class is needed, provide this as np.ndarray. For example, prototype_n_per_class = np.array([1, 6, 3]) this will result in one prototype for the first class, six for the second, and three for the third. Note that the order needs to be the same as the on in the classes_ attribute, which is equal to calling np.unique(labels).
- random_stateint, RandomState instance, default=None
Set the random number generation for reproducibility purposes. Used in random offset of prototypes and shuffling of the data in the solvers.
- force_all_finite{True, “allow-nan”}, default=True
Whether to raise an error on np.inf, np.nan, pd.NA in array. The possibilities are:
True: Force all values of array to be finite.
“allow-nan”: accepts only np.nan and pd.NA values in array. Values cannot be infinite.
References
[1] Sato, A., and Yamada, K. (1996) “Generalized Learning Vector Quantization.” Advances in Neural Network Information Processing Systems, 423–429, 1996.
- Attributes
- classes_ndarray of shape (n_classes,)
The original and unique labels found in the data.
- prototypes_ndarray of shape (n_protoypes, n_features)
Positions of the prototypes after fit(X, labels) has been called.
- prototypes_labels_ndarray of shape (n_prototypes)
Labels for each prototypes. Labels are indexes to
classes_
-
__init__(distance_type: Union[str, type] = 'squared-euclidean', distance_params: Optional[dict] = None, activation_type: Union[str, type] = 'sigmoid', activation_params: Optional[dict] = None, discriminant_type: Union[str, type] = 'relative-distance', discriminant_params: Optional[dict] = None, solver_type: Union[str, type] = 'steepest-gradient-descent', solver_params: Optional[dict] = None, prototype_init: str = 'class-conditional-mean', prototype_n_per_class: Union[int, numpy.ndarray] = 1, random_state: Optional[Union[int, numpy.random.mtrand.RandomState]] = None, force_all_finite: Union[str, bool] = True)[source]¶ Initialize self. See help(type(self)) for accurate signature.
-
add_partial_gradient(gradient, partial_gradient, i_prototype) → None[source]¶ Adds the partial gradient to the correct part of the gradient, which depends on
i_prototype.- Parameters
- gradientndarray
Same shape as the
get_variables()would return.- partial_gradientndarray
1d array containing the partial gradient.
- i_prototypeint
The index of the prototype to which the partial gradient was computed.
-
decision_function(X: numpy.ndarray)¶ Evaluates the decision function for the samples in X. Shape for binary class is (n_observations,) with the decision values for the “greater” class. In the multiclass case it returns decision values for each class and therefore has the shape (n_observations, n_classes).
- Parameters
- Xndarray
The data.
- Returns
- decision_valuesndarray
Binary case shape is (n_observations,) and the multiclass case (n_observations, n_classes)
-
fit(X: numpy.ndarray, y: numpy.ndarray)¶ - Fit function that provides the general implementation of the LVQ algorithms. It checks the data, calls
before_fit method, calls the solve method of the solver, and the after_fit method.
- Parameters
- Xndarray of shape (number of observations, number of dimensions)
- yndarray of size (number of observations)
- Returns
- self
The trained model
-
get_model_params() → numpy.ndarray[source]¶ Returns a view of all model parameters, which are only the prototypes.
- Returns
- ndarray
Returns a view of the prototypes as ndarray.
-
get_params(deep=True)¶ Get parameters for this estimator.
- Parameters
- deepbool, default=True
If True, will return the parameters for this estimator and contained subobjects that are estimators.
- Returns
- paramsdict
Parameter names mapped to their values.
-
get_prototypes() → numpy.ndarray¶ Return a view into
self._variablesof the the shape of the prototypes (n_prototypes, n_features). At the moment only consistency function, does not actually create the shape and only works afterself.prototypes_has been set.- Returns
- prototypesndarray of shape (n_prototypes, n_features)
View into
self._variableswith shape specified above.
-
get_variables() → numpy.ndarray¶ Returns the
self._variablesarray that owns the memory allocated for the model parameters.- Returns
- _variablesndarray
returns the model’s _variables array.
-
mul_step_size(step_size: Union[int, float], gradient: numpy.ndarray) → None[source]¶ As GLVQ only has prototypes that are optimized the step_size should be a single float and can just be used to multiply the gradient inplace.
- Parameters
- step_sizefloat or ndarray
The scalar or list of values containing the step sizes.
- gradientndarray
Same shape as the
get_variables()would return.
-
normalize_variables(var_buffer: numpy.ndarray) → None[source]¶ Modifies the var_buffer as if it was the variables array provided by
get_variables(). As variables only contain prototypes it will now contain the normalized prototypes.- Parameters
- var_bufferndarray
Array with the same size as the model’s variables array as returned by
get_variables().
- Returns
- ndarray
Same shape and size as input, but normalized.
-
predict(X: numpy.ndarray)¶ Predict function
The decision is made for the label of the prototype with the minimum decision value, as provided by the
decision_function().- Parameters
- Xndarray
The data.
- Returns
- ndarray of shape (n_observations)
Returns the predicted labels.
-
predict_proba(X: numpy.ndarray)¶ - Parameters
- Xndarray
The data.
- Returns
- confidence_scoresndarray of shape (n_observations, n_classes)
-
score(X, y, sample_weight=None)¶ Return the mean accuracy on the given test data and labels.
In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.
- Parameters
- Xarray-like of shape (n_samples, n_features)
Test samples.
- yarray-like of shape (n_samples,) or (n_samples, n_outputs)
True labels for X.
- sample_weightarray-like of shape (n_samples,), default=None
Sample weights.
- Returns
- scorefloat
Mean accuracy of
self.predict(X)wrt. y.
-
set_model_params(new_model_params: numpy.ndarray) → None[source]¶ Changes the model’s internal parameters. Copies the values of model_params into
self.prototypes_therefor updating theself.variables_array.- Parameters
- new_model_paramsndarray of shape (n_prototypes, n_features)
In the case the prototypes.
-
set_params(**params)¶ Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline). The latter have parameters of the form<component>__<parameter>so that it’s possible to update each component of a nested object.- Parameters
- **paramsdict
Estimator parameters.
- Returns
- selfestimator instance
Estimator instance.
-
set_prototypes(new_prototypes: numpy.ndarray) → None¶ Accepts a new_prototypes array with the same shape as
self.prototypes_and overwrites theself._variablesarray by copying the values of the new_prototypes.- Parameters
- new_prototypesndarray of shape (n_prototypes, n_features)
The new prototypes the model should store.
-
set_variables(new_variables: numpy.ndarray) → None¶ Modifies the
self._variablesby copying the values ofnew_variablesinto the memory ofself._variables.- Parameters
- new_variablesndarray
1d numpy array that contains all the model parameters in continuous memory
-
to_model_params_view(var_buffer: numpy.ndarray) → numpy.ndarray[source]¶ Should create a view of the variables array in prototype shape.
- Parameters
- var_bufferndarray
Array with the same size as the model’s variables array as returned by
get_variables().
- Returns
- ndarray
Returns the prototypes as ndarray.
-
to_prototypes_view(var_buffer: numpy.ndarray) → numpy.ndarray[source]¶ Returns the prototypes into the provided var_buffer. I.e., it selects/views the appropriate part of memory and reshapes it.
- Parameters
- var_bufferndarray
Array with the same size as the model’s variables array as returned by
get_variables().
- Returns
- ndarray of shape (n_prototypes, n_features)
Prototype view into the var_buffer.