← ZPĚTBACK

09Učení bez učitele — shlukování (k-means)Learning without a teacher — clustering (k-means)

Perceptron (6) i neuronová síť (8) se učily z bodů, u kterých byla správná odpověď — barva. To je učení s učitelem. Tady body žádnou barvu nemají. Algoritmus k-means musí skupiny najít sám — jen podle toho, které body leží blízko sebe. The perceptron (6) and the neural network (8) learned from points that came with the right answer — a color. That is learning with a teacher. Here the points have no color at all. The k-means algorithm has to find the groups on its own — only from which points lie close together.

Co a jak?What & how?

Co to jeWhat it is Shlukování (clustering) = rozdělení dat do skupin podobných bodů, bez jakýchkoli štítků — učení bez učitele. k-means hledá k skupin a každou zastupuje jejím středem (průměrem jejích bodů, anglicky centroid).Clustering = splitting data into groups of similar points, with no labels at all — learning without a teacher (unsupervised learning). k-means looks for k groups and represents each one by its center (the average of its points, the centroid).

Co zkusitWhat to try Mačkej Jeden krok a sleduj dvě fáze, které se střídají: body se obarví podle nejbližšího středu, pak se středy přesunou doprostřed svých bodů. Potom stiskni Spusť do konce. Zkus „5 skupin“ několikrát za sebou (různý start → různý výsledek), „dva měsíce“ (k-means selže) a „rovnoměrný šum“ (skupiny „najde“, i když žádné nejsou). Kliknutím do plochy přidáš bod.Press One step repeatedly and watch the two alternating phases: the points take the color of the nearest center, then the centers move into the middle of their points. Then press Run to the end. Try “5 groups” several times in a row (different start → different result), “two moons” (k-means fails) and “uniform noise” (it “finds” groups even though there are none). Click into the canvas to add a point.

Proč je to důležitéWhy it matters Většina dat na světě štítky nemá — nikdo je ručně neoznačil. Shlukování v nich najde strukturu: skupiny podobných zákazníků, podobné dokumenty nebo shluky slov s podobným významem v embeddinzích (13).Most of the world’s data has no labels — nobody marked it by hand. Clustering finds structure in it: groups of similar customers, similar documents, or clusters of words with similar meaning in embeddings (13).

1 náhodné středyrandom centers → 2 přiřaď body nejbližšímu středuassign points to the nearest center → 3 posuň středy do průměrumove centers to the average ↺ (2 a 3 dokola), dokud se něco mění(2 and 3 again) until nothing changes
1 · data (body nemají žádnou barvu ani štítek)1 · data (the points have no color and no label)

počet shluků knumber of clusters k 3
startstart
2 · k-means krok po kroku2 · k-means step by step
① středy① centers ② přiřaď body② assign points ③ posuň středy③ move centers
iterace:iteration: 0 inertia:inertia: – stav:state: –
Inertia = součet druhých mocnin vzdáleností bodů od středu jejich shluku (čím menší, tím těsnější shluky). Kliknutím do plochy přidáš bod. Data jsou uměle vygenerovaná; algoritmus je skutečný k-means, jen ve 2D, aby byl vidět (v praxi mají data často stovky rozměrů). Inertia = the sum of squared distances of the points from the center of their cluster (smaller = tighter clusters). Click into the canvas to add a point. The data are artificially generated; the algorithm is real k-means, just in 2D so you can see it (real data often have hundreds of dimensions).
3 · jak zvolit k? graf „loket“ (pro každé k 10 startů, bere se nejlepší)3 · how to choose k? the “elbow” chart (10 starts for each k, the best one counts)
Jak k-means hledá skupiny a kde má hraniceHow k-means finds groups and where its limits are

k-means nic neví o tom, co body znamenají. Má jen jedno měřítko — vzdálenost — a jeden cíl: aby body byly co nejblíž středu svého shluku (malá inertia). Dva jednoduché kroky opakované dokola se k tomu cíli postupně přibližují, podobně jako gradientní sestup v příkladu 7 zmenšuje chybu. k-means knows nothing about what the points mean. It has only one measure — distance — and one goal: points should be as close as possible to the center of their cluster (small inertia). Two simple steps repeated over and over improve that goal little by little, much like gradient descent in example 7 shrinks the error.

S učitelem × bez učiteleWith vs. without a teacher

