Skip to content

Instantly share code, notes, and snippets.

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)
X = sm.add_constant(X)
olsr_model = OLS(endog=y, exog=X)
olsr_results =
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)')
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])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment