Skip to content

Instantly share code, notes, and snippets.

@sachinsdate
Created March 5, 2021 00:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save sachinsdate/bf8bff0ca22a47647ad732ed297e1aea to your computer and use it in GitHub Desktop.
Create and show a plot of the Mean Model and a Linear Regression Model
import pandas as pd
from matplotlib import pyplot as plt
from statsmodels.regression.linear_model import OLS as OLS
import statsmodels.api as sm
df = pd.read_csv('taiwan_real_estate_valuation_curated.csv', header=0)
y = df['HOUSE_PRICE_PER_UNIT_AREA']
X = df['HOUSE_AGE_YEARS']
X = sm.add_constant(X)
olsr_model = OLS(endog=y, exog=X)
olsr_results = olsr_model.fit()
y_pred = olsr_results.predict()
fig = plt.figure()
fig.suptitle('Real estate valuation against house age (years)')
plt.xlabel('House Age (years)')
plt.ylabel('House price (10000 New Taiwan Dollar/Ping)')
plt.scatter(df['HOUSE_AGE_YEARS'], df['HOUSE_PRICE_PER_UNIT_AREA'])
mean_y = df['HOUSE_PRICE_PER_UNIT_AREA'].mean()
mean_model, = plt.plot([0.0, 20.0], [mean_y, mean_y], color='orange', linewidth=2, label='Mean Model')
linear_model, = plt.plot(df['HOUSE_AGE_YEARS'], y_pred, marker='o', linestyle='dashed', linewidth=1, markersize=6, color='red', label='OLS Model')
plt.legend(handles=[mean_model, linear_model])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment