This article is going to be different from the rest of my articles published on Analytics Vidhya – both in terms of content and format. I usually layout my article such that after a read, the reader is left to think about how this article can be implemented on grounds.
In this article, I will start with a round of brainstorming around a particular type of business problem and then talk about a sample analytics based solution to these problems. To make use of this article make sure that you follow my instructions carefully.
Let’s start with a few business cases:
What is so common in all the problems mentioned above? Each of these problems deal with getting the distance between multiple combination of source and target destinations.
Exercise : Think about at-least 2 such cases in your current industry and then at least 2 cases outside your current industry and write them in the comment section below.
I have worked in multiple domains and saw this problem being solved in similar fashion which gives approximate but quick results.
Exercise : Can you think of a method to do the same using your currently available data and resources?
Here is the approach :
You generally have a PIN CODE for both source and destination. Using these PIN CODES, we find the centroid of these regions. Once you have both the centroids, you check their latitude and longitude. You finally calculate the eucledian distance between these two points. We approximate our required distance with this number. Following figure will explain the process better :
The two marked areas refers to different PIN CODES and the distance 10 kms is used as an approximate distance between the two points.
Exercise : Can you think of challenges with this approach ?
Here are a few I can think of :
Say you have two branches and a single customer, how will you make a call between the two branches (which one is closer)? Here is a step by step approach :
Obviously, this process cannot be done manually for millions of customers and thousands of branches. But this process can be well automated (however, Google API have a few caps on the total number of searches). Here is a simple Python code which can be used to create functions to calculate the distance between two points on Google Map.
Exercise : Create a table with a few sources and destinations. Use these functions to find distance and time between those points. Reply “Done without support” if you are able to implement the code without looking at the rest of the solution.
Here is how we can read in a table of different source-destination combinations :
Notice that we have all types of combinations here. Combination 1 is a combo of two cities. Combo 4 is a combination of two detailed address. Combo 6 is a combination of a city and a monument. Let’s now try to get the distances and time & check if they make sense.
All the distance and time calculations in this table look accurate.
Exercise : What are the benefits of using this approach over the PIN CODE approach mentioned above? Can you think of a better way to do this task?
Here is the complete Code :
[stextbox id=”grey”]import googlemaps from datetime import datetime
def finddist(source, destination): gmaps = googlemaps.Client(key='XXX') now = datetime.now() directions_result = gmaps.directions(source, destination, mode="driving",departure_time=now) for map1 in directions_result: overall_stats = map1['legs'] for dimensions in overall_stats: distance = dimensions['distance'] return [distance['text']] def findtime(source, destination): gmaps = googlemaps.Client(key='XXX') now = datetime.now() directions_result = gmaps.directions(source, destination, mode="driving",departure_time=now) for map1 in directions_result: overall_stats = map1['legs'] for dimensions in overall_stats: duration = dimensions['duration'] return [duration['text']]
import numpy as np import pandas as pd import pylab as pl import os os.chdir(r"C:\Users\Tavish\Desktop") cities = pd.read_csv("cities.csv")
cities["distance"] = 0 cities["time"] = 0 for i in range(0,8): source = cities['Source'][i] destination = cities['Destination'][i] cities['distance'][i] = finddist(source,destination) cities['time'][i] = findtime(source,destination) [/stextbox]
GoogleMaps API come with a few limitations on the total number of searches. You can have look at the documentation, if you see a use case of this algorithm.
Did you find the article useful? Share with us find more use cases of GoogleMaps API usage apart from the one mentioned in this article? Also share with us any links of related video or article to leverage GoogleMaps API. Do let us know your thoughts about this article in the box below.
hello, sir, I need help regarding my 8th-semester project can you please give me your contact mail.
Hi, In Minkowski Distance, how to determine the value of 'p'? Thanks in advance Sagar
Hello Pulkit, This comment refers to your 'Step-by-Step Deep Learning Tutorial to Build your own Video Classification Model' blog post. I could not seem to add a comment under the post, so i will add it here. First, I will love to thank you for the step-by-step guide to Video classification. However, I don't think you performed video classification. You preprocessed the videos into frames, which was great but you modelled your problem as an image classification and not video classification problem. I think a video classification problem requires processing the frames in sequence i.e., sequence learning. The main difference between video classification and image classification is to capture the temporal or dynamic characteristics between the different frames of a video. However, i dont see this in the way you modelled your data or your model. So i think its better to title the post "step-by-step deep learning tutorial to build an image classification model from videos". Hope this helps. Kind regards,