• For any query, contact us at
  • +91-9872993883
  • +91-8283824812
  • info@ris-ai.com

House Budget Prediction with Python

This article explores how to use Python and finance together via a practical example.The housing market is very important for the financial stability. House Budget Prediction with Python can help a user to predict his/her budget to buy a home on the basis of salary earned.

While doing prediction, we will keep in mind some important factors such as person age , income ,house price etc.

We firstly import necessary library for this project

In [1]:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

House Budget Prediction with Python

Load the data from given CSV file

In [2]:
housing = pd.read_csv('housing.csv')

We take median_income and median_house_value column and divide them in two ration of 80 and 20% by giving test_size = 0.2 Which defines a and y trainset and x and y test_set

In [3]:
xtrain_set, xtest_set, ytrain_set, ytest_set = train_test_split(housing.median_income, housing.median_house_value, test_size=0.2)

Using lin_reg variable for linear regression call

In [4]:
lin_reg = LinearRegression()

By using LinearRegression we train our model to predict the model as we get xtrain_set value to be fit with ytrain_set value

In [5]:
lin_reg.fit(np.array(xtrain_set).reshape(-1,1), ytrain_set)
Out[5]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)

Here we take salary as an input to predict the price of the house

In [6]:
print("Enter Your Income")

salary=float(input("Income: "))
Enter Your Income
Income: 7.2574

Prediction of value using xtest and ytrain for salary entered

In [7]:
predictions = lin_reg.predict(np.array(salary).reshape(-1,1))

Here we get result of prediction of house price i.e. ytest_set on the bases of salary

In [8]:
print("you will buy home with approx rate of:" )
print(predictions)
you will buy home with approx rate of:
[348389.78708337]

Here we can check the difference between value which model predict and the ytest_set which is store in result variable

In [9]:
result = predictions-ytest_set

Visualization with histogram graph

In [10]:
plt.hist(result)
Out[10]:
(array([223., 105., 126., 246., 342., 579., 736., 800., 762., 209.]),
 array([-151611.21291663, -103361.11291663,  -55111.01291663,
          -6860.91291663,   41389.18708337,   89639.28708337,
         137889.38708337,  186139.48708337,  234389.58708337,
         282639.68708337,  330889.78708337]),
 <a list of 10 Patch objects>)
House Budget Prediction with Python

Resources You Will Ever Need