Basic Linear Regression Modeling in Python

For my course in Data Science, I performed a linear regression model on the sales of houses in King County. Data for sales in 2014 and 2015 was provided, and Python was used to do the modeling. This is a rundown of my first linear regression modeling project. For the full code, see the Github repo:

Business Case

It is important to establish a business case when starting a project like this. This is to create a goal and help move towards a meaningful conclusion. My last blog post’s project didn’t have any real goal, so it just stopped when I no longer felt curious. It’s better to start with a firm goal in mind, and build towards that. Here is the business case I used:

We will predict how much a house should be sold for in order to determine whether a house on the market is being underpriced or overpriced. Our clients are homeowners looking to sell their house, but do not know how much to sell their house for.

Data Cleaning

Both the data itself and a description of the columns was supplied. I read the data into a pandas dataframe to start cleaning it. After turning all the data into numerical data types, I handle any NaN values and create a few new features: ‘yr_since_renovation’, ‘yr_since_built’, and ‘renovated’. I then drop unneeded features: ‘view’, ‘sqft_above’, ‘sqft_living15’, ‘sqft_lot15’, and ‘date’.

Exploratory Data Analysis

With the data cleaned, I start to analyze it. Scatter plots of each variable against the target, price, show which variables have obvious linear relationships. Insignificant relationships will be taken care of when I start looking at the p-values of the coefficients, so they don’t need to be handled now. This also makes clearer which variables are categorical and which are continuous.

Histograms show which of variables are normally distributed. None of them are, so I will need to log transform them later.

I look at collinear features using a heatmap and decide to remove the ‘yr_built’, ‘yr_renovated’, ‘price’, ‘yr_sold’, and ‘yr_since_renovation’ columns.

Feature Engineering Baseline Model

In order to prepare for modeling, I need to create dummy variables of the categorical data. Those features are turned into strings from numerics in order to make the get_dummies method work. I also remove any punctuation that may be in the column names, as it may stop our model from working correctly.

At this point, I create the train-test split, using the sklearn method. I keep the default of 75% train and 25% test, and set a random state for repeatability.

With this, I can create the baseline model, using statsmodel.

The baseline model has the following metrics:

  • R2 of 0.821
  • Test RMSE of 139700
  • 81 significant features (p-value < 0.05) of 103 features total

Iterative Modeling Process

I created the following function to print out the metrics for any model, in order to easily ensure all steps taken improved the model.

To improve on our model, I took the following steps:

  • dropped collinear features, as determined during the eda
  • removed outliers from our datasets, more than 3 standard deviations above from the mean
  • log transformed applicable continuous features (no negative or zero values)
  • eliminated insignificant features (p-value < 0.05)

The final model has the following metrics:

  • R2 of 0.857
  • Test RMSE of 99701
  • 90 significant features (p-value < 0.05) of 90 features total

In addition to these metrics, I also plotted the residuals of the final model.

Finally, I save the model coefficients, so they can be analyzed and used to create a prediction function.

Final Model Analysis

By looking at the coefficients of the model, some interesting observations can be made.

  • each bedroom decreases the sale price of a house by 5%
  • each bathroom increases the sale price of a house by 6%
  • a 1% change in square footage living area increases the sale price of a house by .48%
  • a 1% change in square footage lot area increases the sale price of a house by .07%
  • if the house is on the waterfront, the sale price of a house increases by 60%
    a 1% change in square footage basement area decreases the sale price of a house by .00005%
  • if you move north, a 1 degree increase in latitude increases the sale price of a house by 65%
  • if you move east, a 1 degree increase in longitude decreases the sale price of a house by 53%
  • a 1-year increase in the age of a house increases its sale price by .04%
    a house that has been renovated has its sale price increased by 6%
  • using a one-floor house as a baseline, a 1.5-floor house has its price increased by 1.5%, while a 3-floor house has its price decreased by 6.4%. Other numbers of floors are approximately equal in price to a 1-floor house.
  • using a condition of 1 as a baseline, a condition of 2 increases the price by 19%, a condition of 3 increases the price of a house by 31%, a condition of 4 increases the price by 35%, and a condition of 5 increases the price by 40%


The model could be improved in the future by adding interactive features, polynomial features, and map data.

The final model will be useful in predicting sale prices of houses in King County. We can use these predictions to help our clients set the prices for their houses, and find houses that are currently underpriced.

Student of Data Science