파이썬 이것저것/파이썬 데이터분석

[파이썬] ProPhet 활용하여 시계열 예측

agingcurve 2022. 4. 23. 17:41
반응형

# Sin 그래프를 그리는 함수 만들어보기

sin 그래프 추정식

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

 

def plotSinWave(amp, freq, endTIme, smapleTime, startTime, bias):
  """
  plot sin wave
  y = a sin(2 pi f t + t_0) + b
  """
  time = np.arange(startTime, endTIme, smapleTime)
  result = amp * np.sin(2 * np.pi * freq * time + startTime) + bias

  plt.figure(figsize=(12, 6))
  plt.plot(time, result)
  plt.grid(True)
  plt.xlabel("time")
  plt.ylabel("sin")
  plt.title(str(amp) + "*sin(2*pi" + str(freq) + "*t+" + str(startTime) + ")+" + str(bias))
  plt.show()
  plotSinWave(1, 1, 10, 0.01, 0.5, 0)
 

 

(**kwargs) keyword argument를 활용하여함수 생성

def plotSinWave(**kwargs):
  """
  plot sin wave
  y = a sin(2 pi f t + t_0) + b
  """
  amp = kwargs.get("amp",1)
  freq = kwargs.get("freq", 1)
  endTime = kwargs.get("endTIme", 1)
  smapleTime = kwargs.get("smapleTime", 0.01)
  startTime = kwargs.get("startTime", 0)
  bias = kwargs.get("bias", 0)
  figsize = kwargs.get("figsize", (12, 6))

  time = np.arange(startTime, endTime, smapleTime)
  result = amp * np.sin(2 * np.pi * freq * time + startTime) + bias

  plt.figure(figsize=(12, 6))
  plt.plot(time, result)
  plt.grid(True)
  plt.xlabel("time")
  plt.ylabel("sin")
  plt.title(str(amp) + "*sin(2*pi" + str(freq) + "*t+" + str(startTime) + ")+" + str(bias))
  plt.show()
  
  plotSinWave()

인자를 설정하지 않았을때 default값이 나오게 된다

 

plotSinWave(amp = 2, freq = 0.5 , endTime = 10)

 

 

# ProPhet를 실험하기 위한 시계열 데이터 생성
 
time = np.linspace(0, 1, 365 * 2)
result = np.sin(2*np.pi * 12 * time)
ds = pd.date_range("2018-01-01", periods = 365 * 2, freq= "D")
df = pd.DataFrame({"ds" : ds, "y":result})
df

 

df["y"].plot(figsize=(10, 8))

 

from fbprophet import Prophet
m = Prophet(yearly_seasonality= True, daily_seasonality=True)
m.fit(df)

-> fbprophet을 설치, 연계절성과 일계절성설정하여 예측해봄

 

future = m.make_future_dataframe(periods=30)
forecast = m.predict(future)

m.plot(forecast)

예측데이터가 생성되었다.

 

# 시간별로 증가 추세를 보이는 데이터를 생성
 
time = np.linspace(0, 1, 365 * 2)
result = np.sin(2*np.pi * 12 * time) + time
ds = pd.date_range("2018-01-01", periods=365 * 2, freq="D")

df = pd.DataFrame({"ds" : ds, "y": result})
df["y"].plot(figsize=(10, 6))

m = Prophet(yearly_seasonality=True, daily_seasonality=True)
m.fit(df)
future = m.make_future_dataframe(periods=30)
forecast = m.predict(future)
m.plot(forecast)

 

# 잡읍이 들어가는 데이터를 생성
time = np.linspace(0, 1, 365 * 2)
result = np.sin(2*np.pi * 12 * time) + time + np.random.randn(365*2) /4 
ds = pd.date_range("2018-01-01", periods=365 * 2, freq="D")

df = pd.DataFrame({"ds" : ds, "y": result})
df["y"].plot(figsize=(10, 6))

m = Prophet(yearly_seasonality=True, daily_seasonality=True)
m.fit(df)
future = m.make_future_dataframe(periods=30)
forecast = m.predict(future)
m.plot(forecast)

-> 잡음이 있는데이터도 잘 생성이 된다.