0Pricing
Machine Learning Academy · บทเรียน

K-Means: จุดศูนย์กลาง การกำหนดกลุ่ม และขั้นตอนการปรับปรุง

ผู้เรียนจะติดตามการทำซ้ำสามรอบของ K-Means ด้วยมือ กำหนดจุดให้อยู่กับจุดศูนย์กลางที่ใกล้ที่สุด คำนวณจุดศูนย์กลางใหม่ และสังเกตการลู่เข้าบนแผนภาพกระจาย 2 มิติ

K-Means: จุดศูนย์กลาง การกำหนดกลุ่ม และขั้นตอนการปรับปรุง เป็นบทเรียน Machine Learning Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Machine Learning Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Machine Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

What Is K-Means Clustering?

K-Means is an unsupervised algorithm that partitions n data points into k non-overlapping clusters. Unlike supervised learning, there are no labels — the algorithm discovers structure purely from the feature values. K-Means is fast, scalable, and widely used for customer segmentation, image compression, and anomaly detection.

The Three-Step Algorithm

K-Means repeats three steps until convergence: 1) Initialise — randomly place k centroids in feature space. 2) Assignment — assign every point to the nearest centroid. 3) Update — move each centroid to the mean of its assigned points. The loop stops when assignments no longer change.

Computing Distance to Centroids

In each assignment step, the Euclidean distance from every point to every centroid is computed. A point is assigned to the centroid with the smallest distance. For a point x and centroid c, the squared distance is sum((x_i - c_i)^2). Using squared distance avoids the expensive square-root and gives the same ordering.

import numpy as np

def assign_clusters(X, centroids):
    # X: (n, d), centroids: (k, d)
    distances = np.linalg.norm(X[:, np.newaxis] - centroids, axis=2)  # (n, k)
    return np.argmin(distances, axis=1)  # label for each point

X = np.array([[1, 2], [3, 4], [5, 6], [8, 8]])
centroids = np.array([[2, 2], [7, 7]])
labels = assign_clusters(X, centroids)
print(labels)  # [0, 0, 0, 1]

The Update Step: Recomputing Centroids

After assignment, each centroid is relocated to the arithmetic mean of all points currently in its cluster. If a cluster becomes empty (no points assigned), the centroid is usually re-initialised randomly or removed. This mean-shift minimises the total within-cluster sum of squares (WCSS) — also called inertia.

import numpy as np

def update_centroids(X, labels, k):
    d = X.shape[1]
    new_centroids = np.zeros((k, d))
    for c in range(k):
        points = X[labels == c]
        if len(points) > 0:
            new_centroids[c] = points.mean(axis=0)
    return new_centroids

X = np.array([[1, 2], [3, 4], [5, 6], [8, 8]])
labels = np.array([0, 0, 0, 1])
print(update_centroids(X, labels, k=2))

Tracing Convergence by Hand

Consider four 1D points: 1, 2, 8, 9 and k=2. Init: centroids = [1, 8]. Iter 1 assignment: 1→C0, 2→C0, 8→C1, 9→C1. Iter 1 update: C0=1.5, C1=8.5. Iter 2 assignment: unchanged. Converged in 2 iterations! In higher dimensions convergence may take more steps, but the logic is identical.

Inertia: Measuring Cluster Compactness

Inertia (WCSS) is the sum of squared distances between each point and its cluster centroid. Lower inertia means tighter, more compact clusters. K-Means minimises inertia at each update step, but the algorithm is not guaranteed to find the global minimum — it can get stuck in local optima depending on initialisation.

from sklearn.cluster import KMeans
import numpy as np

X = np.array([[1, 2], [1, 4], [1, 0],
              [10, 2], [10, 4], [10, 0]])

km = KMeans(n_clusters=2, random_state=42)
km.fit(X)

print('Inertia:', km.inertia_)
print('Labels:', km.labels_)
print('Centroids:', km.cluster_centers_)

K-Means++ Initialisation

Random centroid initialisation often leads to slow convergence or poor local optima. K-Means++ (the scikit-learn default via init='k-means++') seeds centroids more intelligently: the first centroid is chosen randomly, and each subsequent centroid is selected with probability proportional to its squared distance from the nearest already-chosen centroid. This spreads starting points and consistently finds better solutions.

from sklearn.cluster import KMeans
import numpy as np

X = np.random.randn(300, 2)

# Default: k-means++ initialisation
km = KMeans(n_clusters=3, init='k-means++', n_init=10, random_state=0)
km.fit(X)
print('Inertia with k-means++:', round(km.inertia_, 2))

# Compare with random init
km_rand = KMeans(n_clusters=3, init='random', n_init=10, random_state=0)
km_rand.fit(X)
print('Inertia with random init:', round(km_rand.inertia_, 2))

Visualising Cluster Assignments

Plotting cluster assignments on a 2D scatter shows the Voronoi partition — the decision boundaries where each point's nearest centroid changes. Plotting centroids as large stars and colouring points by cluster label makes convergence intuitive. This visualisation also reveals when clusters overlap or have unequal sizes.

import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs

X, _ = make_blobs(n_samples=300, centers=3, cluster_std=0.6, random_state=0)
km = KMeans(n_clusters=3, random_state=0)
labels = km.fit_predict(X)

plt.scatter(X[:, 0], X[:, 1], c=labels, cmap='tab10', s=30)
plt.scatter(km.cluster_centers_[:, 0], km.cluster_centers_[:, 1],
            c='black', s=200, marker='*', label='Centroids')
plt.legend()
plt.title('K-Means Clusters')
plt.show()

Multiple Restarts and n_init

Because K-Means can converge to local optima, scikit-learn runs the algorithm n_init times with different random seeds and keeps the result with the lowest inertia. The default is n_init=10. For small datasets 10 is usually sufficient; for large or tricky datasets you may raise it to 20 or 50. Always check the final inertia against the best-run inertia to diagnose poor convergence.

from sklearn.cluster import KMeans
import numpy as np

X = np.random.randn(500, 5)

km = KMeans(n_clusters=4, n_init=20, random_state=0)
km.fit(X)

print('Best inertia over 20 runs:', round(km.inertia_, 2))
print('Number of iterations until convergence:', km.n_iter_)

Limitations of K-Means

K-Means has several well-known weaknesses: 1) Assumes spherical clusters — it struggles with elongated or crescent shapes. 2) Sensitive to outliers — a distant outlier pulls the centroid away from the true cluster mean. 3) Requires k upfront — you must know or estimate the number of clusters before fitting. 4) Feature scale matters — always standardise features before running K-Means so large-scale variables do not dominate distances.

Running K-Means with scikit-learn

In practice, using sklearn.cluster.KMeans is the standard approach. Key parameters: n_clusters (k), init (default 'k-means++'), n_init, max_iter (default 300), and random_state. After fitting, km.labels_ contains cluster assignments, km.cluster_centers_ holds centroid positions, and km.inertia_ reports WCSS.

from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_iris

X, _ = load_iris(return_X_y=True)

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

km = KMeans(n_clusters=3, random_state=42)
km.fit(X_scaled)

print('Cluster sizes:', {i: (km.labels_ == i).sum() for i in range(3)})
print('Inertia:', round(km.inertia_, 2))

Quick Check

Test your understanding of K-Means clustering concepts from this lesson.

Lesson Recap

In this lesson you learned: K-Means iterates assignment and update steps until cluster memberships stabilise, inertia (WCSS) measures compactness and is minimised by each update, and K-Means++ initialisation and multiple restarts help avoid poor local optima. Next up we explore how to choose the right value of k using the elbow method and silhouette score.

คำถามที่พบบ่อย

บทเรียน “K-Means: จุดศูนย์กลาง การกำหนดกลุ่ม และขั้นตอนการปรับปรุง” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “K-Means: จุดศูนย์กลาง การกำหนดกลุ่ม และขั้นตอนการปรับปรุง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Machine Learning Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Machine Learning Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “K-Means: จุดศูนย์กลาง การกำหนดกลุ่ม และขั้นตอนการปรับปรุง”

ผู้เรียนจะติดตามการทำซ้ำสามรอบของ K-Means ด้วยมือ กำหนดจุดให้อยู่กับจุดศูนย์กลางที่ใกล้ที่สุด คำนวณจุดศูนย์กลางใหม่ และสังเกตการลู่เข้าบนแผนภาพกระจาย 2 มิติ คุณปฏิบัติ Machine Learning Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Machine Learning Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Machine Learning Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “K-Means: จุดศูนย์กลาง การกำหนดกลุ่ม และขั้นตอนการปรับปรุง” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Machine Learning Academy นี้ได้ไหม

ได้ บทเรียน Machine Learning Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. K-Means: จุดศูนย์กลาง การกำหนดกลุ่ม และขั้นตอนการปรับปรุง
  2. การเลือกค่า K: วิธีข้อศอกและคะแนนซิลูเอตต์
  3. DBSCAN: จุดแกนกลาง จุดขอบ และสัญญาณรบกวน
  4. การจัดกลุ่มเพื่อแบ่งส่วนลูกค้า: ตัวอย่างตั้งแต่ต้นจนจบ
← กลับไปที่ Machine Learning Academy