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.
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
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
xtrain_set, xtest_set, ytrain_set, ytest_set = train_test_split(housing.median_income, housing.median_house_value, test_size=0.2)
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
lin_reg.fit(np.array(xtrain_set).reshape(-1,1), ytrain_set)
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=None, normalize=False)
print("Enter Your Income") salary=float(input("Income: "))
Enter Your Income Income: 7.2574
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
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
result = predictions-ytest_set
plt.hist(result)
(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>)