Module 17 · Finding Hidden Groups

How does a computer circle the clumps?

Lesson 17.2 · Circling the Clumps

Felix · Ashley

Drop pins. Assign. Move pins. Repeat. That’s the whole algorithm.

Ashley circled three groups by eye.

Felix wants the computer to do it, and to do it the same way twice.

The recipe turns out to be simple enough to do on a napkin: drop three pins, assign every song to its nearest pin, move each pin to the middle of its songs, repeat.

The short answer

It doesn’t circle. It plants three centers, lets each song pick the closest, moves the centers to the middle of their songs, and repeats until the picture stops changing. Four rounds, on the song map. Ashley’s finger-circles and the computer’s groups agree almost exactly.

Try it yourself

Press Step. Watch the centers claim their nearest songs, then move to the middle of what they claimed. Keep stepping until they stop moving.

Press Step. Watch the centers claim their nearest songs, then move to the middle of what they claimed. Keep stepping until they stop moving.Unholy (feat. Kim Petras)Quevedo: Bzrp Music Sessions, Vol. 52I'm Good (Blue)La BachataMe Porto BonitoTití Me PreguntóUnder The InfluenceEfectoI Ain't WorriedOjitos LindosAs It WasMoscow MuleGlimpse of UsSweater WeatherAnother LoveCUFF ITNeveritaPROVENZALeft and Right (Feat. Jung Kook of BTS)Super Freaky GirlI Wanna Be YoursCalm Down (with Selena Gomez)Jimmy Cooks (feat. 21 Savage)LOKERATarotCaileBlinding LightsVegas (From the Original Motion Picture Soundtrack ELVIS)FerrariAtlantisSTAR WALKIN' (League of Legends Worlds Anthem)Te FelicitoPartyLa CorrienteRunning Up That Hill (A Deal With God)StarboyMIDDLE OF THE NIGHTDandelionsUntil I Found YouI Was Never ThereGATÚBELASex, Drugs, Etc.Something in the OrangeOne Kiss (with Dua Lipa)Gangsta's ParadiseSTAY (with Justin Bieber)BILLIE EILISH.About Damn TimeWAIT FOR U (feat. Drake & Tems)I Love You SoLA INOCENTEcómo dormiste?Hold Me Closerlovely (with Khalid)Call Out My NameWatermelon SugarSave Your TearsBonesWomanWhere Are You NowI'm Not The Only OneDo I Wanna Know?505Why'd You Only Call Me When You're High?Mary On A CrossWithout MeNo Role ModelzKesariya (From "Brahmastra")Heat WavesAfter LIKELA CANCIÓNBABY OTAKULa Llevo Al Cielo (Ft. Ñengo Flow)Night ChangesGhostBad Habitsdrivers licenseThe HillsDie For Youtraitorgood 4 uDon’t Blame MeHappier Than EverBelieverEvergreen (You Didn’t Deserve Me At All)Daddy IssuesMiss YouOnly Love Can Hurt Like ThisBad Decisions (with BTS & Snoop Dogg)NumbBelly DancerBam Bam (feat. Ed Sheeran)Locked out of HeavenRevengeTHATS WHAT I WANTThe Real Slim ShadyBetty (Get Money)Shut DownYonaguniLo Siento BB:/ (with Bad Bunny & Julieta Venegas)Un RatitoFeliz Cumpleaños FerxxoDos Mil 16Un CocoPerfectSomeone You LovedHeatherdeja vuEverybody Wants To Rule The WorldSoftcoreMr. BrightsideabcdefuYou ProofThe NightsThere's Nothing Holdin' Me BackThe Mottoeverything i wantedHopeINDUSTRY BABY (feat. Jack Harlow)Till I CollapseFirst ClassSupermanBeggin'FreaksTalk that TalkPepasProhibidoxAndreaShape of YouPeaches (feat. Daniel Caesar & Giveon)augustIn The StarshappierEnemy (with JID) - from the series Arcane League of LegendsEvery Breath You TakeCreepIn the EndEasy On MeLost in the Fire (feat. The Weeknd)Levitating (feat. DaBaby)positionsSomething Just Like ThisLeave Before You Love Me (with Jonas Brothers)Wake Me UpWords (feat. Zara Larsson)Bad Memories (feat. Elley Duhé & FAST BOY)Crazy What Love Can DoLet Me Down SlowlyBoredEverybody Dies In Their NightmaresHighway to HellBack In BlackOne DanceStanTHE LONELIESTShinunoga E-WaDynamitePink VenomYet To ComeLOVE DIVEMEMORIASPUNTO 40X ÚLTIMA VEZLos CachosOtro AtardecerNormalYo No Soy CelosoMe Fui de VacacionesLivin' On A PrayerSomewhere Only We KnowRunning Up That Hill (A Deal With God) - 2018 RemasterInfinityBlank SpaceHymn for the WeekendMemoriesPhotographSay You Won't Let GoLoverMAMIIIThunderHouse of MemoriesStressed OutTake on MeClean White Noise - Loopable with no fadeAll of MePink + WhiteLostSeven Nation ArmyLosing My ReligionEverlongFeel Good Inc.ApocalypseHotel California - 2013 RemasterHave You Ever Seen The RainRock and A Hard PlaceCloserLet Me Love You7 ringsLevitatingNeed to KnowTreat You BetterYou RightHips Don't Lie (feat. Wyclef Jean)Leave The Door OpenShallowDon't Be ShyHot In Itbad guyDeep Down (feat. Never Dull)Jocelyn FloresStill D.R.E.EnvolverBamba (feat. Aitch & BIA)Sweet Child O' MineThunderstruckGod's PlanHUMBLE.goosebumpsYKWIM?Pumped Up KicksRun BTSYo Voy (feat. Daddy Yankee)La SantaEnséñame a BailarAguaceroI'm Still StandingWhat Makes You BeautifulLove Yourselfyou broke me firstPayphoneHeartbreak AnniversaryDemonsLittle Dark AgeYou Get Me So HighSmells Like Teen SpiritShut Up and DanceNumbChop Suey!KICK BACKdouble takeWasted On YouNo LieI Like Me BetterAttentionStuck with U (with Justin Bieber)HappierToo Good At GoodbyesOne Last TimeMoth To A Flame (with The Weeknd)Bye ByeStreetsThat's What I LikeUptown Funk (feat. Bruno Mars)For My Hand (feat. Ed Sheeran)21 Reasons (feat. Ella Henderson)Last LastSeptemberWhere Did You Go? (feat. MNEK)Fuck Love (feat. Trippie Redd)SAD!MoonlightThe Night We MetR U Mine?Don't Stop Believin'Summer HighNo LoveMONTERO (Call Me By Your Name)Mask OffGodzilla (feat. Juice WRLD)All The Stars (with SZA)Love The Way You LieN95In Da ClubFair Trade (with Travis Scott)SICKO MODEFormulaMount EverestI WANNA BE YOUR SLAVEWashing Machine HeartLovers RockSUPERMODELNotionTek It - Sped UpYoungRush Hour (Feat. j-hope of BTS)ButterFEARLESSDÁKITIBesos Moja2DesesperadosSi Te La Encuentras Por AhíVolvíVolando - RemixParty en el Barrio (feat. Duki)DilesUN PESOSi Estuviésemos JuntosA Tu MercedAgostoEl ApagónABC

What you just did has a name

K-means

K-means is the standard clustering algorithm.

K is how many groups you ask for.

It starts with k random centers, assigns each point to the nearest one, moves each center to the average of its points, and repeats until nothing changes.

Simple, fast, and everywhere.

A few more things

Two steps, over and over

Assign: every point picks its closest center. Update: every center moves to the middle of its points. Each round makes the groups a little tighter, and after a handful of rounds they stop changing.

The start matters

Different random starting pins can settle into different groups. Real tools run it several times from different starts and keep the tightest result. If your groups change a lot between runs, the clusters aren’t as real as they look.

It finds round groups

K-means draws each group as “everything closest to this center,” so it likes blobs. Long, curved or nested groups confuse it. Other algorithms exist for those shapes, but k-means is the one to learn first.

Go deeper

The longer version, if you want it.

What it’s minimizing

Each round of k-means lowers one number: the total squared distance from every point to its assigned center. Assigning points to their nearest center lowers it. Moving centers to the average lowers it. Since it can only go down and can’t go below zero, the algorithm has to stop, which is why it always settles. It just isn’t guaranteed to settle in the best possible place, which is the random-start problem.

The average of a group of points is its centroid, so the method is sometimes described as “find k centroids.” Same thing.

Quick check

Halfway through a k-means run, one center has 40 points assigned to it. What happens to that center next?

Let’s take it slower

Picture it with pins and string on a table. Toss k pins down at random. Tie each dot to whichever pin is nearest. Now slide each pin to the middle of the dots tied to it. Some dots are now closer to a different pin, so retie those. Slide again. After a few rounds nothing changes hands and the pins stop moving. That’s the answer. The only settings you chose were how many pins and where they happened to land, and the second one is why real tools run it more than once.

More examples

1

Placing three food trucks

A city wants three food trucks placed where the lunch crowd is. Guess three spots, send each person to the nearest truck, then move each truck to the middle of its crowd and repeat. After a few rounds the trucks stop moving, and that is k-means with wheels.

2

Shrinking a photo to 16 colors

Image tools reduce a photo to 16 colors using k-means: pick 16 starting colors, assign each pixel to the nearest, average each group and repeat. The picture ends up in 16 shades nobody chose by hand. The k was the only decision a person made.

Back to the question

So how does the computer circle the clumps?

Show the answer

It doesn’t circle. It plants three centers, lets each song pick the closest, moves the centers to the middle of their songs, and repeats until the picture stops changing. Four rounds, on the song map. Ashley’s finger-circles and the computer’s groups agree almost exactly.

One last question

Build the k-means loop.

Tap the pieces in order. Tap one again to take it back.

…
Talk Card

Talk Card · 17.2

K-means drops k centers, gives every point to its closest center, moves each center to the middle of its points, and repeats until nothing moves.

Data to AI · pettigrewlab.com/learn

Next lesson