S učitelem (příklady 6 a 8): ke každému bodu známe správnou odpověď a model se učí ji předpovídat. Bez učitele (tady): odpovědi nemáme vůbec, model hledá strukturu — co k sobě patří. With a teacher (examples 6 and 8): we know the right answer for every point and the model learns to predict it. Without a teacher (here): there are no answers at all, the model looks for structure — what belongs together.

Výsledek tu nemá „správné řešení“ k porovnání — proto je těžší ho posoudit. There is no “correct answer” to compare the result with — which makes it harder to judge.

Dva kroky dokolaTwo steps, over and over

Přiřaď: každý bod patří nejbližšímu středu. Posuň: každý střed se přesune do průměru svých bodů. Ani jeden krok nemůže inertii zvětšit, takže se algoritmus vždy zastaví — ve chvíli, kdy žádný bod nezmění shluk. Assign: every point belongs to the nearest center. Move: every center moves to the average of its points. Neither step can increase the inertia, so the algorithm always stops — at the moment when no point changes its cluster.

Zastaví se ale v lokálním minimu: z jiného startu může skončit jinde (zkus „5 skupin“). Proto se v praxi spouští víckrát a bere se nejlepší výsledek; chytřejší start k-means++ volí středy daleko od sebe. But it stops in a local minimum: from a different start it can end elsewhere (try “5 groups”). That’s why in practice it’s run several times and the best result is kept; the smarter k-means++ start picks centers far apart.

Jak zvolit k?How to choose k?

Víc shluků znamená vždy menší inertii (při k = počet bodů je nulová), takže „nejmenší inertia“ nepomůže. Hledá se loket — místo, kde přidání dalšího shluku už skoro nic nepřinese. More clusters always means smaller inertia (with k = number of points it is zero), so “smallest inertia” doesn’t help. You look for the elbow — the point where adding another cluster barely helps any more.

Často loket zřetelný není a k volí člověk podle účelu: třeba e-shop chce 4 skupiny zákazníků, protože má 4 typy nabídek. Often there is no clear elbow and a human picks k by purpose: say a shop wants 4 customer groups because it has 4 kinds of offers.

Hranice metodyLimits of the method

k-means předpokládá kulaté shluky podobné velikosti. Hranice mezi dvěma středy je vždy rovná čára, takže zahnuté „měsíce“ nebo kruh v kruhu rozřízne napříč. Na takové tvary jsou jiné metody, např. DBSCAN (hledá husté oblasti). k-means assumes round clusters of similar size. The border between two centers is always a straight line, so curved “moons” or a circle in a circle get cut straight across. Such shapes need other methods, e.g. DBSCAN (it looks for dense regions).

Je citlivý na start a na měřítko os: když je jedna osa v korunách a druhá v letech, rozhodují jen koruny — data se proto nejdřív přeškálují. It is sensitive to the start and to the scale of the axes: if one axis is in dollars and the other in years, only the dollars matter — so the data are rescaled first.

K čemu to jeWhat it’s good for

Segmentace zákazníků (kdo nakupuje podobně), komprese barev obrázku (miliony barev → k středů = paleta k barev), seskupení podobných dokumentů nebo embeddingů (13), detekce anomálií (bod daleko od všech středů je podezřelý). Customer segmentation (who shops alike), color compression of an image (millions of colors → k centers = a k-color palette), grouping similar documents or embeddings (13), anomaly detection (a point far from every center is suspicious).

Pozor: najde i to, co tam neníCareful: it finds what isn’t there

k-means vrátí vždy přesně k shluků — i v rovnoměrném šumu, kde žádné skupiny nejsou. Algoritmus nepozná, jestli jsou skupiny skutečné. Jestli dávají smysl a co znamenají, musí posoudit člověk. k-means always returns exactly k clusters — even in uniform noise, where there are no groups. The algorithm can’t tell whether the groups are real. Whether they make sense and what they mean is up to a human.

Pointa: i bez odpovědí se dá z dat něco naučit — jejich strukturu. Ale k-means vidí jen vzdálenosti: najde kulaté skupiny, i když tam žádné nejsou, a zahnuté tvary nepozná. Stejná myšlenka „blízko = podobné“ se vrátí u embeddingů (13), kde slova s podobným významem tvoří shluky. The point: even without answers you can learn something from data — its structure. But k-means only sees distances: it finds round groups even where there are none, and it can’t recognize curved shapes. The same idea “close = similar” comes back with embeddings (13), where words with similar meaning form clusters.

Verze 2.0.1 · aktualizováno 29. 9. 2026 Version 2.0.1 · updated 29 September 2026