惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
J
Java Code Geeks
雷峰网
雷峰网
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
腾讯CDC
博客园 - 司徒正美
B
Blog RSS Feed
博客园 - 三生石上(FineUI控件)
I
InfoQ
N
Netflix TechBlog - Medium
L
LangChain Blog
博客园_首页
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tailwind CSS Blog
MyScale Blog
MyScale Blog
美团技术团队
The Cloudflare Blog
爱范儿
爱范儿
Stack Overflow Blog
Stack Overflow Blog
博客园 - 聂微东
H
Help Net Security
Martin Fowler
Martin Fowler
V
Visual Studio Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Unsupervised Machine Learning. K-Means & Hierarchical Clu...
Kelvin · 2026-04-30 · via DEV Community
Cover image for Unsupervised Machine Learning. K-Means & Hierarchical Clustering

Kelvin

Unsupervised machine learning is a branch of machine learning where models are trained on data without labelled outcomes. Unlike supervised learning, where the goal is to predict a known target, unsupervised learning focuses on discovering hidden patterns, structures, or relationships within the data.

Common tasks in unsupervised learning include:

  • Clustering (grouping similar data points)
  • Dimensionality reduction

Clustering is the process of grouping data points such that points within the same cluster are similar and points in different clusters are dissimilar.

Similarity is usually measured using distance metrics like:

  • Euclidean distance (most common)
  • Manhattan distance
  • Cosine similarity

K-Means Clustering.

K-Means is a partition-based clustering algorithm that divides data into K distinct clusters, where K is predefined. The goal is to minimize the within-cluster variance, also called inertia.

How K-Means Works

  1. Choose K (number of clusters) - Example: K = 3
  2. Initialize centroids randomly - These are K points representing cluster centers.
  3. Assign data points to nearest centroid - Each point is assigned to the cluster with the closest centroid (using distance, usually Euclidean).
  4. Update centroids - Compute the new centroid as the mean of all points in that cluster. iterate steps 3 and 4 until Centroids stop changing, or maximum iterations is reached.

K-Means Workflow.

Scaling data

Finding the best K.

Plotting the elbow curve. This helps identify the best K - where the curve starts to plateau.

Elbow curve. (4 is our best k)

Training the model
Fits our model on 4 clusters then creates a new column named 'Clusters'.

Profiling clusters.
This code is all about understanding what each cluster actually represents after you’ve created them with K-Means.

Visualization of the clusters and the centroids.


Advantages of K-Means

  • Simple and fast
  • Works well on large datasets
  • Easy to interpret

Limitations of K-Means

  1. Must specify K in advance
  2. Sensitive to - Initial centroid placement & Outliers
  3. Assumes clusters are spherical and equally sized

Hierarchical Clustering.

Hierarchical clustering builds a tree-like structure of clusters, called a dendrogram. Unlike K-Means, it does not require specifying the number of clusters upfront.

There are two types:

  • Agglomerative (bottom-up) – most common
  • Divisive (top-down)

Agglomerative clustering
steps

  1. Start with all points separate: Treat each data point as its own cluster like A, B, C, ... Initially, you have n clusters for n data points.
  2. Compute pairwise distances: Calculate the distance between every pair of clusters. Common choices include Euclidean, Manhattan or Cosine distance. Store these values in a distance matrix. To know more about them refer to: Measures of Distance
  3. Merge the nearest clusters: Identify the two clusters that are closest based on the chosen linkage method such as single, complete, average or Ward linkage. Combine them into a single new cluster.
  4. Update distances: Recalculate the distances between the newly formed cluster and all remaining clusters. Use the same linkage rule to ensure consistency.
  5. Repeat the process: Continue merging clusters and updating distances iteratively. Stop when you reach a predefined number of clusters (k) or a distance threshold.
  6. Visualize the results: Create a dendrogram to visualize how clusters merged at each step. Choose a suitable cut on the dendrogram to obtain the final cluster groups.

Linkage methods
How we measure the distance between clusters.

  1. Single Linkage: Minimum distance between points
  2. Complete Linkage: Maximum distance
  3. Average Linkage: Average distance
  4. Ward’s Method: Minimizes variance (most common)

Dendrogram
A dendrogram is a tree diagram that shows:

  • How clusters are merged
  • At what distance they are merged You can “cut” the dendrogram at a certain height to decide the number of clusters.

Hierarchical model workflow.
Picking a small dataset for easier readability.

Linkage (Ward) to minimize variance.

Plotting the dendrogram.

Fitting the model.
Fits our model on 4 clusters then creates a new column named 'hc-cluster'.

Profiling.
This step helps understand what each cluster represents.

Visualization.
A comparison between the two models. K-Means & Hierarchical clustering.

Advantages of Hierarchical Clustering

  • No need to predefine number of clusters
  • Produces interpretable tree structure
  • Works well for small datasets

Limitations of Hierarchical Clustering

  • Computationally expensive (slow for large datasets)
  • Once clusters are merged, they cannot be undone
  • Sensitive to noise and outliers