This project implements a K-means clustering algorithm with data visualization using Matplotlib and SciPy. The project includes the following key components:
-
Data Generation:
- The
generate_data
function creates random data points with specified features and value ranges.
- The
-
Elbow Method:
- The
elbow_method
function determines the optimal number of clusters by plotting the cost/inertia for different values of ( k ).
- The
-
K-means Clustering:
- The
kmeans
function performs the K-means clustering algorithm, iteratively updating centroids and assigning data points to clusters until convergence or a maximum number of iterations is reached.
- The
-
Visualization:
- The
animate_kmeans
function visualizes the clustering process by plotting the data points, centroids, and cluster boundaries at each iteration, saving the plots as images before updating.
- The
-
Execution:
- The script generates random data, runs the Elbow method to determine the optimal number of clusters, performs K-means clustering, and displays the clustering process using Matplotlib.
kmeans_visualizing_without_boundary.py
: Implements the K-means clustering algorithm with data visualization.kmeans_visualizing_with_boundary.py
: Implements the K-means clustering algorithm with data visualization and cluster boundary.
-
Generate Data:
- Use the
generate_data
function to create random data points.
- Use the
-
Determine Optimal Clusters:
- Use the
elbow_method
function to determine the optimal number of clusters.
- Use the
-
Run K-means Clustering:
- Use the
kmeans
function to perform the clustering.
- Use the
-
Visualize Clustering Process:
- Use the
animate_kmeans
function to visualize and save the clustering process.
- Use the
- Python 3.x
- Matplotlib
- SciPy
Install the required packages using pip:
pip install matplotlib scipy
- Generate random data.
- Determine the optimal number of clusters using the Elbow method.
- Perform K-means clustering.
- Visualize the clustering process.
python3 kmean_visualizing_without_boundary.py
python3 kmean_visualilzing_with_boundary.py
This project is licensed under the MIT License.