<reponame>LiveCoronaDetector/-<filename>003. korea_regional_data_play/regional_eda_analysis_and_prophet_prediction.py
# -*- coding: utf-8 -*-
"""Regional EDA analysis and Prophet prediction.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1nmfyKQc9J-WBoQyqsFGfGlbcxxfECW6Z
"""

import pandas as pd
region_daily = pd.read_csv("/content/region_daily.csv")
region_daily

since_219 = region_daily.iloc[459:]
since_219

data = since_219.groupby(['region','date']).confirmed.sum()
data = pd.DataFrame(data)
data.reset_index(inplace=True)

import plotly.express as px


fig = px.bar(data, x="region", y="confirmed", color="region", title="Regional confirmed patient",
  animation_frame="date", animation_group="date", range_y=[0,7000])
fig.show()

"""# korea total confirmed EDA
국내 확진자수 합계가 있어 막대그래프로 시각화
"""

import pandas as pd
import numpy as np

from fbprophet import Prophet
from fbprophet.plot import plot_plotly, add_changepoints_to_plot
from plotly.offline import iplot, init_notebook_mode

import plotly.graph_objs as go
import plotly.offline as py
import plotly.express as px

import seaborn as sns
import scipy as sp

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

daily_report =pd.read_csv('/content/korea_corona_confirmed_by_region.csv')
col= ['date', 'Seoul','Busan','Incheon','Gwangju' ,'Daejeon', 'Ulsan','Sejong', 'Gyeonggi-do', 'Gangwon-do', 'Chungcheongbuk-do', 'Chungcheongnam-do', 'Jeollabuk-do', 'Jeollanam-do',
      'Gyeongsangnam-do','Jeju-do', 'Daegu','Gyeongsangbuk-do','total']
daily_report.columns=col
daily_report.info()

import datetime
#end = datetime.datetime.now() - datetime.timedelta(1)
date_index = pd.date_range('2020-01-23', '2020-03-13')
daily_report.index = date_index
daily_report.drop(['date'], axis=1, inplace=True)

# 구글코랩에서 plotly 실행

from plotly.offline import iplot, init_notebook_mode
def configure_plotly_browser_state():
  import IPython
  display(IPython.core.display.HTML('''
        <script src="/static/components/requirejs/require.js"></script>
        <script>
          requirejs.config({
            paths: {
              base: '/static/base',
              plotly: 'https://cdn.plot.ly/plotly-latest.min.js?noext',
            },
          });
        </script>
        '''))

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

import cufflinks as cf 
cf.go_offline(connected=True)
daily_report.iloc[:,-1].iplot( kind='bar') # total count

"""# 최근 데이터로 지역별 파이그래프로 시각화 하였습니다. 
대구와 경북이 상위를 차지합니다. 2차지역감염으로 인해 신천지피해가 급증한 지역이므로 아래에서 별도로 막대그래프로 EDA 후 지역전파그래프 예측모델을 그리겠습니다.
"""

latest = daily_report.iloc[-1,:17]
latest = pd.DataFrame(latest)

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

import plotly.express as px
index_name= ['Seoul','Busan','Incheon','Gwangju' ,'Daejeon', 'Ulsan','Sejong', 'Gyeonggi-do', 'Gangwon-do', 'Chungcheongbuk-do', 'Chungcheongnam-do', 'Jeollabuk-do', 'Jeollanam-do',
      'Gyeongsangnam-do','Jeju-do', 'Daegu','Gyeongsangbuk-do']
fig = px.pie(latest, values=latest.values, names=index_name)
fig.show()

"""# After Secondary regional infection ( 2020-02-18 ) EDA"""

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False) # 구글코랩해서 실행하기 위해 붙여주는 코드입니다

daily_by_2_18 = daily_report.iloc[25:,:-1]
daily_by_2_18.iplot(kind='bar')

"""# the Center of Secondary infection"""

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

daily_by_Gyeongbuk_Daegu = daily_report.iloc[23:,15:]
daily_by_Gyeongbuk_Daegu.iplot(kind='bar')

"""# Prophet Prediction on Daegu
# 대구 확진자수 예측모델
"""

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

daily_by_Daegu = daily_report.iloc[23:,15]
daily_by_Daegu = pd.DataFrame(daily_by_Daegu)
daily_by_Daegu.reset_index(inplace=True)
df_prophet = daily_by_Daegu.rename(columns={ 'index': 'ds', 'Daegu': 'y' })

df_prophet

