ML (MachineLearning)

ํ”„๋กœํŽซ(Prophet) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉํ•˜๊ธฐ

567Rabbit 2024. 4. 23. 18:10
  • install : pip install prophet
  • ์œ„ ์—๋Ÿฌ ๋ฐœ์ƒ์‹œ : conda install -c conda-forge prophet

 

 

์ž„ํฌํŠธํ•˜๊ธฐ

 

python
๋‹ซ๊ธฐ
import pandas as pd import numpy as np import matplotlib.pyplot as plt import random import seaborn as sns from prophet import Prophet

 

 

 

 

์•„๋ณด์นด๋„ ๋ฐ์ดํ„ฐ

 

 

 

ํ”„๋กœํŽซ ๋ถ„์„์„ ์œ„ํ•ด, ๋‘๊ฐœ์˜ ์ปฌ๋Ÿผ๋งŒ ๊ฐ€์ ธ์˜ค๊ธฐ ('Date', 'AveragePrice')

 

avocado_prophet_df = df[['Date','AveragePrice']]

 

 

avocado_prophet_df

  Date AveragePrice
0 2015-01-04 1.33
1 2015-01-04 1.35
2 2015-01-04 0.93
3 2015-01-04 1.08
4 2015-01-04 1.28
... ... ...
7 2018-03-25 1.63
8 2018-03-25 1.71
9 2018-03-25 1.87
10 2018-03-25 1.93
11 2018-03-25 1.62

 

 

 

 

ds ์™€ y ๋กœ ์ปฌ๋Ÿผ๋ช… ์…‹ํŒ…

 

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

 

avocado_prophet_df

 

  ds y
0 2015-01-04 1.33
1 2015-01-04 1.35
2 2015-01-04 0.93
3 2015-01-04 1.08
4 2015-01-04 1.28
... ... ...
7 2018-03-25 1.63
8 2018-03-25 1.71
9 2018-03-25 1.87
10 2018-03-25 1.93
11 2018-03-25 1.62

 

 

 

 

ํ”„๋กœํŽซ ์˜ˆ์ธก

 

# 1. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ณ€์ˆ˜๋กœ ๋งŒ๋“ค๊ณ 
prophet = Prophet()

 

# 2. ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šต์‹œํ‚จ๋‹ค
prophet.fit(avocado_prophet_df)

 

# 3. ์˜ˆ์ธกํ•˜๊ณ ์ž ํ•˜๋Š” ๊ธฐ๊ฐ„์„ ์ •ํ•ด์„œ, ๋น„์–ด์žˆ๋Š” ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„์„ ๋งŒ๋“ ๋‹ค
future = prophet.make_future_dataframe(periods=365, freq='D')  #365์ผ

 

# 4. ์˜ˆ์ธก์„ ํ•œ๋‹ค
forecast = prophet.predict(future)

 

 

 

# ์ฐจํŠธ๋กœ ํ™•์ธ
prophet.plot(forecast)
plt.savefig('chart1.jpg')
plt.show()

 

 

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

 

 

 

 

 

 

region ์ด west ์ธ ์•„๋ณด์นด๋„์˜ ๊ฐ€๊ฒฉ์„ ์˜ˆ์ธกํ•˜์‹œ์˜ค.

 

df = df[df['region'].str.contains('West')]

df = df.loc[ : , 'Date':'AveragePrice']

 

import pandas as pd

df = pd.DataFrame(df)

df = df.rename(columns={'Date':'ds', 'AveragePrice':'y'})

 

 

prophet = Prophet()

prophet.fit(df)

future = prophet.make_future_dataframe(periods=365, freq='D')

 

forecast = prophet.predict(future)

 

 

prophet.plot(forecast)
plt.show()

 

 

 

 

prophet.plot_components(forecast)
plt.show()

 

 

 

 

 

 

์‹œ์นด๊ณ  ๋ฒ”์ฃ„ ๋ฐ์ดํ„ฐ

 

 

df_prophet = chicago_df.resample('D').size().to_frame().reset_index().rename(columns={'Date':'ds', 0:'y'})

prophet = Prophet()

prophet.fit(df_prophet)

future = prophet.make_future_dataframe(periods=365, freq='D')

forecast = prophet.predict(future)

 

 

prophet.plot(forecast)

 

 

 

prophet.plot_components(forecast)