Ex No Implement Clustering Algorithms
CLICK HERE : PROGRAM
Aim
To
write a python program to implement a k-means clustering algorithm.
Algorithm
1. Import necessary packages and libraries
2. Load the dataset
3. Load the algorithm k-Nearest Neighbor and train the algorithm
using the dataset
4. Predict the category of new data
Code Summary:
Data Preprocessing:
- The dataset is loaded from Google Drive.
- The
Annual Income (k$)
andSpending Score (1-100)
columns are extracted for clustering. - The features are scaled using
StandardScaler
to standardize the data.
WCSS Calculation (Elbow Method):
- You loop through different numbers of clusters (1 to 10) and calculate the Within-Cluster Sum of Squares (WCSS) for each iteration.
- The Elbow Method graph is plotted to visually find the optimal number of clusters.
KMeans Clustering:
- KMeans clustering is performed using the optimal number of clusters (5 in this case).
- The cluster labels are added to the dataset for visualization.
Visualization:
- Before Clustering: A scatter plot is created to show the raw data (Annual Income vs. Spending Score).
- After Clustering: A scatter plot is created to show the data points colored according to their cluster label.
- Clusters with Centroids: A final scatter plot shows the 5 clusters, colored differently, with the cluster centroids marked in black.
No comments:
Post a Comment