Project

PROJECT 1-(5) prophet(ํ”„๋กœํŽซ)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฏธ๋ž˜ 1๋…„์น˜ ์ฃผ๊ฐ€ ์˜ˆ์ธกํ•˜๋Š” ๋ชจ๋ธ ๋งŒ๋“ค๊ธฐ

567Rabbit 2024. 5. 9. 14:33

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

import FinanceDataReader as fdr
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from prophet import Prophet
import streamlit as st

 

 

1. FinanceDataReader๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค

def run_predict() :
    
    col1, col2, col3 = st.columns([1, 2.5, 1])  # ํ™”๋ฉด์„ ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜๊ธฐ ์œ„ํ•จ์ด๋‹ค.
    
    with col2 :
        
        df = fdr.StockListing("KRX")  
        df.reset_index(inplace=True, drop=True)

 

 


 2. ํ™”๋ฉด์— ํ‘œ์‹œํ•  ์„ค๋ช…์„ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค

        st.title('โ…ก. ์ฃผ์‹๊ฐ€์น˜ ์˜ˆ์ธก')
        st.subheader(' : Predicting stock value')
        st.text('๊ธฐ์—… ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜์—ฌ ํ•œ ๊ธฐ์—…์˜ ๊ณผ๊ฑฐ ์ฃผ์‹๋ฐ์ดํ„ฐ๋ฅผ prophet์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฏธ๋ž˜ 1๋…„์น˜')
        st.text('์ฃผ์‹๊ฐ€์น˜๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค.  Code๋ฅผ ๋ชจ๋ฅธ๋‹ค๋ฉด Viewing stock information๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”')
        st.header('')
        st.subheader('์˜ˆ์ธกํ•˜๊ณ ์ž ํ•˜๋Š” ๊ธฐ์—…์˜ Code์ž…๋ ฅํ•˜๊ธฐ')
        code = st.text_input('๊ธฐ์—… Code๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”')




3. ์ฝ”๋“œ(code) ๋˜๋Š” ISU_CD๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ถœ๋ ฅํ•œ๋‹ค

            if len(code) !=0 :
            
            if len(code) > 6 :   # ISU_CD๋กœ ์ž…๋ ฅ๋ฐ›์•˜์„ ๋•Œ
                code = code[-9:-4+1]
            else :
                code = code

            st.text('๊ฒ€์ƒ‰ํ•œ ๊ธฐ์—…์˜ ์ฃผ์‹ ์ •๋ณด๋ฅผ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค')
            st.dataframe(df.loc[code == df['Code']])


            

3-(1) ~ 3-(7) Propet์— ์ž…๋ ฅํ•  ๋ฐ์ดํ„ฐ๋กœ ๊ฐ€๊ณต์‹œํ‚ค๊ณ , Prophet๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด fitํ•ฉ๋‹ˆ๋‹ค

            # 3-(1) ์ž…๋ ฅํ•œ ์ฝ”๋“œ๊ฐ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ symbol์ด๋ผ๋Š” ์ปฌ๋Ÿผ๊ณผ ์ผ์น˜ํ•˜๋Š” ํ–‰์„ ๊ฐ€์ ธ์˜จ๋‹ค
            
            df2 = fdr.DataReader(symbol=code)  
            df2.reset_index(inplace=True)
            
            # 3-(2) prophet์— ์ ์šฉํ•  ์—ด๋งŒ ๊ฐ€์ ธ์˜จ๋‹ค
            df2 = df2.iloc[ : , [0, 3+1]]
            
            # 3-(3) ์ปฌ๋Ÿผ๋ช…์„ ds์™€ y๋กœ ๋ฐ”๊พผ๋‹ค
            df2.rename(columns={'Date': 'ds', 'Close': 'y'}, inplace=True)  
            st.text('Prophet์— ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ปฌ๋Ÿผ๋ช…์„ ds์™€ y๋กœ ๋ณ€๊ฒฝํ•˜์˜€์Šต๋‹ˆ๋‹ค.')
            st.dataframe(df2)
            
            # 3-(4) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋ณ€์ˆ˜๋กœ ๋งŒ๋“ค๊ณ 
            prophet = Prophet()
            
            # 3-(5) ๋ฐ์ดํ„ฐ๋กœ ํ•™์Šต์‹œํ‚จ๋‹ค
            prophet.fit(df2)

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

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


            
            
4. ๋ฐ์ดํ„ฐํ”„๋ ˆ์ž„, ๊ทธ๋ž˜ํ”„๋ฅผ ํ™”๋ฉด์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค

            st.text('ํ˜„์žฌ์‹œ๊ฐ ๊ธฐ์ค€์œผ๋กœ 1๋…„์น˜ ๋ฐ์ดํ„ฐ๋ฅผ ์˜ˆ์ธกํ•ฉ๋‹ˆ๋‹ค.')
            st.dataframe(forecast)
            
            st.subheader('ํ˜„์žฌ ์‹œ๊ฐ ๊ธฐ์ค€ ๋ฐ์ดํ„ฐ์™€ 1๋…„์น˜ ์˜ˆ์ƒ ์ฃผ๊ฐ€์— ๋Œ€ํ•œ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค')
            fig1 = prophet.plot(forecast)
            st.pyplot(fig1)
            
            st.subheader('')
            st.subheader('๊ธฐ์—…์˜ ์—ฐ,์›”,์ฃผ๊ฐ„ ์ฃผ๊ฐ€์— ๋Œ€ํ•œ ์˜ˆ์ธก์น˜ ๊ทธ๋ž˜ํ”„์ž…๋‹ˆ๋‹ค')
            fig2 = prophet.plot_components(forecast)
            st.pyplot(fig2)