m = Prophet(
    changepoint_prior_scale=0.2, # increasing it will make the trend more flexible
    changepoint_range=0.95, # place potential changepoints in the first 98% of the time series
    yearly_seasonality=False,
    weekly_seasonality=False,
    daily_seasonality=True,
    seasonality_mode='additive'
)

m.fit(df_prophet)

future = m.make_future_dataframe(periods=7)
forecast = m.predict(future)


forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(7)

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

fig = plot_plotly(m, forecast)
fig.update_layout(title_text="Accumulated Confirmed_patient of nCOV-19 in Daegu")
# 대구는 아직까지는 확진자가 미비하게 증가할것으로 보여집니다
py.iplot(fig)

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

fig = m.plot(forecast)
a = add_changepoints_to_plot(fig.gca(), m, forecast)

sns.distplot(m.params["delta"], kde=False, fit=sp.stats.laplace)
plt.box(False)

"""# Prophet Prediction on Gyeongbuk
# 경북 확진자수 예측모델
"""

daily_by_Gyeongbuk = daily_report.iloc[23:,16]
daily_by_Gyeongbuk = pd.DataFrame(daily_by_Gyeongbuk)

daily_by_Gyeongbuk.reset_index(inplace=True)
df_prophet = daily_by_Gyeongbuk.rename(columns={ 'index': 'ds', 'Gyeongsangbuk-do': 'y' })

m = Prophet(
    changepoint_prior_scale=0.2, # increasing it will make the trend more flexible
    changepoint_range=0.95, # place potential changepoints in the first 98% of the time series
    yearly_seasonality=False,
    weekly_seasonality=False,
    daily_seasonality=True,
    seasonality_mode='additive'
)

m.fit(df_prophet)

future = m.make_future_dataframe(periods=7)
forecast = m.predict(future)


forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(7)

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

fig = plot_plotly(m, forecast)
fig.update_layout(title_text="Accumulated Confirmed_patient of nCOV-19 in Gyeongbuk")
py.iplot(fig)

sns.distplot(m.params["delta"], kde=False, fit=sp.stats.laplace)
plt.box(False)

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

import cufflinks as cf 
cf.go_offline(connected=True)
daily_report.iloc[:, 15:].iplot( fill=True) # total count

"""# 국내 확진자수가 증가하는 양상은 
# 지속적으로 증가율이 낮아지고 있음을 그래프를 통해 확인할수 있습니다
"""

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

total = daily_report.iloc[:, 17]
print(total.count())


increased_daily =[0,]
for i in date_index:
  increased_d = total[i+1] - total[i]
  increased_daily.append(increased_d)

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

total= pd.DataFrame(total)
total['increased_daily']= increased_daily
total

"""# 발표되는 확진자수도 일별로 감소하는 양상을 볼수 있는데,
## (이 데이터셋에 들어있지 않지만) 현재는 검사중인 미확진자에 수역시 줄고 있기 때문에 연관성이 있는 것으로 보입니다
"""

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

total.iloc[:,-1].iplot(kind='bar')

total_by_3_3 = total.iloc[40:50, :]
total_by_3_3['active'] = [35555,28414,21810,21832,19620,19376,17458,18452,18540,17727] # 10일간 본 검사중인 사람수 누적결과 by CDC
total_by_3_3

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

total_by_3_3['active'].iplot(kind='bar')




korea_regional_data_play/regional_eda_analysis_and_prophet_prediction.py
# -*- coding: utf-8 -*-
"""Regional EDA analysis and Prophet prediction.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1nmfyKQc9J-WBoQyqsFGfGlbcxxfECW6Z
"""

import pandas as pd
region_daily = pd.read_csv("/content/region_daily.csv")
region_daily

since_219 = region_daily.iloc[459:]
since_219

data = since_219.groupby(['region','date']).confirmed.sum()
data = pd.DataFrame(data)
data.reset_index(inplace=True)

import plotly.express as px


fig = px.bar(data, x="region", y="confirmed", color="region", title="Regional confirmed patient",
  animation_frame="date", animation_group="date", range_y=[0,7000])
fig.show()

"""# korea total confirmed EDA
국내 확진자수 합계가 있어 막대그래프로 시각화
"""

import pandas as pd
import numpy as np

from fbprophet import Prophet
from fbprophet.plot import plot_plotly, add_changepoints_to_plot
from plotly.offline import iplot, init_notebook_mode

import plotly.graph_objs as go
import plotly.offline as py
import plotly.express as px

import seaborn as sns
import scipy as sp

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

