시간에 흐름에 대해 특정 패턴과 같은 정보를 가지고 있는 경우
conda install pandas-datareader
pip install pystan
conda install plotly
conda install -c conda-forge fbprophet
pip install fbprophet
※ 안될 시
pip install prophet
from pandas_datareader import data
from prophet import Prophet
test_df
)과 입력 인자(a, b
)를 정해준다return
)을 작성def test_def(a, b):
return a + b
test_def(2, 1)
a = 1 # 전역변수
def edit_a(i):
global a #
a = i
edit_a(2)
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
def plotSinWave(amp, freq, endTime, sampleTime, startTime, bias):
"""
plot sine wave
y = a sin(2 pi f t + t_0) + b
"""
time = np.arange(startTime, endTime, sampleTime)
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(2, 1, 10, 0.01, 0.5, 0)
def plotSinWave(**kwargs):
"""
plot sine wave
y = a sin(2 pi f t + t_0) + b
"""
endTime = kwargs.get("endTime", 1)
sampleTime = kwargs.get("sampleTime", 0.01)
amp = kwargs.get("amp", 1)
freq = kwargs.get("freq", 1)
startTime = kwargs.get("startTime", 0)
bias = kwargs.get("bias", 0)
figsize = kwargs.get("figsize", (12, 6))
time = np.arange(startTime, endTime, sampleTime)
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()
plotSinWave(amp=2, freq=0.5, endTime=10)
%%writefile ./drawSinWave.py # drawSinWave.py을 만들겠다
import numpy as np
import matplotlib.pyplot as plt
def plotSinWave(**kwargs):
"""
plot sine wave
y = a sin(2 pi f t + t_0) + b
"""
endTime = kwargs.get("endTime", 1)
sampleTime = kwargs.get("sampleTime", 0.01)
amp = kwargs.get("amp", 1)
freq = kwargs.get("freq", 1)
startTime = kwargs.get("startTime", 0)
bias = kwargs.get("bias", 0)
figsize = kwargs.get("figsize", (12, 6))
time = np.arange(startTime, endTime, sampleTime)
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()
if __name__ == "__main__":
print("hello world~!!")
print("this is test graph!!")
plotSinWave(amp=1, endTime=2)
import drawSinWave as dS
dS.plotSinWave()
%%writefile ./set_matplotlib_hangul.py
import platform
import matplotlib.pyplot as plt
from matplotlib import font_manager, rc
path = "c:/Windows/Fonts/malgun.ttf"
if platform.system() == "Darwin":
print("Hangul OK in your MAC!!!")
rc("font", family="Arial Unicode MS")
elif platform.system() == "Windows":
font_name = font_manager.FontProperties(fname=path).get_name()
print("Hangul OK in your Windows!!!")
rc("font", family=font_name)
else:
print("Unknown system.. sorry~~~")
plt.rcParams["axes.unicode_minus"] = False
import set_matplotlib_hangul
plt.title("한글")
※ 안될 경우
conda install -c conda-forge prophet # conda 이용해서 한 번 더 설치
Module Load
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
테스트 데이터 생성
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.head()
df["y"].plot(figsize=(10, 6));
from pandas_datareader import data
from prophet import Prophet
m = Prophet(yearly_seasonality = True, daily_seasonality = True)
m.fit(df)
Output :
11:59:35 - cmdstanpy - INFO - Chain [1] start processing 11:59:35 - cmdstanpy - INFO - Chain [1] done processing
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) # 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 # noise 추가
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);