Transforming Categorical Data for Usability in Machine Learning Predictions
By: Kevin Rexis Velasco
Tags: Students, Machine Learning, Linear Regression, Data Science, Housing

Why location is important to our dataset

We have all heard the overused phrase ‘location, location, location’ as a central theme in real estate market assessment. It is wildly believed to be the most important factor in determining where one should live. Price and location often times go hand in hand with buyers. The condition, price, and size of any home can change. The one constant is the home’s location.

Take for example the zip code feature of our dataset:

In its current form, with 70 unique categorical values in ‘zipcode’ column, a machine learning model cannot extract any of the useful information contained within each zip code to assess a relationship to price. Within each of these categories exists a unique selection of parks, schools, cafes, shops, grocery stores, and access to transit and major roadways, just to name a few. All major factors that can indicate implicit differences in the Wallingford neighborhood of Seattle vs. a home out in the middle of the Yakima Valley, for example. Unlocking the intrinsic qualities of a zip code in regards to price is important to incorporate into our price predicting model, so we had to adapt to maintain the power of the category.

A quick glance at the information above shows it was obvious that we needed to engineer new features to make sense of the zip code, as well as the latitude and longitude!

Transforming & Comprehending Categorical Data

Our approach involved creating a feature to measure the distance from the major economic centers of Bellevue and Seattle, using their respective latitudes and longitudes, in order to assess home price in the data set. Rather than re-inventing the wheel by creating a mathematical function, we decided to calculate distance via the Haversine formula illustrated below:

Example of the Haversine function

The haversine module (https://pypi.org/project/haversine/) saved us plenty of time.

The formula we used takes in two latitude and longitude tuples and calculates the geographic distance between the two. We hypothesized that there would be a significant relationship with distances from Seattle and Bellevue city centers because they are the biggest hubs of jobs & economic activity in the area.

For the formula, we needed to transform for the coordinates of each house. Given the latitude and longitude, we applied the zip function to create a new column of the coordinates:

With single points for each home in the data set, as well as two points of reference, all the variables for the haversine formula were created. Applying the function directly into a new column into the existing Dataframe proved to be tricky. In order to create a new column indicating distances, we had to create a Pandas Series from a list datatype to then add to the existing Dataframe.

Lastly, we created yet another column which selected the minimum distance between both points to input for the “distance_from_epicenter” column. This step was important to capture the information of proximity to both cities to ensure our interpretation of the shortest distance to an economic hub was ready for our model to interpret.