0
0
ML Pythonprogramming~5 mins

K-Means clustering in ML Python

Choose your learning style9 modes available
Introduction

K-Means clustering helps group similar things together automatically. It finds patterns without needing labels.

Grouping customers by buying habits to offer better deals.
Organizing photos by similar colors or shapes.
Segmenting areas on a map based on weather patterns.
Finding groups of similar documents or articles.
Detecting unusual data points by seeing which group they don't fit.
Syntax
ML Python
from sklearn.cluster import KMeans

kmeans = KMeans(n_clusters=k, random_state=seed)
kmeans.fit(data)
labels = kmeans.labels_
centers = kmeans.cluster_centers_

n_clusters is how many groups you want to find.

random_state helps get the same result every time you run it.

Examples
This finds 3 groups in the data with a fixed random seed.
ML Python
kmeans = KMeans(n_clusters=3, random_state=42)
kmeans.fit(data)
This gets the group number for each data point after fitting.
ML Python
labels = kmeans.labels_
This gets the center points of each group found.
ML Python
centers = kmeans.cluster_centers_
Sample Program

This program groups 6 points into 2 clusters. It prints which group each point belongs to and the center of each group.

ML Python
from sklearn.cluster import KMeans
import numpy as np

# Sample data: points in 2D space
data = np.array([
    [1, 2], [1, 4], [1, 0],
    [10, 2], [10, 4], [10, 0]
])

# Create KMeans with 2 clusters
kmeans = KMeans(n_clusters=2, random_state=0)

# Fit the model to data
kmeans.fit(data)

# Get cluster labels for each point
labels = kmeans.labels_

# Get cluster centers
centers = kmeans.cluster_centers_

print("Labels:", labels)
print("Centers:", centers)
OutputSuccess
Important Notes

K-Means works best when groups are round and similar size.

Choosing the right number of clusters (k) is important and can be tricky.

Random starting points can change results; setting random_state helps keep results consistent.

Summary

K-Means finds groups in data without labels.

You choose how many groups to find with n_clusters.

It returns group labels and center points for each group.