Loading...

Clustering

Clustering algorithms are used to segment data or to find anomalies. Generally speaking, clustering algorithms are sensitive to unnormalized data, so it’s important to properly prepare your data beforehand.

For example, if we consider the titanic dataset, the features fare and age don’t have values within the same interval; that is, fare can be much higher than the age. Applying a clustering algorithm to this kind of dataset would create misleading clusters.

To create a clustering model, we’ll start by importing the KMeans algorithm.

import vastorbit as vo
from vastorbit.machine_learning.vast import KMeans

Next, we’ll create a model object.

model = KMeans(n_clusters = 3)

Let’s use the iris dataset to fit our model.

from vastorbit.datasets import load_iris

iris = load_iris()

We can then fit the model with our data.

model.fit(iris, ["PetalLengthCm", "SepalLengthCm"])
model.plot()

While there aren’t any real metrics for evaluating unsupervised models, metrics used during computation can help us to understand the quality of the model. For example, a KMeans model with fewer clusters and when the KMeans score, Between-Cluster SS / Total SS is close to 1.

You can add the prediction to your VastFrame.

model.predict(iris, name = "cluster")
123
sepallengthcm
Decimal(5,2)
100%
123
sepalwidthcm
Decimal(5,2)
100%
123
petallengthcm
Decimal(5,2)
100%
123
petalwidthcm
Decimal(5,2)
100%
Abc
species
Varchar(30)
100%
123
cluster
Integer
100%
15.13.51.40.2Iris-setosa0
24.93.01.40.2Iris-setosa0
34.73.21.30.2Iris-setosa0
44.63.11.50.2Iris-setosa0
55.03.61.40.2Iris-setosa0
65.43.91.70.4Iris-setosa0
74.63.41.40.3Iris-setosa0
85.03.41.50.2Iris-setosa0
94.42.91.40.2Iris-setosa0
104.93.11.50.1Iris-setosa0
115.43.71.50.2Iris-setosa0
124.83.41.60.2Iris-setosa0
134.83.01.40.1Iris-setosa0
144.33.01.10.1Iris-setosa0
155.84.01.20.2Iris-setosa0
165.74.41.50.4Iris-setosa0
175.43.91.30.4Iris-setosa0
185.13.51.40.3Iris-setosa0
195.73.81.70.3Iris-setosa0
205.13.81.50.3Iris-setosa0

This concludes this lesson on clustering models in vastorbit.

In the next lesson, we’ll go over Clustering