vastorbit.machine_learning.memmodel.cluster.NearestCentroid¶
- class vastorbit.machine_learning.memmodel.cluster.NearestCentroid(clusters: Annotated[list | ndarray, 'Array Like Structure'], classes: Annotated[list | ndarray, 'Array Like Structure'], p: int = 2)¶
InMemoryModelimplementation ofNearestCentroidalgorithm.- Parameters:
clusters (ArrayLike) –
listof the model’s cluster centers.classes (ArrayLike) – Names of the classes.
p (int, optional) – The
pcorresponding to one of thep-distances.
- Variables:
input (Attributes are identical to the)
underscore (parameters, followed by an)
('_').
Examples
Initalization
Import the required module.
from vastorbit.machine_learning.memmodel.cluster import NearestCentroid
A
NearestCentroidmodel is defined by itsclusterscentroids,classesand thepvalue. In this example, we will use the following:clusters = [[0.5, 0.6], [1, 2], [100, 200]] p = 2 classes = ['class_a', 'class_b', 'class_c']
Let’s create a
NearestCentroidmodel.model_nc = NearestCentroid(clusters, classes, p)
Create a dataset.
data = [[2, 3]]
Making In-Memory Predictions
Use
predict()method to do predictions.model_nc.predict(data)[0]
Use
predict_proba()method to compute the predicted probabilities for each cluster.model_nc.predict_proba(data)
Use
transform()method to compute the distance from each cluster.model_nc.transform(data)
Deploy SQL Code
Let’s use the following column names:
cnames = ['col1', 'col2']
Use
predict_sql()method to get the SQL code needed to deploy the model using its attributes.model_nc.predict_sql(cnames)
Use
predict_proba_sql()method to get the SQL code needed to deploy the model that computes predicted probabilities.model_nc.predict_proba_sql(cnames)
Use
transform_sql()method to get the SQL code needed to deploy the model that computes distance from each cluster.model_nc.transform_sql(cnames)
Hint
This object can be pickled and used in any in-memory environment, just like scikit-learn models.
- __init__(clusters: Annotated[list | ndarray, 'Array Like Structure'], classes: Annotated[list | ndarray, 'Array Like Structure'], p: int = 2) None¶
Methods
__init__(clusters, classes[, p])Returns the model attributes.
predict(X)Predicts clusters using the input matrix.
Predicts the probability of each input to belong to the model clusters.
Returns the SQL code needed to deploy the model probabilities.
predict_sql(X)Returns the SQL code needed to deploy the model using its attributes.
set_attributes(**kwargs)Sets the model attributes.
transform(X)Transforms and returns the distance to each cluster.
Transforms and returns the SQL distance to each cluster.
Attributes
Must be overridden in child class