daily_report =pd.read_csv('/content/korea_corona_confirmed_by_region.csv')
col= ['date', 'Seoul','Busan','Incheon','Gwangju' ,'Daejeon', 'Ulsan','Sejong', 'Gyeonggi-do', 'Gangwon-do', 'Chungcheongbuk-do', 'Chungcheongnam-do', 'Jeollabuk-do', 'Jeollanam-do',
      'Gyeongsangnam-do','Jeju-do', 'Daegu','Gyeongsangbuk-do','total']
daily_report.columns=col
daily_report.info()

import datetime
#end = datetime.datetime.now() - datetime.timedelta(1)
date_index = pd.date_range('2020-01-23', '2020-03-13')
daily_report.index = date_index
daily_report.drop(['date'], axis=1, inplace=True)

# 구글코랩에서 plotly 실행

from plotly.offline import iplot, init_notebook_mode
def configure_plotly_browser_state():
  import IPython
  display(IPython.core.display.HTML('''
        <script src="/static/components/requirejs/require.js"></script>
        <script>
          requirejs.config({
            paths: {
              base: '/static/base',
              plotly: 'https://cdn.plot.ly/plotly-latest.min.js?noext',
            },
          });
        </script>
        '''))

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

import cufflinks as cf 
cf.go_offline(connected=True)
daily_report.iloc[:,-1].iplot( kind='bar') # total count

"""# 최근 데이터로 지역별 파이그래프로 시각화 하였습니다. 
대구와 경북이 상위를 차지합니다. 2차지역감염으로 인해 신천지피해가 급증한 지역이므로 아래에서 별도로 막대그래프로 EDA 후 지역전파그래프 예측모델을 그리겠습니다.
"""

latest = daily_report.iloc[-1,:17]
latest = pd.DataFrame(latest)

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

import plotly.express as px
index_name= ['Seoul','Busan','Incheon','Gwangju' ,'Daejeon', 'Ulsan','Sejong', 'Gyeonggi-do', 'Gangwon-do', 'Chungcheongbuk-do', 'Chungcheongnam-do', 'Jeollabuk-do', 'Jeollanam-do',
      'Gyeongsangnam-do','Jeju-do', 'Daegu','Gyeongsangbuk-do']
fig = px.pie(latest, values=latest.values, names=index_name)
fig.show()

"""# After Secondary regional infection ( 2020-02-18 ) EDA"""

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False) # 구글코랩해서 실행하기 위해 붙여주는 코드입니다

daily_by_2_18 = daily_report.iloc[25:,:-1]
daily_by_2_18.iplot(kind='bar')

"""# the Center of Secondary infection"""

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

daily_by_Gyeongbuk_Daegu = daily_report.iloc[23:,15:]
daily_by_Gyeongbuk_Daegu.iplot(kind='bar')

"""# Prophet Prediction on Daegu
# 대구 확진자수 예측모델
"""

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

daily_by_Daegu = daily_report.iloc[23:,15]
daily_by_Daegu = pd.DataFrame(daily_by_Daegu)
daily_by_Daegu.reset_index(inplace=True)
df_prophet = daily_by_Daegu.rename(columns={ 'index': 'ds', 'Daegu': 'y' })

df_prophet

m = Prophet(
    changepoint_prior_scale=0.2, # increasing it will make the trend more flexible
    changepoint_range=0.95, # place potential changepoints in the first 98% of the time series
    yearly_seasonality=False,
    weekly_seasonality=False,
    daily_seasonality=True,
    seasonality_mode='additive'
)

m.fit(df_prophet)

future = m.make_future_dataframe(periods=7)
forecast = m.predict(future)


forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(7)

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

fig = plot_plotly(m, forecast)
fig.update_layout(title_text="Accumulated Confirmed_patient of nCOV-19 in Daegu")
# 대구는 아직까지는 확진자가 미비하게 증가할것으로 보여집니다
py.iplot(fig)

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

fig = m.plot(forecast)
a = add_changepoints_to_plot(fig.gca(), m, forecast)

sns.distplot(m.params["delta"], kde=False, fit=sp.stats.laplace)
plt.box(False)

"""# Prophet Prediction on Gyeongbuk
# 경북 확진자수 예측모델
"""

daily_by_Gyeongbuk = daily_report.iloc[23:,16]
daily_by_Gyeongbuk = pd.DataFrame(daily_by_Gyeongbuk)

daily_by_Gyeongbuk.reset_index(inplace=True)
df_prophet = daily_by_Gyeongbuk.rename(columns={ 'index': 'ds', 'Gyeongsangbuk-do': 'y' })

