Create and show a plot of the Mean Model and a Linear Regression Model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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