Using Foursquare to classify tube stations

Introduction

The London underground system is the oldest underground metro in the world.
It consists of 11 lines linking 270 stations. In 2017/18 it carried nearly 1.4 billion passengers

For my Capstone project, I will be looking into the neighbourhoods that each station is found in. By analyzing the venues surrounding each station I will be able to group them and also determine what is their primary usage, whether they serve an area which is mostly residential, office, commercial based etc.

The target audience for this analysis would be TfL (Transport for London) and local government for town planning purposes. It will show what the system is being used for, how people travel from home to work or entertainment, and where expansion of the network may be required.

Data

The data I will require to execute this project will include:

  1. The geo locations of each of the stations on the network (which I will scrape from Wikipedia here). From this I will be able to extract the latitude and logitude of each of the stations which will allow me to plot them on a map and to find the venues near to their location.
  1. The Foursquare dataset which I will access to ascertain the primary venue types surrounding each station. This could be ‘residential’, or ‘education’, or ‘bars & restaurants’ etc. I will pass the Foursquare API the latitude and longitude coordinates for each underground station and it will return to me the varieties
    of venue in the surrounding area.

Based on the primary venue types surrounding each station I will be able to do a cluster analysis to identify groups of stations and examine what these groups have in common / what differentiates them from other groups.

Methodology

1 – Scraping and wrangling data

In the first place we used a module called BeautifulSoup to scrap the table of station names along with the latitude and longitude values from the wikipedia page mentioned above. We limited ourselves to the columns which were required for our analysis and converted it into a dataframe. Some wrangling was necessary where the table included duplicate values and explanatory text in numerical columns.

2 – Calling the Foursquare API for data about venues surrounding each station

Once we had the dataframe of stations with their geo-locations, we created a function to call the Foursquare API and query the total results for each high level category of venue within a given radius of each station. The function updated a new dataframe to show the totals of each venue type alongside each station.

3 – Exploring and clustering the data

Now we had a usable dataset showing the list of stations, their geo-locations and the totals for each venue type surrounding them. We transformed this into an array and scaled it so that it was possible to create boxplots using the module seaborn for the numbers of venue types on both an absolute and a relative basis. We also ran a K-means cluster on the dataset to group the stations into 5 clusters based on the total number of venue types surrounding them.

4 – Creating a map with stations colour-coded by cluster

By adding the cluster labels back onto the dataset of station names and geo-locations, we used the folium module to create a map of London and superimpose the stations, colour-coded to show where the clusters lie.

Results

From our first analysis of the number of venue types it was clear that the category of “Food” has the highest number of venues in London. Several categories such as Arts, Educational and Residential were significantly lower, and the more commercial venue types had significantly more ratings. This is probably because the primary way people use Foursquare is to rate commercial locations rather than every building.

undefined

When we clustered our stations we saw the following:

undefined

We can characterise these clusters as follows:

  • Cluster 0 (blue in map below) – Scored low across all venue types.
  • Cluster 1 (green in map below) – Scored highly in Food, Nightlife and Professional venues.
  • Cluster 2 (orange in map below) – Scored highly in Food, Residence and to a lesser extent Professional venues.
  • Cluster 3 (red in map below) – Had middling to low scores in most categories
  • Cluster 4 (white in map below) – Scored the highest in all venue types except for residential

If we plot the stations on a map and colour code them by the clusters we just discussed then the picture becomes clearer:

undefined

  • Blue stations – our low scoring cluster 0, these stations are far outside central London
  • Red stations – our middling to low scoring cluster 3, these stations are on the outskirts of central London. They have more venues than the blue stations, but still far fewer than those in central London
  • Green stations – food, nightlife and professional venues, cluster 1: the square mile, the City of London. Lots of busy bankers looking for lunch and a night out.
  • Orange stations – food, arts and professional venues, cluster 2: the West End. Not as many professional venues as the City but still a very busy, very touristy part of London.
  • White stations – cluster 4, scoring highly across the board we find central London. No surprises there.

Discussion

There are a number of limitations to this analysis that should be borne in mind related to the fact that it relies entirely on the Foursquare data.

Firstly, Foursquare does not represent everything. It is to be expected that the highest number of venues to be found are food and other commercial venues, since people use the service to rate and review places where they have spent money. It is likely that since people do not rate every single building, categories such as residential in particular will be significantly underrepresented.

Secondly, Foursquare does not account for the sizes of venues. A large university in one part of London may educate and house thousands of students but it will only count as 1 venue, just as each ice cream kiosque or burger stand will count as 1 venue despite being many times smaller.

Conclusion

Despite the limitations of the Foursquare data discussed above, it was possible to identify general areas of London and characterise them based on the kinds of venue that proliferate in them. Further analysis could be based on the number of addresses in each postcode to try to cross reference that to find the hidden venues, or some kind of weighting based on venue size could be attempted.

Introduce Yourself (Example Post)

This is an example post, originally published as part of Blogging University. Enroll in one of our ten programs, and start your blog right.

You’re going to publish a post today. Don’t worry about how your blog looks. Don’t worry if you haven’t given it a name yet, or you’re feeling overwhelmed. Just click the “New Post” button, and tell us why you’re here.

Why do this?

  • Because it gives new readers context. What are you about? Why should they read your blog?
  • Because it will help you focus you own ideas about your blog and what you’d like to do with it.

The post can be short or long, a personal intro to your life or a bloggy mission statement, a manifesto for the future or a simple outline of your the types of things you hope to publish.

To help you get started, here are a few questions:

  • Why are you blogging publicly, rather than keeping a personal journal?
  • What topics do you think you’ll write about?
  • Who would you love to connect with via your blog?
  • If you blog successfully throughout the next year, what would you hope to have accomplished?

You’re not locked into any of this; one of the wonderful things about blogs is how they constantly evolve as we learn, grow, and interact with one another — but it’s good to know where and why you started, and articulating your goals may just give you a few other post ideas.

Can’t think how to get started? Just write the first thing that pops into your head. Anne Lamott, author of a book on writing we love, says that you need to give yourself permission to write a “crappy first draft”. Anne makes a great point — just start writing, and worry about editing it later.

When you’re ready to publish, give your post three to five tags that describe your blog’s focus — writing, photography, fiction, parenting, food, cars, movies, sports, whatever. These tags will help others who care about your topics find you in the Reader. Make sure one of the tags is “zerotohero,” so other new bloggers can find you, too.

Create your own website, free on WordPress.com.Create your own website.
Start free