m = Prophet(
    changepoint_prior_scale=0.2, # increasing it will make the trend more flexible
    changepoint_range=0.95, # place potential changepoints in the first 98% of the time series
    yearly_seasonality=False,
    weekly_seasonality=False,
    daily_seasonality=True,
    seasonality_mode='additive'
)

m.fit(df_prophet)

future = m.make_future_dataframe(periods=7)
forecast = m.predict(future)


forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail(7)

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

fig = plot_plotly(m, forecast)
fig.update_layout(title_text="Accumulated Confirmed_patient of nCOV-19 in Gyeongbuk")
py.iplot(fig)

sns.distplot(m.params["delta"], kde=False, fit=sp.stats.laplace)
plt.box(False)

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

import cufflinks as cf 
cf.go_offline(connected=True)
daily_report.iloc[:, 15:].iplot( fill=True) # total count

"""# 국내 확진자수가 증가하는 양상은 
# 지속적으로 증가율이 낮아지고 있음을 그래프를 통해 확인할수 있습니다
"""

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

total = daily_report.iloc[:, 17]
print(total.count())


increased_daily =[0,]
for i in date_index:
  increased_d = total[i+1] - total[i]
  increased_daily.append(increased_d)

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

total= pd.DataFrame(total)
total['increased_daily']= increased_daily
total

"""# 발표되는 확진자수도 일별로 감소하는 양상을 볼수 있는데,
## (이 데이터셋에 들어있지 않지만) 현재는 검사중인 미확진자에 수역시 줄고 있기 때문에 연관성이 있는 것으로 보입니다
"""

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

total.iloc[:,-1].iplot(kind='bar')

total_by_3_3 = total.iloc[40:50, :]
total_by_3_3['active'] = [35555,28414,21810,21832,19620,19376,17458,18452,18540,17727] # 10일간 본 검사중인 사람수 누적결과 by CDC
total_by_3_3

# Recquired code to run plotly in a cell
configure_plotly_browser_state()
init_notebook_mode(connected=False)

total_by_3_3['active'].iplot(kind='bar')
# -*- coding: utf-8 -*- Regional EDA analysis and Prophet prediction.ipynb  Automatically generated by Colaboratory.  Original file is located at     https://colab.research.google.com/drive/1nmfyKQc9J-WBoQyqsFGfGlbcxxfECW6Z # korea total confirmed EDA 국내 확진자수 합계가 있어 막대그래프로 시각화 #end = datetime.datetime.now() - datetime.timedelta(1) # 구글코랩에서 plotly 실행 <script src="/static/components/requirejs/require.js"></script>         <script>           requirejs.config({             paths: {               base: '/static/base',               plotly: 'https://cdn.plot.ly/plotly-latest.min.js?noext',             },           });         </script> # Recquired code to run plotly in a cell # total count # 최근 데이터로 지역별 파이그래프로 시각화 하였습니다.  대구와 경북이 상위를 차지합니다. 2차지역감염으로 인해 신천지피해가 급증한 지역이므로 아래에서 별도로 막대그래프로 EDA 후 지역전파그래프 예측모델을 그리겠습니다. # Recquired code to run plotly in a cell # After Secondary regional infection ( 2020-02-18 ) EDA # Recquired code to run plotly in a cell # 구글코랩해서 실행하기 위해 붙여주는 코드입니다 # the Center of Secondary infection # Recquired code to run plotly in a cell # Prophet Prediction on Daegu # 대구 확진자수 예측모델 # Recquired code to run plotly in a cell # increasing it will make the trend more flexible # place potential changepoints in the first 98% of the time series # Recquired code to run plotly in a cell # 대구는 아직까지는 확진자가 미비하게 증가할것으로 보여집니다 # Recquired code to run plotly in a cell # Prophet Prediction on Gyeongbuk # 경북 확진자수 예측모델 # increasing it will make the trend more flexible # place potential changepoints in the first 98% of the time series # Recquired code to run plotly in a cell # Recquired code to run plotly in a cell # total count # 국내 확진자수가 증가하는 양상은  # 지속적으로 증가율이 낮아지고 있음을 그래프를 통해 확인할수 있습니다 # Recquired code to run plotly in a cell # Recquired code to run plotly in a cell # 발표되는 확진자수도 일별로 감소하는 양상을 볼수 있는데, ## (이 데이터셋에 들어있지 않지만) 현재는 검사중인 미확진자에 수역시 줄고 있기 때문에 연관성이 있는 것으로 보입니다 # Recquired code to run plotly in a cell # 10일간 본 검사중인 사람수 누적결과 by CDC # Recquired code to run plotly in a cell