Analysing store locations. Finding the optimal location for a new…
By: Emilio Macias
Tags: Scrapping, Scatter Plots, K Means Clustering, GIS, API

Analysing store locations

Finding optimal location for a low-cost supermarket in Madrid

Photo by gemma on Unsplash

Background

Opening a physical store is not without risks. One the most obvious risks to evaluate before opening a new store is receiving enough customers to make the business profitable. Therefore it is essential to pick the right location to make sure it is convenient for the customers and there is enough demand for a new store in the area.

Identifying the target customers is also key taking into account the behaviour of the different groups of consumers and the stores they usually buy at.

Business problem

In this project, we tackle the problem of a low-cost supermarket chain trying to decide in which area of Madrid (Spain) they should open their new store in order to maximise the revenue. It is important to note that the city of Madrid consists of 21 districts and 131 neighbourhoods with great differences between them.

The goal is to identify the optimal neighbourhood for opening a store taking different factors into consideration such as the types of neighbourhood (a residential area would be ideal), the amount of people living in those areas (the higher the population the higher the food demand), their average income (working class people are preferred) and the stores that are already available (avoiding areas with a big density of supermarkets).

Using data science, geospatial analysis and machine learning techniques, this project aims to provide a solution for this problem and recommending the best neighbourhood for opening the low-cost supermarket.

Photo by Waldemar Brandt on Unsplash

Data

The following sections describe the data that is needed for answering this business question.

Wikipedia

The first data that we need is the list of neighbourhoods in Madrid. The Wikipedia page “List of neighbourhoods of Madrid” shows a table with the name of each neighbourhood for each of the 21 districts. In our project, we will work directly with the neighbourhoods and ignore the districts since this way we can perform a more granular analysis of the areas.

Image from Wikipedia

Geospatial data

Since the plan is to target residential areas, we need to analyse the type of food venues present in each neighbourhood. With the Foursquare API we can explore the different food venues, considering that a big density of bars and restaurants over very few supermarkets will most likely refer to a business or recreational area where people don’t usually buy at supermarkets. In the other hand, a large proportion of supermarkets over the rest of food venues might indicate it is a residential area where people normally make their food shopping.

Before we can make use of the Foursquare API we need to convert the neighbourhood names into a pair of latitude and longitude coordinates. We can acquire this with the Geocoder Python library.

We can query the Foursquare API using the HTTP GET method on the explore endpoint indicating the geographical coordinates, venue categories and radius.

Image from Foursquare

Census data

Finally we will need data from the census of Madrid. We can obtained this data from Excel files that are accessible from the Madrid city council website. Particularly we are interested in the population and the average household income of each neighbourhood.

Image extracted from Madrid city council

Image extracted from Madrid city council

Data acquisition

The following sections describe the different stages for acquiring the needed data.

Scrapping Wikipedia

We start by scrapping the Wikipedia HTML to obtain the list of neighbourhoods of Madrid, as follows:

Scrapping Wikipedia for the list of neighbourhoods of Madrid

This above code produces the following dataframe with only one column of 131 neighbourhoods:

df_madrid

Getting geographical coordinates for the neighbourhoods

We can obtain the latitude and longitude coordinates for the above neighbourhoods by using Geocoder:

Getting latitude and longitude coordinates for Madrid neighbourhoods

The above code creates geographical columns for our dataframe:

df_madrid

Map of Madrid neighbourhoods

Now that we have the geographical coordinates of all the neighbourhoods we can represent them on the city map of Madrid:

Displaying map of Madrid neighbourhoods

As we can see in the map below, only the areas of the city have been considered, ignoring the surrounding towns.

map_madrid

Requesting Foursquare API for geospatial data

With the geographical coordinates we can send requests to the Foursquare API to obtain the list of venues in each neighbourhood. We are particularly interested in the category of the venues since this will help us discriminate the different types of areas:

Getting venues for each neighbourhood

A new dataframe of venues is created with the geographical location, category and neighbourhood they belong to:

madrid_venues

In order to detect the type of each neighbourhood we need to find out the most popular venues in those areas. A first step consists in finding the frequency of each venue category, we can use a one hot encoding approach:

Finding frequency of each type of venue in each neighbourhood

The following dataframe holds the frequency of each venue type for each of the 131 neighbourhoods:

madrid_grouped

Loading census data

The detailed population of Madrid can be obtained from Madrid city countil website, downloading an Excel file and loading into a dataframe. Note that we are only keeping only the necessary columns (neighbourhood and population).

df_madrid_population

The household income information can be downloaded from Madrid city council website. Again an Excel file is loaded into a dataframe as below:

df_madrid_income

Neighbourhood segmentation

Since initially we don’t know how many different types of neighbourhood we can find in Madrid, we are using the elbow method to obtain the optimal number (k) of clusters:

Finding the optimal number of neighbourhood clusters

Although 3 seems the most optimal value as per diagram below, we are using 5 (the second best k) since this way we can break down more the number of neighbourhoods that we are going to analyse against the census data.

Elbow method for finding optimal K

With K-means algorithm we can group the 131 neighbourhoods into 5 clusters depending on the most popular venues in those areas:

Finding clusters of neighbourhoods by k-means algorithm

Below we can visualise what are the most common venues and the cluster (from 0 to 4) that has been assigned to each area.

madrid_merged

The map below represents the neighbourhoods on the city map again but this time a color code represents the cluster of each area.

Map of Madrid with 5 different clusters of neighbourhoods

We can examine the most popular venues of each cluster in order to assign a name that best represents each of them.

Examining popular venues of Cluster 0

Popular venues in neighbourhoods of Cluster 0

The following table summarises the type of neighbourhood that can be found in each cluster:

Types of neighbourhood in each cluster

Since we want to target residential areas only, we discard any neighbourhoods that are not included in cluster 2. Then we can send another request to Foursquare API to count the number of supermarkets that already exist in those residential areas.

Counting the number of existing markets in each residential neighbourhood

Census analysis

Before we can analyse the census data we need to join the three dataframes (residential neighbourhoods, household income and population) into one single dataframe. The next step is to calculate the ratio population — number of supermarkets (the more people per market the safer it is to open a new business).

Joining residential neighbourhoods, population and household income dataframes

Finally we can draw a 2D scatterplot with the residential neighbourhoods considering the ratio people — markets and the household income. The highlighted quadrant covers neighbourhoods with a household income below average (remember we are targeting working class areas) and a ratio of people per market above average, making these neighbourhoods good candidates for the new store.

Drawing scatterplot of residential neighbourhoods with household income vs people per market

Household income vs people per market for residential neighbourhoods

Results and discussion

In the above diagram, the highlighted area in green represents the most adequate areas to open a low-cost supermarket since their household income is below average and also their ratio of people per market is above average.

In particular, we can clearly see that Villaverde Alto looks the optimal neighbourhood since the household income is clearly under average (around 26K €) and there are very few supermarkets for the amount of population in the area.

However it would also be recommended to consider other similar areas such as Portazgo that is also located on the bottom-right corner of the plot.

It is also interesting to mention that even though the low amount of existing supermarkets in Mirasierra does clearly not cover the demand of the neighbourhood, it would be a convenient place for a high-class market since the household income there is at the very top.

Conclusion

The neighbourhoods of Madrid were analysed with the purpose of finding the ideal location for a new low-cost supermarket. We applied machine learning techniques such as k-means clustering to find different clusters so that we could focus in only one type of neighbourhood (residential). Further data such as population and market venues have been used to reduce the number of potential areas.

This project could be improved by only taking certain venue categories into consideration when performing the clustering segmentation. We could for example identify the key types of venue that define a residential area such as schools, pharmacies, small markets and corner shops, and the types of venue that discard a residential area such as night clubs, theaters and so on. Another improvement could be achieved by only handling certain groups of ages and social classes that would normally shop in a supermarket.

Although this project focuses particularly in a low-cost supermarket, it could easily be amended for any type of business and city, as long as the corresponding data are available to be included in the analysis.

References

Notebook created by Emilio Macias for the ‘Applied Data Science Capstone’ course on Coursera.

To Access the Jupyter Notebook click here.