This high school project implements the K-means clustering algorithm in Python. K-means is a popular unsupervised learning algorithm used for clustering data points into K clusters based on their features.
- Initialization: K initial centroids are randomly selected from the data points.
- Assignment: Each data point is assigned to the nearest centroid based on Euclidean distance.
- Update: The centroids are recalculated as the mean of all data points assigned to each cluster.
- Repeat: Steps 2 and 3 are repeated until convergence, i.e., until centroids no longer change.
- assignPointsToCentroids: Function to assign data points to the nearest centroids based on Euclidean distance.
- recalcCentroids: Function to recalculate the centroids based on the assigned data points.
- KMeansND class: Class encapsulating the K-means algorithm with methods for computing centroids and providing a generator for animation.
- generateClusters: Function to generate random clusters of data points.
- initializeCentroids: Function to randomly initialize centroids from data points.
- animate: Function to draw each frame of the animation showing clustering process.
- setAxisLimits: Function to set axis limits for the plot to accommodate data points and centroids.
- Python 3.x
- NumPy
- Matplotlib
- Run the main script to generate clusters and visualize the K-means clustering process:
python kmeans.py-
Enter the desired number of clusters when prompted.
-
The script will generate random clusters of data points, perform K-means clustering, and animate the process.
The animation demonstrates the K-means clustering algorithm in action, showing how centroids move iteratively to optimize cluster assignments.
Contributions are welcome! Please fork the repository and submit a pull request with your improvements.
