House Price Prediction with Python

Vaghela Lagdhir
5 min readOct 29, 2021

Predicting house prices can help to determine the selling price of a house in a particular region and can help people to find the right time to buy a home. In this article, I will introduce you to a machine learning project on house price prediction with Python.

House Price Prediction

In this task on House Price Prediction using machine learning, our task is to use data from the California census to create a machine learning model to predict house prices in the State. The data includes features such as population, median income, and median house prices for each block group in California.

Block groups are the smallest geographic unit which typically has a population of 600 to 3,000 people. We can call them districts for short. Ultimately, our machine learning model should learn from this data and be able to predict the median house price in any neighbourhood, given all other metrics.

House Price Prediction with Python

I hope you have understood the above problem statement about predicting house prices. Now, I will take you through a machine learning project on House Price prediction with Python. Let’s start by importing the necessary Python libraries and the dataset:

Each row represents a district and there are 10 attributes in the dataset. Now let’s use the info() method which is useful for getting a quick description of the data, especially the total number of rows, the type of each attribute, and the number of non-zero values:

There are 20,640 instances in the dataset. Note that the total_bedrooms attribute has only 20,433 non-zero values, which means 207 districts do not contain values. We will have to deal with that later.

All attributes are numeric except for the ocean_proximity field. Its type is an object, so it can contain any type of Python object. You can find out which categories exist in that column and how many districts belong to each category by using the value_counts() method:

Another quick way to get a feel for what kind of data you’re dealing with is to plot a histogram for each numerical attribute:

The next step in this task of House Price Prediction is to split the data into training and test sets. Creating a test set is theoretically straightforward: select some instances at random, typically 20% of the dataset (or less if your dataset is very large), and set them aside:

Let’s take a closer look at the histogram of median income, as most median income values cluster around 1.5 to 6, but some median income goes well beyond 6.

It is important to have a sufficient number of instances in your dataset for each stratum, otherwise, the estimate of the importance of a stratum may be biased. This means that you should not have too many strata and that each stratum should be large enough:

Stratified Sampling on Dataset

Now the next step is to perform some stratified sampling on the dataset.For this we can use the StratifiedShuffleSplit class of Scikit-Learn:

Now we need to remove the Income_cat attribute added by us to get the data back to its form:

Now before creating a machine learning model for house price prediction with Python let’s visualize the data in terms of longitude and latitude:

The graph shows house prices in California where red is expensive, blue is cheap, larger circles indicate areas with a larger population.

Finding Correlations

Since the dataset is not too large, we can easily calculate the standard correlation coefficient between each pair of attributes using the corr() method:

Correlation ranges are between -1 and 1. When it is close to 1 it means that there is a positive correlation and when it is close to -1 it means that there is a negative correlation. When it is close to 0, it means that there is no linear correlation.

Another way to check the correlation between attributes is to use the pandas scatter_matrix() function, which plots each numeric attribute against every other numeric attribute:

And now let’s look at the correlation matrix again by adding three new columns to the dataset; rooms per household, bedrooms per room and population per household:

Data Preparation

Now, this is the most important step before a train a machine learning model for the task of house price prediction. Now let’s perform all the necessary data transformations:

As we can see, there are many data transformation steps that need to be performed in the correct order. Fortunately, Scikit-Learn provides the Pipeline class to help you with such sequences of transformations. Here is a small pipeline for numeric attributes:

Linear Regression for House Price Prediction with Python

Now I will use the linear regression algorithm for the task of house price prediction with Python:

I hope you liked this article on the Machine Learning project on House Price Prediction with Python.

Thank You!!!

--

--