Clustering Taxi Geolocation Data To Predict Location of Taxi Service Stations (Pt 1)- The Beginning
Tags: Dbscan, Machine Learning, K Means Clustering, Geolocation, Knn Algorithm

This article is a step by step guide for ‘Clustering Taxi Geolocation Data To Predict Location of Taxi Service Stations’.

This is quite a big topic to cover so I decided to divide it into two parts, this article being part 1.

For anyone and everyone who is new to this field, please do not let the topic scare you away. It is pretty easy once you get the hang of it. Just stick with me till the end, okay? This is going to be long (apologies in advance) .

Also, this is my first proper Machine Learning project. God knows I’ve been at this subject for as long as it took for Merlin’s beard to turn white.

Anyhoo, Coursera offers a wide range of guided projects. This was one of them. You can check it out right here:

https://www.coursera.org/courses?query=guided%20projects

Word. Make sure you spend a good amount of time understanding your project. Background reading on Google and making a personal report can work wonders.

What You Need To Know About ML

Machine Learning is the field of study that gives computers the capability to learn without being explicitly programmed. ML is one of the most exciting technologies that one would have ever come across. As it is evident from the name, it gives the computer that makes it more similar to humans: The ability to learn. Machine learning is actively being used today, perhaps in many more places than one would expect.

Supervised vs Unsupervised Machine Learning:

2. Regression vs Classification:

3. Clustering:

Prerequisites:

Problem Statement And Our Approach To Solve:

Taxi ranks are locations of taxis when they are on a standby. We need analyse their geolocation data to understand where to build service stations using clustering. A simple way is to build a service station at the center of each cluster that will serve every taxi belonging to that cluster. You will learn how to cluster initially with a K-Means approach, before using a more complicated density-based algorithm, DBSCAN. We will discuss how to evaluate these models, and offer improvements to DBSCAN with the introduction of HDBSCAN.

Getting Started:

I use Google Colab for coding.

Importing file from local system:

2. Importing libraries and colors (cols) that will be used in the project.

1. Exploratory Data Analysis

We read the data from the dataframe and check for any duplicate and null values. We have some duplicate and null values. Our next step is to remove them.

This is a skeleton code which helps us get a view of our data frame before and after these changes are made. We use df.head() to check if our data frame looks the same. Yes it does.

We extract our latitude and longitude values as a numpy array. Dtype is a kind of data we want to visualize our lats and lons as they are big numbers.

dt = np.dtype(‘float64’) # 64-bit floating-point number

And then we scatter plot X.

alpha : The alpha blending value, between 0 (transparent) and 1 (opaque). Helps is seeing which areas are densely populated in the graph.

: The alpha blending value, between 0 (transparent) and 1 (opaque). Helps is seeing which areas are densely populated in the graph. s : For the size of dots.

In the next visualization we are going to see the same scatter plot on a map. Because it is hard to understand how many little points belong in a region.

2. Visualizing Geographical Data

To plot nice interactive geographical data we use a nice package called Folium which is written in Python.

We need to give the location while writing the code so that Folium can process where we want to visualize our map initially. Since it takes the value of latitudes and longitudes, the easiest way for us will be to just take the mean of the lats and lons from the data frame that we already have. That is where our map will start. Then we give the zoom value and the tile value which is the color scheme for the map basically. And we run ‘m’ to get the map ready. (The output in Img1 is given by the first two lines of the code in Img2)

Img1

Next we add the data of the taxi ranks to the map and we can do that by iteratively adding all the rows and columns of the data frame. This is how folium works and will be easy for us in the future when we are giving a name to each cluster case.

There is _ used in the code because it returns us the tuple of the row number that we are looking at and the actual row information. We use a circle marker to plot the lats and lons. We give it a radius value as per our own convenience. And then a pop up value i.e what is the value that will pop up when we click on the circles. We also set colors by stating the hex values for that color. And then we set fill color to true and fill the circle with the same color because it looks good. We add the circle marker to m.

We run the code Img2 and again run ‘m’ to get Img2 Output as output.

Img2

Img2 Output

If we click on the small circles then we will see small pop ups of each taxi rank details individually. Also we can zoom in and see the locations of the highly densely populated dots.

Next we will be seeing how to determine the strength of the cluster.

3. Clustering Strength / Performance Metric

