House prediction model — linear regression

using linear regression we will make a model that will predict house prices (with source code)

Aviral Bhardwaj
4 min readJun 18, 2021

as in the previous article, I have given you an introduction to linear regression now I will tell you how to make a basic linear regression model in this article with some lines of codes.
so let’s start

the first step is we need to download the dataset and then apply the dataset to the model.

you can download or copy data from the url -

importing the libraries

now we will import pandas and NumPy as given below if your system has not installed these libraries you can download by pip command.

import pandas as pd

import numpy as np

data preparation

now we will read the data by pandas and give it a name house so we don’t need to call it again and again, by the head command we can see the first 5 elements of the data if you wish to see more you can enter the number inside the bracket.

url = ‘https://raw.githubusercontent.com/aviralb13/codes/main/datas/house_prediction(lr).csv'

house = pd.read_csv(url)

house.head()

the columns will tell you about all the columns data is divided into.

house.columns

now we will need some features that decide the prices of our house so we are creating a list of particular items we will add to the list and the second variable is price and we named these variables as x and y (you can add your own features if you like to ).

features= [‘condition’,’grade’,’yr_built’,’floors’,’sqft_living’]

x = house[features]

y = house[‘price’]

making the model

now we will import the linear regression model from sklearn and test the train split module.

from sklearn.linear_model import LinearRegression

import matplotlib.pyplot as plt

from sklearn.model_selection import train_test_split

now we will split our data into 2 parts train and test, train dataset will train the model and the test will be calculating the model score we now can fit our train dataset into the model by the following commands.

train_x, val_x, train_y, val_y = train_test_split(x,y)

linear_model = LinearRegression()

linear_model.fit(train_x,train_y)

predicting from the model

now we need to import the mean absolute error module which will calculate the errors in the model. mean absolute error is a measure of errors between paired observations expressing the same phenomenon.

from sklearn.metrics import mean_absolute_error

val_predictions = linear_model.predict(val_x)

print(mean_absolute_error(val_y, val_predictions))

and by the predict command, you can see we can give any value to the model to know the prices so as here you can see I have enter 3,7,1951,2.0,2570this means.

condition — 3
grade — 7
year built — 1951
floors — 2
sqft living — 2570

now the model is predicting that the expected selling price of the house is
625440.43748829$.

and if you can see I have entered these particular features from house number one and now we can see what is the actual price of the house so the actual price of our house is 538000.0$.

still, the value is very close but it’s not accurate so in the next few articles I will show you how to improve models accuracy and we can similarly predict some more features to know the prices of their houses you can go check the link for full code.

conclusion

in the article I have given you information and codes on how to make a simple linear regression model with source code I would be making more exciting projects for you so stay connected

--

--

Aviral Bhardwaj

One of the youngest writer and mentor on AI-ML & Technology.