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 sepallengthcmDecimal(5,2) | 123 sepalwidthcmDecimal(5,2) | 123 petallengthcmDecimal(5,2) | 123 petalwidthcmDecimal(5,2) | Abc speciesVarchar(30) | 123 clusterInteger | |
|---|---|---|---|---|---|---|
| 1 | 5.1 | 3.5 | 1.4 | 0.2 | Iris-setosa | 0 |
| 2 | 4.9 | 3.0 | 1.4 | 0.2 | Iris-setosa | 0 |
| 3 | 4.7 | 3.2 | 1.3 | 0.2 | Iris-setosa | 0 |
| 4 | 4.6 | 3.1 | 1.5 | 0.2 | Iris-setosa | 0 |
| 5 | 5.0 | 3.6 | 1.4 | 0.2 | Iris-setosa | 0 |
| 6 | 5.4 | 3.9 | 1.7 | 0.4 | Iris-setosa | 0 |
| 7 | 4.6 | 3.4 | 1.4 | 0.3 | Iris-setosa | 0 |
| 8 | 5.0 | 3.4 | 1.5 | 0.2 | Iris-setosa | 0 |
| 9 | 4.4 | 2.9 | 1.4 | 0.2 | Iris-setosa | 0 |
| 10 | 4.9 | 3.1 | 1.5 | 0.1 | Iris-setosa | 0 |
| 11 | 5.4 | 3.7 | 1.5 | 0.2 | Iris-setosa | 0 |
| 12 | 4.8 | 3.4 | 1.6 | 0.2 | Iris-setosa | 0 |
| 13 | 4.8 | 3.0 | 1.4 | 0.1 | Iris-setosa | 0 |
| 14 | 4.3 | 3.0 | 1.1 | 0.1 | Iris-setosa | 0 |
| 15 | 5.8 | 4.0 | 1.2 | 0.2 | Iris-setosa | 0 |
| 16 | 5.7 | 4.4 | 1.5 | 0.4 | Iris-setosa | 0 |
| 17 | 5.4 | 3.9 | 1.3 | 0.4 | Iris-setosa | 0 |
| 18 | 5.1 | 3.5 | 1.4 | 0.3 | Iris-setosa | 0 |
| 19 | 5.7 | 3.8 | 1.7 | 0.3 | Iris-setosa | 0 |
| 20 | 5.1 | 3.8 | 1.5 | 0.3 | Iris-setosa | 0 |
This concludes this lesson on clustering models in vastorbit.
In the next lesson, we’ll go over Clustering