aiqtech commited on
Commit
baea38f
·
verified ·
1 Parent(s): 2af2718

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -3
app.py CHANGED
@@ -8,9 +8,10 @@ import plotly.graph_objects as go
8
 
9
  def predict_future_prices(ticker, periods=1825):
10
  data = download_data(ticker)
11
-
12
  def download_data(ticker, start_date='2010-01-01'):
13
- """ 주식 데이터를 다운로드하고 포맷을 조정하는 함수 """
 
 
14
  data = yf.download(ticker, start=start_date)
15
  if data.empty:
16
  raise ValueError(f"No data returned for {ticker}")
@@ -24,31 +25,37 @@ def download_data(ticker, start_date='2010-01-01'):
24
 
25
  def predict_future_prices(ticker, periods=1825):
26
  data = download_data(ticker)
 
27
  # Prophet 모델 생성 및 학습
28
  model_prophet = Prophet(daily_seasonality=False, weekly_seasonality=False, yearly_seasonality=True)
29
  model_prophet.fit(data)
 
30
  # 미래 데이터 프레임 생성 및 예측
31
  future = model_prophet.make_future_dataframe(periods=periods, freq='D')
32
  forecast_prophet = model_prophet.predict(future)
 
33
  # Linear Regression 모델 생성 및 학습
34
  model_lr = LinearRegression()
35
  X = pd.to_numeric(pd.Series(range(len(data))))
36
  y = data['y'].values
37
  model_lr.fit(X.values.reshape(-1, 1), y)
 
38
  # 미래 데이터 프레임 생성 및 예측
39
  future_dates = pd.date_range(start=data['ds'].iloc[-1], periods=periods+1, freq='D')[1:]
40
  future_lr = pd.DataFrame({'ds': future_dates})
41
  future_lr['ds'] = future_lr['ds'].dt.strftime('%Y-%m-%d')
42
  X_future = pd.to_numeric(pd.Series(range(len(data), len(data) + len(future_lr))))
43
  future_lr['yhat'] = model_lr.predict(X_future.values.reshape(-1, 1))
 
44
  # 예측 결과 그래프 생성
45
  forecast_prophet['ds'] = forecast_prophet['ds'].dt.strftime('%Y-%m-%d')
46
  fig = go.Figure()
47
  fig.add_trace(go.Scatter(x=forecast_prophet['ds'], y=forecast_prophet['yhat'], mode='lines', name='Prophet Forecast (Blue)'))
48
  fig.add_trace(go.Scatter(x=future_lr['ds'], y=future_lr['yhat'], mode='lines', name='Linear Regression Forecast (Red)', line=dict(color='red')))
49
  fig.add_trace(go.Scatter(x=data['ds'], y=data['y'], mode='lines', name='Actual (Black)', line=dict(color='black')))
 
50
  return fig, forecast_prophet[['ds', 'yhat', 'yhat_lower', 'yhat_upper']], future_lr[['ds', 'yhat']]
51
-
52
  css = """footer { visibility: hidden; }"""
53
 
54
  with gr.Blocks(css=css) as app:
 
8
 
9
  def predict_future_prices(ticker, periods=1825):
10
  data = download_data(ticker)
 
11
  def download_data(ticker, start_date='2010-01-01'):
12
+ """
13
+ 주식 데이터를 다운로드하고 포맷을 조정하는 함수
14
+ """
15
  data = yf.download(ticker, start=start_date)
16
  if data.empty:
17
  raise ValueError(f"No data returned for {ticker}")
 
25
 
26
  def predict_future_prices(ticker, periods=1825):
27
  data = download_data(ticker)
28
+
29
  # Prophet 모델 생성 및 학습
30
  model_prophet = Prophet(daily_seasonality=False, weekly_seasonality=False, yearly_seasonality=True)
31
  model_prophet.fit(data)
32
+
33
  # 미래 데이터 프레임 생성 및 예측
34
  future = model_prophet.make_future_dataframe(periods=periods, freq='D')
35
  forecast_prophet = model_prophet.predict(future)
36
+
37
  # Linear Regression 모델 생성 및 학습
38
  model_lr = LinearRegression()
39
  X = pd.to_numeric(pd.Series(range(len(data))))
40
  y = data['y'].values
41
  model_lr.fit(X.values.reshape(-1, 1), y)
42
+
43
  # 미래 데이터 프레임 생성 및 예측
44
  future_dates = pd.date_range(start=data['ds'].iloc[-1], periods=periods+1, freq='D')[1:]
45
  future_lr = pd.DataFrame({'ds': future_dates})
46
  future_lr['ds'] = future_lr['ds'].dt.strftime('%Y-%m-%d')
47
  X_future = pd.to_numeric(pd.Series(range(len(data), len(data) + len(future_lr))))
48
  future_lr['yhat'] = model_lr.predict(X_future.values.reshape(-1, 1))
49
+
50
  # 예측 결과 그래프 생성
51
  forecast_prophet['ds'] = forecast_prophet['ds'].dt.strftime('%Y-%m-%d')
52
  fig = go.Figure()
53
  fig.add_trace(go.Scatter(x=forecast_prophet['ds'], y=forecast_prophet['yhat'], mode='lines', name='Prophet Forecast (Blue)'))
54
  fig.add_trace(go.Scatter(x=future_lr['ds'], y=future_lr['yhat'], mode='lines', name='Linear Regression Forecast (Red)', line=dict(color='red')))
55
  fig.add_trace(go.Scatter(x=data['ds'], y=data['y'], mode='lines', name='Actual (Black)', line=dict(color='black')))
56
+
57
  return fig, forecast_prophet[['ds', 'yhat', 'yhat_lower', 'yhat_upper']], future_lr[['ds', 'yhat']]
58
+
59
  css = """footer { visibility: hidden; }"""
60
 
61
  with gr.Blocks(css=css) as app: