t-SNE and more with The Simpsons Dataset

· Medium ·

4 min read Original article ↗

Sid Ghodke

I was curious to find out how characters in the Simpsons were related. What is the difference between a recurring and supporting character. And who REALLY shot Mr Burns.

I wrote a quick script to parse The Simpsons Wikia XML dump — this gets me the list of Characters in each episode.

Now I can build a co-occurance matrix. The co-occurance matrix basically counts the number of times two characters have been seen together. From this we can determine that, given one character was present, which other characters are likely to co-occur — but more on that a bit later. By the way, the dimentionality of this matrix is 2651 by 2651.

co_occurrence[char][cmp_char] += 1

Lets throw this matrix at t-SNE, I can produce this visualization in 2D embedding space.

Press enter or click to view image in full size

10,000ft View of Simpson’s Characters Relate (Longer distances between points means less related)

If we zoom in on the Simpsons’ family members (11 o’clock position), we can see the most important characters.

Main, Supporting and Recurring characters

Great! We can start to see groupings of common and recurring characters.

By running a clustering algorithm (k-Means), we can start to see how the Characters shake out. Clusters not in any particular order.

Cluster 1: Apu Nahasapeemapetilon, Barney Gumble, Carl Carlson, Clancy Wiggum, Edna Krabappel, Groundskeeper Willie, Julius Hibbert, Kent Brockman, Krusty the Clown, Lenny Leonard, Martin Prince, Milhouse Van Houten, Moe Szyslak, Ned Flanders, Nelson Muntz, Ralph Wiggum, Seymour Skinner

Cluster 2: Arnie Pye, Chief Wiggum, Comic Book Guy, Drederick Tatum, Duffman, Fat Tony, Mayor Quimby, Mr. Burns, Mr. Teeny, Reverend Lovejoy, …

Cluster 3: Agnes Skinner, Cletus Spuckler, Gary Chalmers, Hans Moleman, Jasper Beardly, Jimbo Jones, Kearney Zzyzwicz, Kirk Van Houten, Lou, Eddie …

Cluster 4: Bart Simpson, Homer Simpson, Lisa Simpson, Maggie Simpson, Marge Simpson

Cluster 5: Anoop Nahasapeemapetilon, Gheet Nahasapeemapetilon, Jamshed Nahasapeemapetilon, Nabendu Nahasapeemapetilon, Pahusacheta Nahasapeemapetilon, Itchy & Scratchy, Otto, Yoda …

Cluster 6: Sherri Mackleberry and Terri Mackleberry, Sherri and Terri, Sideshow Mel, Snake Jailbird, Snowball II, Waylon Smithers …

I’ve drastically truncated the output here, but already from the above we can see the core Simpsons Family are Cluster 4. I think Cluster 1 is the Supporting Characters, it was the second shortest list. It is followed by Cluster 2 and 3, as the Recurring Characters. And the remaining Clusters 5 and 6, are generally Non-speaking Characters.

Next, lets see if we can do something interesting with the embedding space. A very famous problem is to solve the analogy Man : Woman :: King : Blank, where Blank, of course, is Queen. I can wire up an optimization method that solves for the analogy using the formulation

queen is the word w that maximizes: cos(w, king) - cos(w, man) + cos(w, woman).

Now we can ask some interesting questions:

Who appears with Skinner as much as Homer to Mr. Burns, Raphael!

Homer appears with Mr Burns, as much as Skinner appears with XRaphael matches with -0.007807840754573123 score
Agnes Skinner matches with -0.006507300136243243 score
Seymour Skinner matches with -0.0013063478460662251 score
Ned Flanders matches with 0.0003449207169453298 score
Julius Hibbert matches with 0.0013784755420673082 score

Carl appears with Lenny in the same way that Bart appears with Milhouse

Bart : Milhouse :: Lenny : XCarl Carlson matches with 0.00836752019644514 score
Lenny Leonard matches with 0.009283519230516186 score
Seymour Skinner matches with 0.010598601885707543 score
Milhouse Van Houten matches with 0.011148180200725749 score
Groundskeeper Willie matches with 0.011500540096501217 score

And just for fun — Milhouse is to Smithers and Bart is to Mona Simpson!

Milhouse : Smithers :: Bart : XWaylon Smithers matches with -0.015349511875509168 score
Mona Simpson matches with 0.004664844417944729 score
Larry (barfly) matches with 0.012164243760824693 score
Mr. Burns matches with 0.01671679923621895 score
Lenny Leonard matches with 0.018889929394280414 score

Pretty cool.

This was a small experiement with a familiar cast of characters, and we could already see the latent structure present in the dataset. The same techniques I mentioned above are used everywhere, from Search Engines to People You Make Know. I have some interesting, future plans for this data, stay tuned.