Here we will be testing the clustering algorithm and will see if by changing it there is an improvement in the clustering. We are going to start this segment by creating test data by using scikit-learn’s ‘make_blobs’ function. The make_blobs() function can be used to generate blobs of points with a Gaussian distribution. You can control how many blobs to generate and the number of samples to generate, as well as a host of other properties.

We use [:,0] is for all rows and 0th column of the array and [:,1] for all rows and1st column of the array.

There are four well defined clusters on the LHS and four on the RHS and one on the right side seems like a fusion of two clusters.

Now what we have to do is create individual clusters by running class prediction. (Class prediction is a supervised learning method where the algorithm learns from samples with known class membership (training set) and establishes a prediction rule to classify new samples (test set). This method can be used, for instance, to predict cancer types using genomic expression profiling)

And then distinguish them from each other by allocating colors (cols).

The output looks something like this-

Now we will use something called ‘silhouette score’. It is used to score the clustering algorithm. How it works is it will assign a score between -1 and 1 to the clustering algorithm and the closer it is to 1, the better. It checks how well clustered a single cluster is as compared to as to the center of an opposing cluster.

0: Means clusters are indifferent, or we can say that the distance between clusters is not significant.

-1: Means clusters are assigned in the wrong way.

Silhouette Score = (b-a)/max(a,b)

where

a= average intra-cluster distance i.e the average distance between each point within a cluster.

b= average inter-cluster distance i.e the average distance between all clusters.

The score function will take the test data X_blobs (an array) that we considered and the class_predictions.

We get a score of about 0.66 which is good. Now we will copy paste and run the above algorithm (named improved) once again just to see if the silhouette score actually worked.

We get a result of a plot which looks like this:

See?! We can now tell the overlapping clusters from each other. Isn’t this great?

Now we will check the silhouette score again to see if it has improved.

Bravo! It has! Keep going. You are doing great. We are almost done.

4. K-Means Clustering

Here we will look at our first clustering approach which is K means clustering. We run a few iterations using the K-means algorithm so that it learns how to cluster our data in an interactive way.

This is the interactive plot that we were talking about. From the dummy data we got this plot and clearly we can give the value of K=3 (the number of clusters). Also the plot above is the 10th iteration.

Now how K-means works is it iteratively finds each of the clusters a centroid. (understanding the math behind k-means — https://heartbeat.fritz.ai/understanding-the-mathematics-behind-k-means-clustering-40e1d55e2f4c)

The algorithm randomly assigned 3 centroids to the test data cluster (the black triangles).

Then it clusters the nearest data points near to this centroid and once that is done (like shown in the above plot) it re-clusters again iteratively and assigns a new centroid. Currently the x represents the iterations and with each one a new centroid is allocated iteratively (the black triangles will be moving slightly) and this is done until no changes are found. This is unsupervised learning and this is how the algo learns from the data.

It reassigns its points to the nearest centroid cluster which is why we see the red yellow and green dots pushing over each other with increasing iterations.

Here is how the 1st and the 20th iteration look like.

We will continue coding.

The ‘k’ value is normally based on intuition and easy to guess with 2D data but not with other higher dimensional data.

Lets assign k=70. And then we model it with some parameters. We use fit(x) which is to change the weights ( coefficients of input variables) so that they fit the training data set. And also assigned a column in the dataframe. Common error: do not forget to redefine X in the code.

Then we call df.head() to see what the first five cluster predictions acc to the k means algo that we used. They came out fine.

We now have to visualize this on the map like earlier. We need to call a function to create a map and that should include the dataframe in question along with the cluster columns. We should also assign a cluster color.

When we run the code we get the k value and the silhouette score. Then we can run m in the next kernel to get a visualization of the map.

This code is written to iterate for the various values of ‘k’ to see how our data works and find the best possible silhouette score range for this k value. We can adjust the value of the number of clusters we want to have but then again it has got its own limitations. We are sticking to k=70 and we will see in the next phase why it is a good value for our data.

The final silhouette score is 0.697 which is an improvement from our previous score of 0.637 (scroll up and check it out above the map). The map came out just fine too but there is still room for improvement.

Don’t you feel like points on the map look too crowded in the center? Well, that’s maybe because K-Means does not work too well with such highly populated data.

The taxis are very dense in the center of the city and their population slowly narrows down as we move towards the outskirts. We need more taxi service stations in such populated areas and obviously present model will be very hard to work with. We need to other approaches to improve our current model.

We will talk more about the alternate approaches in my next article.