In [278]:
import pandas as pd
import numpy as np
from fbprophet import Prophet
실제함수 input
In [280]:
rawArrayDatas=[["2017-08-11", "2017-08-12", "2017-08-13", "2017-08-14", "2017-08-15","2017-08-16"],
[20.0, 30.0, 40.0, 50.0, 60.0,20.0]]
processId=12
forecastDay=4
In [281]:
In [281]:
mockForecast={}
rmse={}
forecast=[]
realForecast={}
trainSize=int(len(rawArrayDatas[0]) * 0.7)
testSize=len(rawArrayDatas[0])-trainSize
In [282]:
print(trainSize)
print(testSize)
전체 data 전처리
In [283]:
ds = rawArrayDatas[0]
y = list(np.log(rawArrayDatas[1]))
sales = list(zip(ds, y))
In [284]:
rawArrayDatas
In [285]:
sales
Out[285]:
In [286]:
print(type(ds), type(y))
In [287]:
preprocessedData= pd.DataFrame(data = sales, columns=['ds', 'y'])
In [288]:
preprocessedData
Out[288]:
In [289]:
model = Prophet()
model.fit(preprocessedData)
Out[289]:
In [289]:
future = model.make_future_dataframe(periods=forecastDay)
forecast = future[-forecastDay:]
In [296]:
# Python
forecast = model.predict(future)
forecast[['ds', 'yhat']].tail()
Out[296]:
In [296]:
forecast
Out[296]:
In [297]:
forecast
In [298]:
model.plot(forecast)
In [299]:
forecastData= [np.exp(y) for y in forecast['yhat'][-forecastDay:]]
In [300]:
In [305]:
print(forecastData)
In [306]:
data=rawArrayDatas[1]+forecastData
In [307]:
data
Out[307]:
In [265]:
In [200]:
ans=np.log10(10)
In [173]:
ans
Out[173]:
In [174]:
np.exp(ans)
Out[174]:
In [ ]:
In [ ]:
In [177]:
data= [np.exp(y) for y in forecast['yhat']]
In [178]:
print(data)
In [165]:
date= [d.strftime('%Y-%m-%d') for d in forecast['ds']]
In [159]:
date
Out[159]:
In [129]:
dateStamp = list(forecast['ds'][-forecastDay:])
dateStamp
date = [p.strftime('%Y-%m-%d') for p in dateStamp]
date
Out[129]:
In [130]:
realForecast['Bayseian'] = Bayseian(preprocessedData=XY, forecastDay=forecastDay)[0]
In [99]:
realForecast
Out[99]:
In [100]:
In [42]:
XY = PrepareBayseian(rawArrayDatas)
In [43]:
Bayseian(preprocessedData=XY, forecastDay=forecastDay)[0]
Out[43]:
In [33]:
LearningModuleRunner(rawArrayDatas, processId, forecastDay)
In [40]:
realForecast
Out[40]:
In [49]:
type(realForecast['Bayseian'])
Out[49]:
In [ ]:
In [ ]:
In [2]:
import sys
print(sys.version)
In [ ]:
In [63]:
#data 준비위한 하드코딩
#rawArrayDatas
df0=pd.read_csv('./data/397_replace0with1.csv')
df0['y'] = np.log(df0['y'])
ds=df0['ds']
y=df0['y']
In [4]:
#processId
processId=1
#정식적인 data input과정
rawArrayDatas=[['2016-01-01','2016-01-02','2016-01-03','2016-01-04','2016-01-05'],[10,10,12,13,14]]
ds=rawArrayDatas[0]
y=rawArrayDatas[1]
sales=list(zip(ds,y))
day=5
In [150]:
sales
Out[150]:
In [ ]:
In [143]:
rawArrayDatas[:][:2]
Out[143]:
In [144]:
rawArrayDatas=[['2016-01-01','2016-01-02','2016-01-03','2016-01-04','2016-01-05'],[10,10,12,13,14]]
ds=rawArrayDatas[0]
y=rawArrayDatas[1]
In [147]:
sales = list(zip(rawArrayDatas[0], rawArrayDatas[1]))
In [146]:
y
Out[146]:
In [148]:
sales
Out[148]:
In [124]:
ds=rawArrayDatas[0]
#-->year, month, dayOfWeek 추출
year=[2016,2016, 2016, 2017]
month=[1,1,1,1]
dayOfWeek=[1,2,3,4]
y=rawArrayDatas[1][:len(train)]
In [ ]:
ds=rawArrayDatas[0]
#-->year, month, dayOfWeek 추출
year=np.random.beta(2000, 2017, len(train))*(2017-2000)
month=np.random.beta(1, 12, len(train))*(12-1)
dayOfWeek=np.random.beta(0, 6, len(train))*(6-0)
y=rawArrayDatas[1][:len(train)]
In [152]:
year
Out[152]:
In [153]:
month
Out[153]:
In [154]:
dayOfWeek
Out[154]:
In [155]:
sales=list(zip(year, month, dayOfWeek, y))
In [156]:
sales
Out[156]:
In [157]:
x = pd.DataFrame(data = sales, columns=['year', 'month', 'dayOfWeek','y'])
In [158]:
x
Out[158]:
In [125]:
y
Out[125]:
In [108]:
np.size(y)
Out[108]:
In [126]:
x = pd.DataFrame(data = sales, columns=['year', 'month', 'dayOfWeek','y'])
x['month']
Out[126]:
In [66]:
type(rawArrayDatas)
Out[66]:
In [127]:
x
Out[127]:
In [ ]:
In [62]:
type(x)
Out[62]:
In [49]:
list(df)
Out[49]:
In [ ]:
In [34]:
#값에 0이 있으면 log를 할 때 inf가 되므로 Initialization failed. 오류가 나니 주의할 것.
m = Prophet()
m.fit(df);
In [53]:
future = m.make_future_dataframe(periods=day)
future.tail()
Out[53]:
In [54]:
future[-day:]
Out[54]:
In [159]:
future
Out[159]:
In [36]:
forecast=m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
Out[36]:
In [166]:
temp=list(forecast['ds'][-day:])
date=[p.strftime('%Y-%m-%d') for p in temp]
In [169]:
date=[p.strftime('%Y-%m-%d') for p in temp]
In [170]:
date
Out[170]:
In [ ]:
In [ ]:
In [ ]:
strftime('We are the %d, %b %Y')
In [37]:
m.plot(forecast)
Out[37]:
In [38]:
m.plot_components(forecast)
Out[38]:
In [ ]:
In [ ]:
In [ ]: