vastorbit.machine_learning.memmodel.cluster.BisectingKMeans¶
- class vastorbit.machine_learning.memmodel.cluster.BisectingKMeans(clusters: Annotated[list | ndarray, 'Array Like Structure'], children_left: Annotated[list | ndarray, 'Array Like Structure'], children_right: Annotated[list | ndarray, 'Array Like Structure'], cluster_size: Annotated[list | ndarray, 'Array Like Structure'] | None = None, cluster_score: Annotated[list | ndarray, 'Array Like Structure'] | None = None, p: int = 2)¶
InMemoryModelimplementation ofBisectingKMeans.- Parameters:
clusters (ArrayLike) –
listof the model’s cluster centers.children_left (ArrayLike) – A list of node IDs, where
children_left[i]is the node ID of the left child of node i.children_right (ArrayLike) – A list of node IDs, where
children_right[i]is the node ID of the right child of node i.cluster_size (ArrayLike) – A list of sizes, where
cluster_size[i]is the number of elements in node i.cluster_score (ArrayLike) – A list of scores, where
cluster_score[i]is the score for internal node i. The score is the ratio between the within -cluster sum of squares of the node and the total within-cluster sum of squares.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 BisectingKMeans
A
BisectingKMeansmodel is defined by itsclusterscentroids, left and right child node id’s of given node. In this example, we will use the following:clusters = [ [0.5, 0.6], [1, 2], [100, 200], [10, 700], [-100, -200], ] children_left = [1, 3, None, None, None] children_right = [2, 4, None, None, None]
Let’s create a
BisectingKMeansmodel.model_bkm = BisectingKMeans(clusters, children_left, children_right)
Create a dataset.
data = [[2, 3]]
Making In-Memory Predictions
Use
predict()method to do predictions.model_bkm.predict(data)[0]
Use
predict_proba()method to compute the predicted probabilities for each cluster.model_bkm.predict_proba(data)
Use
transform()method to compute the distance from each cluster.model_bkm.transform(data)
Use
to_graphviz()method to generate code for a Graphviz tree.model_bkm.to_graphviz()
Use
plot_tree()method to draw the input tree.model_bkm.plot_tree()
Note
plot_tree()requires the Graphviz module.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_bkm.predict_sql(cnames)
Use
predict_proba_sql()method to get the SQL code needed to deploy the model that computes predicted probabilities.model_bkm.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_bkm.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'], children_left: Annotated[list | ndarray, 'Array Like Structure'], children_right: Annotated[list | ndarray, 'Array Like Structure'], cluster_size: Annotated[list | ndarray, 'Array Like Structure'] | None = None, cluster_score: Annotated[list | ndarray, 'Array Like Structure'] | None = None, p: int = 2) None¶
Methods
__init__(clusters, children_left, children_right)Returns the model attributes.
plot_tree([pic_path])Draws the input tree.
predict(X)Predicts using the
BisectingKMeansmodel.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
BisectingKMeansmodel using its attributes.set_attributes(**kwargs)Sets the model attributes.
to_graphviz([round_score, percent, ...])Returns the code for a Graphviz tree.
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