my code stock.com

aravindh ram

Housing pricing prediction using linear regression
by aravindh ram

Snippet options

Download: Download snippet as housing-pricing-prediction-using-linear-regression.py.
Copy snippet: For this you need a free my code stock.com account.
Embed code : You will find the embed code for this snippet at the end of the page, if you want to embed it into a website or a blog!

# 
# The data contains the following columns:
# 
# * 'Avg. Area Income': Avg. Income of residents of the city house is located in.
# * 'Avg. Area House Age': Avg Age of Houses in same city
# * 'Avg. Area Number of Rooms': Avg Number of Rooms for Houses in same city
# * 'Avg. Area Number of Bedrooms': Avg Number of Bedrooms for Houses in same city
# * 'Area Population': Population of city house is located in
# * 'Price': Price that the house sold at
# * 'Address': Address for the house




import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')




USAhousing = pd.read_csv('USA_Housing.csv')




USAhousing.head()




USAhousing.info()




USAhousing.describe()




USAhousing.columns




sns.pairplot(USAhousing)




sns.distplot(USAhousing['Price'])




sns.heatmap(USAhousing.corr())





X = USAhousing['Avg. Area Number of Bedrooms'].values.reshape(-1,1)
pd.options.display.float_format = '{:,.2f}'.format
y = USAhousing['Price'].values




from sklearn.model_selection import train_test_split




X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.4, random_state=101)





from sklearn.linear_model import LinearRegression




lm = LinearRegression()




lm.fit(X_train,y_train)




y_train_pred=lm.predict(X_train)
y_test_pred=lm.predict(X_test)




plt.figure(figsize=(12,8))
plt.scatter(y_train_pred, y_train_pred - y_train, c='blue', marker='o', label='Training data')
plt.scatter(y_test_pred, y_test_pred - y_test, c='orange', marker='*', label='Test data')
plt.xlabel('Predicted values')
plt.ylabel('Residuals')
plt.legend(loc='upper left')
plt.xlim([1.593866e+04, 6.593866e+04])
plt.ylim([-5, 35])
plt.show()


print(lm.intercept_)




coeff_df = pd.DataFrame(lm.coef_,X.columns,columns=['Coefficient'])
coeff_df




plt.scatter(y_test,predictions)





sns.distplot((y_test-predictions),bins=50);



from sklearn import metrics




print('MAE:', metrics.mean_absolute_error(y_test, predictions))
print('MSE:', metrics.mean_squared_error(y_test, predictions))
print('RMSE:', np.sqrt(metrics.mean_squared_error(y_test, predictions)))



Create a free my code stock.com account now.

my code stok.com is a free service, which allows you to save and manage code snippes of any kind and programming language. We provide many advantages for your daily work with code-snippets, also for your teamwork. Give it a try!

Find out more and register now

You can customize the height of iFrame-Codes as needed! You can find more infos in our API Reference for iframe Embeds.