IMPLEMENT CLUSTER -K_M

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:

  1. Data Preprocessing:

    • The dataset is loaded from Google Drive.
    • The Annual Income (k$) and Spending Score (1-100) columns are extracted for clustering.
    • The features are scaled using StandardScaler to standardize the data.
  2. 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.
  3. 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.
  4. 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