vastorbit.machine_learning.memmodel.cluster.KPrototypes¶
- class vastorbit.machine_learning.memmodel.cluster.KPrototypes(clusters: Annotated[list | ndarray, 'Array Like Structure'], p: int = 2, gamma: float = 1.0, is_categorical: Annotated[list | ndarray, 'Array Like Structure'] | None = None)¶
InMemoryModelimplementation ofKPrototypes.- Parameters:
clusters (ArrayLike) –
listof the model’s cluster centers.p (int, optional) – The p corresponding to one of the
p-distances.gamma (float, optional) – Weighting factor for categorical columns. This determines relative importance of numerical and categorical attributes.
is_categorical (list | numpy.array, optional) – ArrayLike of
booleansto indicate whetherX[idx]is a categorical variable, whereTrueindicates categorical andFalsenumerical. If empty, all the variables are considered categorical.note:: (..) –
KPrototypesalgorithm allows you to use categorical variables directly without the need to encode them.
- 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 KPrototypes
A
KPrototypesmodel is defined by its cluster centroids. Optionally you can also providepvalue, gamma and provide information about categorical variables. In this example, we will use the following:clusters = [ [0.5, 'high'], [1, 'low'], [100, 'high'], ] p = 2 gamma = 1.0 is_categorical = [0, 1]
Let’s create a
KPrototypesmodel.model_kp = KPrototypes(clusters, p, gamma, is_categorical)
Create a dataset.
data = [[2, 'low']]
Making In-Memory Predictions
Use
predict()method to do predictions.model_kp.predict(data)[0]
Note
KPrototypesassigns a cluster id to identify each cluster. In this example, cluster with centroid[0.5, 'high']will haveid = 0, with centroid[1,'low']will haveid = 1and so on.predict()method returns the id of the predicted cluster.Use
predict_proba()method to compute the predicted probabilities for each cluster.model_kp.predict_proba(data)
Use
transform()method to compute the distance from each cluster.model_kp.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_kp.predict_sql(cnames)
Use
predict_proba_sql()method to get the SQL code needed to deploy the model that computes predicted probabilities.model_kp.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_kp.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'], p: int = 2, gamma: float = 1.0, is_categorical: Annotated[list | ndarray, 'Array Like Structure'] | None = None) None¶
Methods
__init__(clusters[, p, gamma, is_categorical])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