dibend commited on
Commit
49525d0
1 Parent(s): fbea4cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -2,9 +2,10 @@ import gradio as gr
2
  import pandas as pd
3
  import plotly.graph_objects as go
4
  from sklearn.linear_model import LinearRegression
5
- from datetime import timedelta
 
6
 
7
- def plot_and_predict(zip, prediction_days):
8
  # Read and process the real estate data from Zillow
9
  df = pd.read_csv('https://files.zillowstatic.com/research/public_csvs/zhvi/Zip_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv')
10
  df = df[df['RegionName'] == int(zip)]
@@ -14,16 +15,16 @@ def plot_and_predict(zip, prediction_days):
14
  df['Date'] = pd.to_datetime(df['Date'])
15
 
16
  # Train linear regression model
17
- df['Timestamp'] = (df['Date'] - pd.Timestamp("1970-01-01")) // pd.Timedelta('1D')
18
- X = df['Timestamp'].values.reshape(-1, 1)
19
  y = df['Price'].values
20
  model = LinearRegression()
21
  model.fit(X, y)
22
 
23
  # Predict future prices
24
- last_timestamp = df['Timestamp'].iloc[-1]
25
- future_timestamps = [last_timestamp + i for i in range(1, prediction_days + 1)]
26
- predicted_prices = model.predict(pd.np.array(future_timestamps).reshape(-1, 1))
27
 
28
  # Prepare data for plotting
29
  historical_prices_trace = go.Scatter(
@@ -32,7 +33,7 @@ def plot_and_predict(zip, prediction_days):
32
  mode="lines",
33
  name="Historical Prices"
34
  )
35
- future_dates = [df['Date'].iloc[-1] + timedelta(days=i) for i in range(1, prediction_days + 1)]
36
  predicted_prices_trace = go.Scatter(
37
  x=future_dates,
38
  y=predicted_prices,
@@ -58,7 +59,7 @@ interface = gr.Interface(
58
  fn=plot_and_predict,
59
  inputs=[
60
  gr.Textbox(label="ZIP Code"),
61
- gr.Slider(minimum=1, maximum=365, step=1, label="Prediction Days"),
62
  ],
63
  outputs="plot"
64
  )
 
2
  import pandas as pd
3
  import plotly.graph_objects as go
4
  from sklearn.linear_model import LinearRegression
5
+ import numpy as np
6
+ from pandas.tseries.offsets import MonthEnd
7
 
8
+ def plot_and_predict(zip, prediction_months):
9
  # Read and process the real estate data from Zillow
10
  df = pd.read_csv('https://files.zillowstatic.com/research/public_csvs/zhvi/Zip_zhvi_uc_sfrcondo_tier_0.33_0.67_sm_sa_month.csv')
11
  df = df[df['RegionName'] == int(zip)]
 
15
  df['Date'] = pd.to_datetime(df['Date'])
16
 
17
  # Train linear regression model
18
+ df['MonthsSinceStart'] = np.arange(len(df))
19
+ X = df['MonthsSinceStart'].values.reshape(-1, 1)
20
  y = df['Price'].values
21
  model = LinearRegression()
22
  model.fit(X, y)
23
 
24
  # Predict future prices
25
+ last_month_index = df['MonthsSinceStart'].iloc[-1]
26
+ future_months = np.array([last_month_index + i for i in range(1, prediction_months + 1)]).reshape(-1, 1)
27
+ predicted_prices = model.predict(future_months)
28
 
29
  # Prepare data for plotting
30
  historical_prices_trace = go.Scatter(
 
33
  mode="lines",
34
  name="Historical Prices"
35
  )
36
+ future_dates = [df['Date'].iloc[-1] + MonthEnd(i) for i in range(1, prediction_months + 1)]
37
  predicted_prices_trace = go.Scatter(
38
  x=future_dates,
39
  y=predicted_prices,
 
59
  fn=plot_and_predict,
60
  inputs=[
61
  gr.Textbox(label="ZIP Code"),
62
+ gr.Slider(minimum=1, maximum=60, step=1, label="Prediction Months"),
63
  ],
64
  outputs="plot"
65
  )