تجميع K-Means
تعرّف على تجميع K-Means، وهي خوارزمية تعلم غير خاضعة للإشراف رئيسية لتجميع البيانات في مجموعات. استكشف عمليتها وتطبيقاتها ومقارناتها!
K-Means Clustering is a fundamental and widely used algorithm in the field of
unsupervised learning designed to uncover
hidden structures within unlabeled data. Its primary
objective is to partition a dataset into distinct subgroups, known as clusters, such that data points within the same
group are as similar as possible, while those in different groups are distinct. As a cornerstone of
data mining and exploratory analysis, K-Means empowers
data scientists to automatically organize complex information into manageable categories without the need for
predefined labels or human supervision.
كيف تعمل الخوارزمية
The operation of K-Means is iterative and relies on distance metrics to determine the optimal grouping of the
training data. The algorithm operates by organizing
items into K clusters, where each item belongs to the cluster with the nearest mean, or centroid. This
process minimizes the variance within each group. The workflow
generally follows these steps:
-
التهيئة: تختار الخوارزمية K نقاط أولية كنقاط مركزية. يمكن اختيار هذه النقاط
عشوائياً أو عبر طرق محسّنة مثل k-means++ لتسريع
التقارب.
-
المهمة: يتم تعيين كل نقطة بيانات في مجموعة البيانات إلى أقرب مركز ثقل بناءً على مقياس مسافة محدد
، وغالبًا ما يكون
المسافة الأوقليدية.
-
تحديث: يتم إعادة حساب المراكز عن طريق أخذ متوسط (متوسط) جميع نقاط البيانات المخصصة
لتلك المجموعة.
-
التكرار: يتم تكرار الخطوتين 2 و 3 حتى تتوقف النقاط المركزية عن التحرك بشكل ملحوظ أو يتم الوصول إلى الحد الأقصى
لعدد مرات التكرار.
يعد تحديد العدد الصحيح للمجموعات (K) جانبًا مهمًا في استخدام هذه الخوارزمية. غالبًا ما يستخدم الممارسون
تقنيات مثل طريقة Elbow أو
يحللون
نتيجة Silhouette
لتقييم مدى فصل المجموعات الناتجة.
تطبيقات العالم الحقيقي في الذكاء الاصطناعي
تعد تقنية K-Means Clustering متعددة الاستخدامات للغاية وتجد فائدة في مختلف الصناعات للتبسيط و
المعالجة المسبقة للبيانات.
-
Image Compression and Color Quantization: In
computer vision (CV), K-Means helps reduce the
file size of images by clustering pixel colors. By grouping thousands of colors into a smaller set of dominant
colors, the algorithm effectively performs
dimensionality reduction while
preserving the visual structure of the image. This technique is often used before training advanced
object detection models to normalize input data.
-
Customer Segmentation: Businesses leverage clustering to group customers based on purchasing
history, demographics, or website behavior. This allows for targeted marketing strategies, a key component of
AI in retail solutions. By identifying high-value
shoppers or churn risks, companies can tailor their messaging effectively.
-
Anomaly Detection: By learning the structure of "normal" data clusters, systems can
identify outliers that fall far from any centroid. This is
valuable for fraud detection in finance and
anomaly detection in network security, helping
to flag suspicious activities that deviate from standard patterns.
-
Anchor Box Generation: Historically, object detectors like older YOLO versions utilized K-Means to
calculate optimal anchor boxes from training
datasets. While modern models like YOLO26 utilize advanced
anchor-free methods, understanding K-Means remains relevant to the evolution of detection architectures.
مثال على التنفيذ
While deep learning frameworks like the Ultralytics Platform handle
complex training pipelines, K-Means is often used for analyzing dataset statistics. The following Python snippet
demonstrates how to cluster 2D coordinates—simulating object centroids—using the popular
Scikit-learn library.
import numpy as np
from sklearn.cluster import KMeans
# Simulated coordinates of detected objects (e.g., from YOLO26 inference)
points = np.array([[10, 10], [12, 11], [100, 100], [102, 101], [10, 12], [101, 102]])
# Initialize K-Means to find 2 distinct groups (clusters)
kmeans = KMeans(n_clusters=2, random_state=0, n_init="auto").fit(points)
# Output the cluster labels (0 or 1) for each point
print(f"Cluster Labels: {kmeans.labels_}")
# Output: [1 1 0 0 1 0] -> Points near (10,10) are Cluster 1, near (100,100) are Cluster 0
مقارنة مع الخوارزميات ذات الصلة
من المهم التمييز بين K-Means والخوارزميات الأخرى ذات الأسماء أو الوظائف المماثلة لضمان اختيار الأداة الصحيحة
للمشروع.
-
K-Means مقابل K-Nearest Neighbors (KNN): غالبًا ما يتم الخلط بينهما بسبب حرف "K" في
أسمائهما. K-Means هو خوارزمية غير خاضعة للإشراف تُستخدم لتجميع البيانات غير المصنفة. في المقابل،
K-Nearest Neighbors (KNN) هو
خوارزمية تعلم خاضعة للإشراف تُستخدم
لتصنيف الصور والانحدار، وتعتمد
على البيانات المصنفة لإجراء تنبؤات بناءً على فئة الأغلبية من الجيران.
-
K-Means vs. DBSCAN: While both cluster data, K-Means assumes clusters are spherical and requires
the number of clusters to be defined beforehand.
DBSCAN
groups data based on density, can find clusters of arbitrary shapes, and handles noise better. This makes DBSCAN
superior for complex spatial data found in datasets with
irregular structures where the number of clusters is unknown.