Site icon Machine Learning Projects

House Tax Prediction using Random Forest – Boston Housing Data – with source code – 2023

Machine Learning Projects

In this blog, we will be performing House Tax Prediction using the Random Forest algorithm. We will be using the very famous Boston Housing Data for this problem. So without further due, Let’s do it…

Checkout the video here – https://youtu.be/xNIvhWZzoew

Step 1 – Importing required packages.

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.metrics import r2_score

%matplotlib inline

Step 2 – Reading our data.

data = pd.read_csv('HousingData.csv')
data.head()
Our input data

Step 3 – Describe our data.

data.describe()
Description of data

Step 4 – Check the info of our data.

data.info()

Step 5 – Filling null values.

col = ['CRIM','ZN','INDUS','CHAS','AGE','LSTAT']
for c in col:
    data[c].fillna(data[c].mean(),inplace=True)

Step 6 – Now again check the info of our data.

data.info()

Step 7 – Check the correlation of our target field ‘TAX’ with other features.

data.corr()['TAX'].sort_values(ascending=False)
correlation of ‘TAX’ with other features

Step 8 – Preprocessing our data.

from sklearn.preprocessing import StandardScaler

X = data.drop('TAX',axis=1)
y = data['TAX']

scaler = StandardScaler()
X = scaler.fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

Step 9 – Training our House Tax Prediction model.

from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import GridSearchCV

rfc = RandomForestRegressor()
params = {'n_estimators':[100,200,300,400,500,600,700,800,900,1000]}

grid_model = GridSearchCV(rfc, params,verbose=2)
grid_model.fit(X_train,y_train)

pred = grid_model.predict(X_test)

print('Random Forest accuracy is --> ',r2_score(y_test,pred)*100)

Step 10 -Checking the best parameters for the House Tax Prediction model.

grid_model.best_params_
best parameters

Step 11 – Just watch the results.

res = pd.DataFrame()
res['Y_Test'] = y_test
res['PRED'] = pred
res.head()

Step 12 – Plotting out the results of House Tax Prediction.

sns.scatterplot(y_test,pred)
plt.xlabel('real values')
plt.ylabel('predicted values')
final plot between predicted values and real values

Download Source Code for House Tax Prediction…

Do let me know if there’s any query regarding House Tax Prediction by contacting me on email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Read my previous post: PEDESTRIAN DETECTION USING HOGS IN PYTHON

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net.

Exit mobile version