K-Means Clustering

1. Definition

K-Means Clustering is an Unsupervised Machine Learning Algorithm that groups similar data into K different clusters.

The value of K represents the number of groups (clusters) we want to create.

Unlike Supervised Learning, K-Means does not have a target (output) column. It finds similar records by measuring the distance between data points.

In simple words:

K-Means automatically divides similar data into different groups.

Example

Suppose a company has customer data.

CustomerAgeMonthly Income
C12225000
C22427000
C34570000
C44773000
C55076000

The company does not know which customers belong to which category.

K-Means may automatically create:

Cluster 0

  • Customer 1

  • Customer 2

(Low Income Customers)

Cluster 1

  • Customer 3

  • Customer 4

  • Customer 5

(High Income Customers)

The algorithm finds these groups automatically.


2. Purpose

The main purpose of K-Means is to group similar records together.

Instead of predicting an answer, it discovers natural groups in the data.

Businesses use K-Means to understand customers, products, and markets.

Examples:

  • Customer Segmentation

  • Product Grouping

  • Sales Analysis

  • Market Segmentation

  • Store Planning

  • Website User Analysis

  • Image Compression


3. When to Use

Use K-Means when:

  • There is no target column.

  • You want to divide data into groups.

  • Similar records should belong to the same cluster.

  • You know approximately how many groups (K) you want.

  • Your data is numerical.

Real-Life Applications

  • Customer Segmentation

  • Product Recommendation

  • Sales Analysis

  • Shopping Pattern Analysis

  • Employee Grouping

  • Hospital Patient Grouping

  • City Population Analysis

  • Marketing Campaign Planning


4. Complete Python Code (Using Pandas DataFrame)

import pandas as pd

from sklearn.cluster import KMeans

# ---------------------------------
# Create Dataset
# ---------------------------------

data = {
    "Age": [
        22,24,23,25,27,
        45,46,48,50,52,
        30,31,33,35,36,
        55,57,58,60,62
    ],

    "MonthlyIncome": [
        25000,27000,26000,28000,30000,
        70000,72000,73000,76000,78000,
        42000,44000,45000,47000,49000,
        85000,87000,89000,91000,93000
    ]
}

df = pd.DataFrame(data)

print("="*50)
print("DATASET")
print("="*50)
print(df)

# ---------------------------------
# Select Features
# ---------------------------------

X = df[
    [
        "Age",
        "MonthlyIncome"
    ]
]

# ---------------------------------
# Create Model
# ---------------------------------

model = KMeans(
    n_clusters=3,
    random_state=42,
    n_init=10
)

# ---------------------------------
# Train Model
# ---------------------------------

model.fit(X)

# ---------------------------------
# Cluster Prediction
# ---------------------------------

df["Cluster"] = model.labels_

print("\n")
print("="*50)
print("CUSTOMERS WITH CLUSTERS")
print("="*50)
print(df)

# ---------------------------------
# Cluster Centers
# ---------------------------------

print("\n")
print("="*50)
print("CLUSTER CENTERS")
print("="*50)

centers = pd.DataFrame(
    model.cluster_centers_,
    columns=X.columns
)

print(centers)

# ---------------------------------
# New Customer
# ---------------------------------

new_customer = [[34,46000]]

prediction = model.predict(new_customer)

print("\n")
print("="*50)
print("NEW CUSTOMER")
print("="*50)

print("Cluster Assigned :", prediction[0])

Sample Output

CUSTOMERS WITH CLUSTERS

    Age   MonthlyIncome   Cluster

0   22       25000          2
1   24       27000          2
2   23       26000          2
3   25       28000          2
4   27       30000          2

5   45       70000          1
6   46       72000          1
7   48       73000          1

8   50       76000          1

9   52       78000          1

10  30       42000          0
11  31       44000          0
12  33       45000          0
13  35       47000          0
14  36       49000          0

Cluster Centers

      Age    MonthlyIncome

0    33.0      45400

1    48.2      73800

2    24.2      27200

New Customer

Age = 34

Income = 46000

Cluster Assigned = 0

This means the new customer belongs to the same group as customers with medium income and medium age.


Summary

SectionDescription
DefinitionK-Means is an Unsupervised Learning algorithm that divides similar data into K clusters.
PurposeTo automatically group similar records into clusters.
When to UseUse when there is no target column and you want to discover groups in numerical data.
Code ExampleUses a Pandas DataFrame, KMeans, cluster assignment, cluster centers, and prediction for a new customer.


 

0 Comments