# Exploring Animal Crossing with Data Science

May 18, 2020

Like many people around the world, I have been spending the last few weeks at home pouring hours into Nintendo’s newest hit game, Animal Crossing: New Horizons. In what the New York Times describe as “The Game for the Coronavirus Moment,” players take up the role of a human who moves to a deserted island to start a new town alongside their anthropomorphic animal neighbors. Crafting furniture, fishing, and shopping are among the activities you can engage in, and this idyllic world has proven to be the perfect escape over the last couple of months. COVID-19 has also coincided with me wrapping up my doctoral studies here at Berkeley. As I reflect on my time here, I wanted to take this opportunity to share some of the skills I picked up along the way that helped me become a full-fledged researcher. So, in this post I will illustrate how we can use data science techniques to analyze Animal Crossing! All of the code for this post can be found on my GitHub.

The villagers have been a staple of the Animal Crossing franchise for years. Players naturally have favorites, and some have even become internet sensations. Other villagers are not so lucky. With so many villagers, I thought it would be fun to explore some features about them, and hopefully help me keep building my town.

Figure 1: Raymond has become one of the most popular and sought-after villagers. Image Source: Nintendo

In this post, I will highlight some of the data science techniques that I use to acquire, clean, and analyze data. Using both Python and R, I will show you how to web scrape data, visualize the information with exploratory analysis, and apply a common machine learning technique. Data science is empowering because it provides a toolkit for researchers to solve problems at any stage of their research. Although my own research focuses on cybersecurity and privacy law, I wanted to do this blog post to show that data science can be applied across domains. In this case, we can even explore Animal Crossing villagers! Our first step will be to acquire some data about them, which we will scrape from the Animal Crossing wikipage, as seen in Figure 2.

Figure 2: The first five rows of the table that we are going to scrape. Source: Animal Crossing Wiki

To scrape this table, we’re going to use some common Python tools. In particular, we’ll use the Selenium and Beautiful Soup packages. Selenium is a package that allows one to automatically navigate to a website using code alone. The basic idea is that using Python, we can traverse the Internet without ever manually clicking anything. Then, we use Beautiful Soup to extract the underlying HTML from a webpage, where all of the information is stored. We can then scrape the table in Figure 2 and turn it into a pandas dataframe. Figure 3 shows some of this code, written in a Jupyter notebook.

Figure 3: Code to navigate to a webpage and scrape a table from it.

Now that we have a dataframe, we can start exploring and visualizing our data. For this part, we will switch over to R to do some plotting with the ggplot2 package, which is part of the tidyverse. The tidyverse is a suite of tools that is invaluable for data science. It includes methods for cleaning, organizing, plotting, and modeling data. It is important to note that as data scientists, we use the best tool available to us regardless of language. I frequently switch between Python and R depending on what I want to do. As I found out, learning to code in one language made learning a second or third programming language much easier because they all share the same computational principles. Being able to flexibly switch between languages, packages, and environments opens up lots of possibilities for budding data scientists.

Figure 4: Sample R Code to produce plots

So, what are some of the things that we can plot? We have information about each villager’s personality, species, birthday, and catchphrase. One thing we might do is visualize how many villagers there are per species. Then, we could shade these plots to break them down further by personality type. Because each personality type corresponds to a particular gender, we can also do a little data manipulation to make a column for gender and shade with that instead. There are lots of possibilities here, even with only a handful of columns to work with.

Figure 5: Barplots for Animal Crossing Villagers

Plotting out all of these relationships, we learn some interesting things! Cats, rabbits, and squirrels are among the most common species. Meanwhile meeting an octopus or cow is relatively rare. Broken down by personality type, different species also have different personality distributions. For example, there are a lot of jock frogs and peppy rabbits. Because the personality types also map to gender, different species also have different gender breakdowns.

Finally, we can even apply some common modeling techniques to this dataset. In this case, I will visualize k-means clusters and Principal Components Analysis (PCA). K-means clustering essentially groups data together that are similar. PCA is a common technique for dimensionality reduction, and it essentially simplifies information in our columns into fewer columns (or “components”). So, we first one-hot encode our data to transform the categorical Species, Personality, and Birth Month columns into numerical information. We then find some clusters, and plot them against two principal components. In this case, we don’t get very nicely separated clusters, which makes sense given we only have three features, and they are categorical rather than continuous. Still, the fact that we are able to explore the data this way with a little code is incredible.

Figure 6: K-Means Clusters on PCA Components

Using the skills that I learned over the last few years, I was able to acquire and explore this dataset, and learn some things about the villagers who populate my virtual town! Some species are much less common than I thought, and it was fascinating to see that different personalities are not equally distributed. Recruiting a diverse cohort of villagers to my town means I need to balance all of these different features. The fact that I was able to take my research skills and apply them to a hobby shows the flexibility that data science provides. The intellectual framework and techniques cross disciplinary boundaries, and can change the way we approach problems. Thinking in a data-driven way has not only changed the way I approach research, but also how I think about the world more generally.