The use of Foursquare API and clustering of Sydney meals venues.

Harry Ngo
Photograph by way of Jakub Kapusnak on Unsplash

Background Knowledge

For some, it may be tricky to determine the place they wish to consume out with their pals. There are such a lot of other meals choices and cuisines to choose between. Usually, if a space is understood for a selected delicacies, then guests are much more likely to consume in that house for that delicacies. With Australia being a multicultural nation, it’s not arduous to search out other ethnic communities in not unusual spaces.

On this challenge, our goal is to lend a hand folks find places in Sydney on the place to consume in keeping with their hobby. As an example, if you’re feeling like consuming Thai meals then it’s essential to cross to those specific suburbs round you. We can use Foursquare API and Okay-means clustering to find the commonest cuisines in spaces and visualize the clusters on a map.


There are a variety of eating places positioned as regards to teach stations in Sydney. Because of this, I compiled a knowledge set which incorporates all of Sydney’s teach and Metro stations, together with their respective coordinates in latitude and longitude. This may permit us to visualise the places on a map of Sydney. The places of stations had been selected as nearly all of Sydney employees trip by way of public delivery. Eating places as regards to stations supply handy choices for citizens. You’ll view the information set here, a complete of 181 stations.

.head() of Sydney stations knowledge body

Technique and Research

After studying the Sydney stations right into a pandas knowledge body, we can now use Foursquare API to search out meals venues close to our stations. We can then use Okay-means clustering to create clusters of stations that have identical options.

First, let’s see how our stations seem like on a map of Sydney to test if our coordinates are proper. We will use the geopy and folium library to create a map of Sydney with our station coordinates superimposed on best.

Sydney Educate/Metro Community — Run the Pen for an interactive model of the map

Now, we use Foursquare API, a social location carrier that permits customers to find about companies and sights. As we are looking for meals similar venues, I added the class ID ‘4d4b7105d754a06374d81259’ as a part of the API request URL. I outlined the prohibit of venues returned to be 300 and a radius of one km.

url = '{}&client_secret={}&v={}&ll={},{}&radius={}&prohibit={}'.layout(

With our extracted data of close by meals venues, we will merge this into a brand new knowledge body containing our station data.

.head() of extracted Sydney meals venues knowledge body

A complete of 5565 meals venues had been returned by way of Foursquare. On the other hand, when taking a look on the depend of venues for some stations, some had low counts or none in any respect. To ensure that those stations not to affect the clustering, I got rid of all stations which had lower than nine meals venues. This ended in 5458 meals venues in general and 175 distinctive classes. From 181 stations, our knowledge body now contained 142 stations i.e. 39 stations had been got rid of.

After the use of one sizzling encoding and taking the imply of the frequency for each and every meals venue class, we will use Okay-means clustering, an unmanaged finding out set of rules used to create Okay clusters of information issues in keeping with function similarity.

sydney_onehot = pd.get_dummies(sydney_venues[['Venue Category']], prefix="", prefix_sep="")

# upload group column again to dataframe
sydney_onehot['Station'] = sydney_venues['Station']

# transfer group column to the primary column
fixed_columns = [sydney_onehot.columns[-1]] + checklist(sydney_onehot.columns[:-1])
sydney_onehot = sydney_onehot[fixed_columns]

sydney_grouped = sydney_onehot.groupby('Station').imply().reset_index()
Sydney grouped knowledge body appearing imply of the frequency for the primary 7 classes

The use of this knowledge body, we will drop the primary column and use it for Okay-means clustering. Staring at the elbow manner and silhouette ranking, we discover that the optimum selection of clusters to make use of is Okay = 5.

Elbow manner and silhouette ranking for optimum Okay

With our cluster labels generated, we will upload this to a brand new knowledge body merging our unique knowledge body and their respective best 10 venues.

As there have been stations got rid of in the past, we download NaN values for some stations, so we take away those rows the use of the dropna() manner.
I visualized the first maximum not unusual venues for each and every station, grouped by way of their clusters to present us a transparent image of what options those clusters can have.

From taking a look on the bar chart, I generalized each and every cluster and labelled them.
I needed to come with a number of classes as labels since there have been similarities in best venues, reminiscent of cafés:

Labels for each and every cluster in a knowledge body

I additionally added the highest Three venues for each and every station so we will view them on a map.

Our ultimate knowledge body containing all our new data is then created by way of merging the former knowledge frames.


Our ultimate map exhibiting the clusters with data is created.

Map of Sydney stations clustered

The result of clustering a complete of 142 stations (at the start from 181) show off Five clusters. We generalized the clusters as the next:

  • Cluster 0 (yellow) — 23 stations, Bakery/Cafe/Indian Eating place Venues
  • Cluster 1 (cyan) — 2 stations, Korean Eating places
  • Cluster 2 (pink) — 35 stations, Cafe Venues
  • Cluster 3 (blue) — 63 stations, Cafe/Espresso/Thai Eating place Venues
  • Cluster 4 (crimson) — 19 stations, Chinese language/Vietnamese/Pizza Eating place Venues
Meals Venues round Sydney Stations — Run the Pen for an interactive model of the map

Despite the fact that some stations would possibly vary from the label description, it provides a just right evaluate of what forms of eating places or meals venues there are.


To conclude, we used Okay-means clustering which created Five other clusters for Sydney teach and Metro stations having no less than nine meals venues round them. Subsequent time whilst you cross out to consume (optimistically quickly!), it’s essential to take a look at this map to present a good suggestion of what forms of meals are introduced at Sydney stations with surrounding meals venues.


Please enter your comment!
Please enter your name here