728x90
아래 코드는 데이터 전처리 과정이 생략되어 있습니다.
Prophet함수를 어떻게 사용하는지에 관한 부분과 그 결과가 어떻게 생겨나는지 파악하는 정도로 이해하려고 썼습니다.
더욱 자세한 내용은 참고 사이트를 통해 Prophet기능을 숙지하면 큰 도움이 될 것입니다.
In [ ]:
# import prophet
!pip install prophet
from prophet import Prophet
Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/
Requirement already satisfied: prophet in /usr/local/lib/python3.8/dist-packages (1.1.1)
Requirement already satisfied: cmdstanpy>=1.0.4 in /usr/local/lib/python3.8/dist-packages (from prophet) (1.0.8)
Requirement already satisfied: numpy>=1.15.4 in /usr/local/lib/python3.8/dist-packages (from prophet) (1.21.6)
Requirement already satisfied: setuptools>=42 in /usr/local/lib/python3.8/dist-packages (from prophet) (57.4.0)
Requirement already satisfied: pandas>=1.0.4 in /usr/local/lib/python3.8/dist-packages (from prophet) (1.3.5)
Requirement already satisfied: tqdm>=4.36.1 in /usr/local/lib/python3.8/dist-packages (from prophet) (4.64.1)
Requirement already satisfied: setuptools-git>=1.2 in /usr/local/lib/python3.8/dist-packages (from prophet) (1.2)
Requirement already satisfied: wheel>=0.37.0 in /usr/local/lib/python3.8/dist-packages (from prophet) (0.38.4)
Requirement already satisfied: convertdate>=2.1.2 in /usr/local/lib/python3.8/dist-packages (from prophet) (2.4.0)
Requirement already satisfied: holidays>=0.14.2 in /usr/local/lib/python3.8/dist-packages (from prophet) (0.18)
Requirement already satisfied: matplotlib>=2.0.0 in /usr/local/lib/python3.8/dist-packages (from prophet) (3.2.2)
Requirement already satisfied: LunarCalendar>=0.0.9 in /usr/local/lib/python3.8/dist-packages (from prophet) (0.0.9)
Requirement already satisfied: python-dateutil>=2.8.0 in /usr/local/lib/python3.8/dist-packages (from prophet) (2.8.2)
Requirement already satisfied: pymeeus<=1,>=0.3.13 in /usr/local/lib/python3.8/dist-packages (from convertdate>=2.1.2->prophet) (0.5.12)
Requirement already satisfied: korean-lunar-calendar in /usr/local/lib/python3.8/dist-packages (from holidays>=0.14.2->prophet) (0.3.1)
Requirement already satisfied: hijri-converter in /usr/local/lib/python3.8/dist-packages (from holidays>=0.14.2->prophet) (2.2.4)
Requirement already satisfied: pytz in /usr/local/lib/python3.8/dist-packages (from LunarCalendar>=0.0.9->prophet) (2022.7)
Requirement already satisfied: ephem>=3.7.5.3 in /usr/local/lib/python3.8/dist-packages (from LunarCalendar>=0.0.9->prophet) (4.1.4)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib>=2.0.0->prophet) (3.0.9)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.8/dist-packages (from matplotlib>=2.0.0->prophet) (1.4.4)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.8/dist-packages (from matplotlib>=2.0.0->prophet) (0.11.0)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.8/dist-packages (from python-dateutil>=2.8.0->prophet) (1.15.0)
- StateHoliday: indicates a state holiday. Normally all stores, with few exceptions, are closed on state holidays. Note that all schools are closed on public holidays and weekends. a = public holiday, b = Easter holiday, c = Christmas, 0 = None
- SchoolHoliday: indicates if the (Store, Date) was affected by the closure of public schools
Prophet 함수 만들기
In [ ]:
# Prophet 요구사항: 날짜와 판매액 열의 이름을 ds와 y로 바꾸기
def sales_predictions(store_ID, sales_df, holidays, periods):
sales_df = sales_df[sales_df['Store'] == store_ID]
sales_df = sales_df[['Date','Sales']].rename(columns = {'Date':'ds','Sales':'y'})
sales_df = sales_df.sort_values('ds')
model = Prophet(daily_seasonality=True, holidays = holidays)
model.fit(sales_df)
future = model.make_future_dataframe(periods = periods) # 미래의 데이터 프레임 형성
forecast = model.predict(future)
figure = model.plot(forecast, xlabel = 'Date', ylabel = 'Sales')
figure2 = model.plot_components(forecast)
휴일에 대한 날짜 데이터 프레임 형성하기
In [ ]:
# Get all the dates pertaining to school holidays
school_holidays = sales_all_df[sales_all_df['SchoolHoliday'] == 1].loc[:,'Date'].values
In [ ]:
school_holidays.shape
Out[ ]:
(163457,)
In [ ]:
sales_all_df['StateHoliday'].unique()
Out[ ]:
array(['0', 0, 'a', 'b', 'c'], dtype=object)
In [ ]:
# Get all the dates pertaining to state holidays
state_holidays = sales_all_df[(sales_all_df['StateHoliday'] == 'a') | (sales_all_df['StateHoliday'] == 'b') | (sales_all_df['StateHoliday'] == 'c') ].loc[:,'Date'].values
In [ ]:
state_holidays.shape
Out[ ]:
(910,)
In [ ]:
state_holidays = pd.DataFrame({'ds': pd.to_datetime(state_holidays),
'holiday': 'state_holiday'})
In [ ]:
school_holidays = pd.DataFrame({'ds': pd.to_datetime(school_holidays),
'holiday': 'school_holiday'})
In [ ]:
# concatenate both school and state holidays
school_state_holidays = pd.concat([state_holidays, school_holidays])
Prophet
In [ ]:
# Let's make predictions using holidays for a specific store
# 6번 매장의 미래 90일간의 매출 예측
sales_predictions(6, sales_all_df, school_state_holidays, 90)
DEBUG:cmdstanpy:input tempfile: /tmp/tmptset8rva/8vnj5g57.json
DEBUG:cmdstanpy:input tempfile: /tmp/tmptset8rva/sry8kjo_.json
DEBUG:cmdstanpy:idx 0
DEBUG:cmdstanpy:running CmdStan, num_threads: None
DEBUG:cmdstanpy:CmdStan args: ['/usr/local/lib/python3.8/dist-packages/prophet/stan_model/prophet_model.bin', 'random', 'seed=11234', 'data', 'file=/tmp/tmptset8rva/8vnj5g57.json', 'init=/tmp/tmptset8rva/sry8kjo_.json', 'output', 'file=/tmp/tmptset8rva/prophet_modelte8mhaag/prophet_model-20230125121358.csv', 'method=optimize', 'algorithm=lbfgs', 'iter=10000']
12:13:58 - cmdstanpy - INFO - Chain [1] start processing
INFO:cmdstanpy:Chain [1] start processing
12:13:58 - cmdstanpy - INFO - Chain [1] done processing
INFO:cmdstanpy:Chain [1] done processing
728x90
'Data Analytics with python > [Data Science for Business]' 카테고리의 다른 글
FACEBOOK PROPHET (1) | 2023.01.25 |
---|
댓글