Engineering Location Features with Haversine's Formula for Prediction Modeling
Tags: python, linearregression, machinelearning, distancecalculation
Published: 2019-04-19 23:29:11

---
title: Engineering Location Features with Haversine's Formula for Prediction Modeling
published: true
description: 
tags: python, linear regression, machine learning, distance calculation
---

Here is the full git repo: [Predicting King Co home prices](https://github.com/nkacoroski/dsc-1-final-project#module-1-final-project) This blog post is just a snippet of one strategy we used.
*Co-written with [Natasha Kacoroski](https://github.com/nkacoroski)*

So you'd like to build a prediction model from some data, but some of the columns seem a bit . . . useless? Alternatively, the column of data might accidentally infer meaning where there shouldn't be any. Let's try this out by engineering some columns with location data to help predict the price of a home using a small slice of the King County Housing Data set from Washington State. Let's explore how we can turn difficult columns into something useful instead of just throwing them away or using them incorrectly.


```python
import pandas as pd

df = pd.read_csv('kc_house_data.csv')
df.head().T
```




<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>0</th>
      <th>1</th>
      <th>2</th>
      <th>3</th>
      <th>4</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>id</th>
      <td>7129300520</td>
      <td>6414100192</td>
      <td>5631500400</td>
      <td>2487200875</td>
      <td>1954400510</td>
    </tr>
    <tr>
      <th>date</th>
      <td>10/13/2014</td>
      <td>12/9/2014</td>
      <td>2/25/2015</td>
      <td>12/9/2014</td>
      <td>2/18/2015</td>
    </tr>
    <tr>
      <th>price</th>
      <td>221900</td>
      <td>538000</td>
      <td>180000</td>
      <td>604000</td>
      <td>510000</td>
    </tr>
    <tr>
      <th>bedrooms</th>
      <td>3</td>
      <td>3</td>
      <td>2</td>
      <td>4</td>
      <td>3</td>
    </tr>
    <tr>
      <th>bathrooms</th>
      <td>1</td>
      <td>2.25</td>
      <td>1</td>
      <td>3</td>
      <td>2</td>
    </tr>
    <tr>
      <th>sqft_living</th>
      <td>1180</td>
      <td>2570</td>
      <td>770</td>
      <td>1960</td>
      <td>1680</td>
    </tr>
    <tr>
      <th>sqft_lot</th>
      <td>5650</td>
      <td>7242</td>
      <td>10000</td>
      <td>5000</td>
      <td>8080</td>
    </tr>
    <tr>
      <th>floors</th>
      <td>1</td>
      <td>2</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
    </tr>
    <tr>
      <th>waterfront</th>
      <td>NaN</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>view</th>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>condition</th>
      <td>3</td>
      <td>3</td>
      <td>3</td>
      <td>5</td>
      <td>3</td>
    </tr>
    <tr>
      <th>grade</th>
      <td>7</td>
      <td>7</td>
      <td>6</td>
      <td>7</td>
      <td>8</td>
    </tr>
    <tr>
      <th>sqft_above</th>
      <td>1180</td>
      <td>2170</td>
      <td>770</td>
      <td>1050</td>
      <td>1680</td>
    </tr>
    <tr>
      <th>sqft_basement</th>
      <td>0.0</td>
      <td>400.0</td>
      <td>0.0</td>
      <td>910.0</td>
      <td>0.0</td>
    </tr>
    <tr>
      <th>yr_built</th>
      <td>1955</td>
      <td>1951</td>
      <td>1933</td>
      <td>1965</td>
      <td>1987</td>
    </tr>
    <tr>
      <th>yr_renovated</th>
      <td>0</td>
      <td>1991</td>
      <td>NaN</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <th>zipcode</th>
      <td>98178</td>
      <td>98125</td>
      <td>98028</td>
      <td>98136</td>
      <td>98074</td>
    </tr>
    <tr>
      <th>lat</th>
      <td>47.5112</td>
      <td>47.721</td>
      <td>47.7379</td>
      <td>47.5208</td>
      <td>47.6168</td>
    </tr>
    <tr>
      <th>long</th>
      <td>-122.257</td>
      <td>-122.319</td>
      <td>-122.233</td>
      <td>-122.393</td>
      <td>-122.045</td>
    </tr>
    <tr>
      <th>sqft_living15</th>
      <td>1340</td>
      <td>1690</td>
      <td>2720</td>
      <td>1360</td>
      <td>1800</td>
    </tr>
    <tr>
      <th>sqft_lot15</th>
      <td>5650</td>
      <td>7639</td>
      <td>8062</td>
      <td>5000</td>
      <td>7503</td>
    </tr>
  </tbody>
</table>
</div>



What do each of these columns mean?


```python
from IPython.display import display, Markdown

with open('column_names.md', 'r') as fh:
    content = fh.read()

display(Markdown(content))
```


COLUMN NAMES AND DESCRIPTIONS FOR KING COUNTRY DATA SET
* **id** - unique identified for a house
* **dateDate** - house was sold
* **pricePrice** -  is prediction target
* **bedroomsNumber** -  of Bedrooms/House
* **bathroomsNumber** -  of bathrooms/bedrooms
* **sqft_livingsquare** -  footage of the home
* **sqft_lotsquare** -  footage of the lot
* **floorsTotal** -  floors (levels) in house
* **waterfront** - House which has a view to a waterfront
* **view** - Has been viewed
* **condition** - How good the condition is ( Overall )
* **grade** - overall grade given to the housing unit, based on King County grading system
* **sqft_above** - square footage of house apart from basement
* **sqft_basement** - square footage of the basement
* **yr_built** - Built Year
* **yr_renovated** - Year when house was renovated
* **zipcode** - zip
* **lat** - Latitude coordinate
* **long** - Longitude coordinate
* **sqft_living15** - The square footage of interior housing living space for the nearest 15 neighbors
* **sqft_lot15** - The square footage of the land lots of the nearest 15 neighbors



Many of these columns seem like they should have some kind of an influence on the price. In particular, the common saying "the three most important things to think about when buying are home are location, location, location" tells us we should probably care a lot about things like zipcode, latitude, and longitude. Also, just to note, 'sqft_living15' and 'sqft_lot15' are metadata about each house in the database that someone else has already engineered for us. Thanks, King Co. data scientist!

There are plenty of columns we could use, but for this tutorial with Haversine, let's just focus on latitude & longitude. Let's get a visual of what this data looks like.


```python
target = df['price'].copy()
needs_work = df[['long', 'lat']].copy()

import seaborn as sns
import matplotlib.pyplot as plt

for col in needs_work:
    x = needs_work[col]
    y = target
    
    plt.figure(figsize=(16,8))
    plt.subplot(1,2,1)
    sns.distplot(x)
    plt.subplot(1,2,2)
    
    sns.scatterplot(x, y)
    plt.suptitle(col)
    plt.tight_layout
    plt.savefig(col + '.png')
```

    



![png_of_long](https://github.com/nkacoroski/dsc-1-final-project/raw/blog/long.png)



![png_of_lat](https://github.com/nkacoroski/dsc-1-final-project/raw/blog/lat.png)


Even though these pairs of graphs look similar, notice the y-axes on these plots are measuring different things. The histogram is simply counting how many observations of the x-value we have in the data, while the scatterplot is showing the price in USD of the house sold at the x-value. It's almost like more houses sold (histogram height) correlates rather strongly with a higher sale price (scatterplot height). The Law of Supply & Demand seems alive and well in King County, WA. We'll discuss how to deal with this data in regard to predicting sale price using Haversine's 3D distance formula.

Before we get into the details of the formula, let's talk about why we should consider augmenting the location data at all. The problem here is that ONLY looking at one-dimensional movement on a map doesn't particularly generate accurate housing price patterns. Another way of saying this is there is no linear relationship between either latitude vs price nor longitude vs price. For example, if there are multiple wealthy neighborhoods that have lower-priced neighborhoods in between them, using just that one dimension of data won't accurately predict the value. In the following image (shamelessly lifted from http://www.peteryu.ca/tutorials/matlab/image_in_3d_surface_plot_with_multiple_colormaps) imagine the Z axis isn't elevation, but rather home price. As you can see in the heatmap below the surface, there isn't a clear linear pattern when you move along JUST the X-direction or just the Y-direction, but proximity to the dark red spot near the back right part of the graph *would* have a clear pattern for predicting price.
(Note the red spot near the bottom left should actually be blue on the heatmap, as in this example, it represents very low house prices.)

![image](http://www.peteryu.ca/_media/tutorials/matlab/image_surf_3d_basic.png)

When modeling with multivariable linear regression, simply having both latitude and longitude doesn't really help either. Instead, let's think about why home values in some neighborhoods are high vs low. It could simply be the houses in those neighborhoods have a view of the mountains & water, but the only way any one home sells for a lot of money is if someone pays a lot of money for it. I know this seems simplistic, but high home values require high paying jobs, and people with high paying jobs will pay more to be conveniently close their work (especially if the home has a view of the mountains or water). This leads me to believe a "radius from high-paying job cluster" might be a much better indicator of home value instead of using the two columns of 'longitude' and 'latitude' on their own. Let's build one! In fact, if there are multiple employment clusters, maybe building a single column for each "job hub" would make sense.

If you are willing to accept that we live on a round planet, we can utilize the Haversine formula, which measures 3D arc-length on the surface of a sphere. This is really just an adaption of the Pythagorean theorem.

We adapt the python code from:  https://stackoverflow.com/questions/4913349/haversine-formula-in-python-bearing-and-distance-between-two-gps-points/4913653


We choose to pass in two arguments, each in the form `[longitude, latitude]`. To do this, we first build a new column to serve as the input to the function.


```python
needs_work.head(2)
```




<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>long</th>
      <th>lat</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-122.257</td>
      <td>47.5112</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-122.319</td>
      <td>47.7210</td>
    </tr>
  </tbody>
</table>
</div>




```python
needs_work['long_lat'] = list(zip(needs_work['long'], needs_work['lat']))
needs_work.head(2)
```




<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>long</th>
      <th>lat</th>
      <th>long_lat</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>-122.257</td>
      <td>47.5112</td>
      <td>(-122.257, 47.5112)</td>
    </tr>
    <tr>
      <th>1</th>
      <td>-122.319</td>
      <td>47.7210</td>
      <td>(-122.319, 47.721000000000004)</td>
    </tr>
  </tbody>
</table>
</div>



Now we define the Haversine function. Alternatively, you can simply import it from https://pypi.org/project/haversine/ but we want to make sure we can control exactly what is going on and it's fun to see trigonometry in action!


```python
from math import radians, cos, sin, asin, sqrt

def haversine(list_long_lat, other=[-122.336283, 47.609395]):
    """
    Calculate the great circle distance between two points 
    on the earth (specified in decimal degrees), in this case the 2nd point is in the 
    Pike Pine Retail Core of Seattle, WA.
    """
    lon1, lat1 = list_long_lat[0], list_long_lat[1]
    lon2, lat2 = other[0], other[1]
    # convert decimal degrees to radians 
    lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
    # haversine formula 
    dlon = lon2 - lon1 
    dlat = lat2 - lat1 
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    c = 2 * asin(sqrt(a)) 
    # Radius of earth in kilometers is 6371
    km = 6371 * c
    return km
```

We're finally ready to build our useful output column using `series.apply()` which yields the distance to Seattle.


```python
needs_work['dist_to_seattle'] = needs_work['long_lat'].apply(haversine)
```

In fact, it would very easy to build another column for a different "job cluster location" at this same point by passing in the optional arguments for haversine() doing a different `series.apply(function, opt_arg=______)`. Seattle's "suburb" of Bellevue has actually grown into its own city with skyscrapers and high paying jobs of its own. Let's create the `'dist_to_bellevue'` column as well.


```python
needs_work['dist_to_bellevue'] = needs_work['long_lat'].apply(haversine, other=[-122.198985, 47.615577])
```

Finally, let's drop the columns we used as the building blocks for these two new columns. (Although instead of using `df.drop([columns], axis=1)` we actively select the columns to keep.)


```python
engineered_columns = needs_work.loc[:,['dist_to_seattle', 'dist_to_bellevue']]
```


```python
engineered_columns.head(10)
```




<div>

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>dist_to_seattle</th>
      <th>dist_to_bellevue</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>12.434278</td>
      <td>12.395639</td>
    </tr>
    <tr>
      <th>1</th>
      <td>12.477217</td>
      <td>14.770934</td>
    </tr>
    <tr>
      <th>2</th>
      <td>16.247460</td>
      <td>13.838051</td>
    </tr>
    <tr>
      <th>3</th>
      <td>10.731122</td>
      <td>17.970486</td>
    </tr>
    <tr>
      <th>4</th>
      <td>21.850148</td>
      <td>11.542868</td>
    </tr>
    <tr>
      <th>5</th>
      <td>25.361129</td>
      <td>15.217257</td>
    </tr>
    <tr>
      <th>6</th>
      <td>33.331870</td>
      <td>35.347235</td>
    </tr>
    <tr>
      <th>7</th>
      <td>22.284717</td>
      <td>24.515385</td>
    </tr>
    <tr>
      <th>8</th>
      <td>10.796605</td>
      <td>15.463271</td>
    </tr>
    <tr>
      <th>9</th>
      <td>35.274167</td>
      <td>30.244215</td>
    </tr>
  </tbody>
</table>
</div>



Now you can add these back into to your original dataframe and begin improving your model with these new feature columns.


```python

```