파이썬/라이브러리

딥러닝 : prophet으로 범죄율 예측하기

공부짱짱열심히하기 2023. 1. 3. 15:43

시카고 범죄 현황

 

먼저 Date 컬럼 가공

iso 표준 시간으로 바꿔주기

chicago_df['Date'] = pd.to_datetime(chicago_df['Date'] , format = '%m/%d/%Y %I:%M:%S %p')

인덱스 설정

https://seonggongstory.tistory.com/164

 

pandas: resample이용하여 Time Series 년도별,월별,일별 group화 하기

시카고의 범죄상황 데이터 프레임 여기서 년도별 시간별로 월별등 시간을 쪼개서 데이터를 가공하고 싶을때 그룹바이함수를 이용해서는, 날짜 데이터로 바로 년단위 ,월단위, 일단위, 시단위,

seonggongstory.tistory.com

 

 


데이터 준비

 

chicago_prophet =  chicago_df.resample('D').size()
chicago_prophet = chicago_prophet.reset_index()

프로펫 라이브러리 사용하려면 날짜는 'ds' 예측수치는 y로 세팅을 해야한다

 

chicago_prophet.columns=['ds','y']

 

 


예측하기

prophet = Prophet()
prophet.fit(chicago_prophet)

 

freq조절 문자

https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#timeseries-offset-aliases

 

Time series / date functionality — pandas 1.5.2 documentation

Time series / date functionality pandas contains extensive capabilities and features for working with time series data for all domains. Using the NumPy datetime64 and timedelta64 dtypes, pandas has consolidated a large number of features from other Python

pandas.pydata.org

페이스북 프로펫 페이지 예

https://facebook.github.io/prophet/docs/non-daily_data.html

 

Non-Daily Data

Prophet is a forecasting procedure implemented in R and Python. It is fast and provides completely automated forecasts that can be tuned by hand by data scientists and analysts.

facebook.github.io

 

 

2년치(730일)예측을 한다면

future = prophet.make_future_dataframe(periods = 730 , freq = 'D')
forecast = prophet.predict(future)


시각화

prophet.plot(forecast)
plt.show()
plt.savefig('chart11.jpg')

 

prophet.plot_components(forecast)
plt.show()
plt.savefig('chart12.jpg')