import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/danielcaraway/data/master/Zip_Zhvi_SingleFamilyResidence.csv', encoding='latin')
CA = df[df.State == 'CA']
# CA.CountyName.value_counts()
LA = CA[CA.CountyName == 'Los Angeles County']
LA.City.value_counts()
glendale = LA[LA.City == 'Glendale']
glendale.RegionName.value_counts()
# LA.groupby('City')[].mean()
mine = LA[LA.RegionName == 90039]
df = mine.copy()
df_t = df.loc[:, '1996-04'::].T
df_t.columns = ['average home price']
from matplotlib import pyplot
df_t.plot()
from fbprophet import Prophet
import logging
logging.getLogger().setLevel(logging.ERROR)
df = df_t.copy()
df.reset_index(inplace=True)
df.columns = ['ds','y']
df.head()
len(df)
prediction_size = 57
train_df = df[:-prediction_size]
m = Prophet()
m.fit(train_df)
future = m.make_future_dataframe(periods=prediction_size)
forecast = m.predict(future)
forecast.head()
m.plot(forecast)
prediction_size = 100
train_df = df[:-prediction_size]
m = Prophet()
m.fit(train_df)
future = m.make_future_dataframe(periods=prediction_size)
forecast = m.predict(future)
forecast.head()
m.plot(forecast)
fig2 = m.plot_components(forecast)
prediction_size = 12
train_df = df[:-prediction_size]
m = Prophet()
m.fit(train_df)
future = m.make_future_dataframe(periods=prediction_size)
forecast = m.predict(future)
forecast.head()
m.plot(forecast)
fig2 = m.plot_components(forecast)
# from fbprophet.plot import plot_plotly
# import plotly.offline as py
# py.init_notebook_mode()
# fig = plot_plotly(m, forecast) # This returns a plotly Figure
# py.iplot(fig)
len(LA)
# def graph_prices_for(df, location_name):
# df_t = df.loc[:, '1996-04'::].T
# df_t['avg'] = df_t.mean(numeric_only=True, axis=1)
# df_t.reset_index(inplace=True)
# columns = ['index', 'avg']
# df = pd.DataFrame(df_t, columns = columns)
# df = df.rename(index=str, columns={"avg": "y", "index": "ds"})
# ax = df.set_index('ds').plot(figsize=(12, 8))
# ax.set_ylabel('Home Prices in ' + location_name)
# ax.set_xlabel('Date')
# plt.show()
def transform(df, location_name):
df_t = df.loc[:, '1996-04'::].T
df_t['avg'] = df_t.mean(numeric_only=True, axis=1)
df_t.reset_index(inplace=True)
columns = ['index', 'avg']
df = pd.DataFrame(df_t, columns = columns)
df = df.rename(index=str, columns={"avg": "y", "index": "ds"})
df['place'] = location_name
return df
# def transform(df):
# mine = LA[LA.RegionName == 90039]
def do_the_prophit(zip):
df = LA[LA.RegionName == zip]
df_t = transform(mine, zip)
prediction_size = 12
train_df = df_t[:-prediction_size]
m = Prophet()
m.fit(train_df)
future = m.make_future_dataframe(periods=prediction_size)
forecast = m.predict(future)
forecast.head()
m.plot(forecast)
fig2 = m.plot_components(forecast)
return forecast, future
forecast, future = do_the_prophit(90039)
forecast
future