quick_anomaly_detector.models.KMeansModel

class quick_anomaly_detector.models.KMeansModel(K=3)

The KMeansModel class is a Python implementation of the K-means clustering algorithm. Clustering is a type of unsupervised machine learning that partitions data into groups (clusters) based on similarity. The K-means algorithm aims to divide the data into K clusters, where each cluster is represented by its centroid.

To use the KMeansModel class, follow these steps:

  1. Create an instance of the class with an optional parameter K (number of clusters, default is 3).

from quick_anomaly_detector.models import KMeansModel

kmeans = KMeansModel(K=3)
  1. Train the model on your data using the train method.

centroids, labels = kmeans.train(X, max_iters=10)
  • X: Input data matrix.

  • max_iters: Maximum number of iterations for the K-means algorithm (default is 10).

  1. Access the resulting centroids and labels.

centroids = kmeans.centroids
labels = kmeans.labels
  1. Optionally, perform image compression using the image_compression method.

compressed_img = kmeans.image_compression(image_path, color_K=16, max_iters=10)
__init__(K=3)

Initialize a KMeansModel instance.

Parameters:

K (int): Number of centroids (clusters). Default is 3.

Methods

__init__([K])

Initialize a KMeansModel instance.

compute_centroids(X, idx, K)

Compute new centroids based on assigned examples.

find_closest_centroids(X, centroids)

Find the closest centroid for each example.

image_compression(image_path[, color_K, ...])

Perform image compression using K-means clustering.

kMeans_init_centroids(X, K)

Randomly initialize centroids.

train(X[, K, max_iters])

Train the KMeansModel.