a = 1
def edit_a(i):
global a
a = i
edit_a(2)
a
$$ y = asin(2\pi ft + t_0) + b $$
1
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
def plotSinWave(amp, freq, endTime,sampleTime,startTime, bias):
"""
plot sine wave
y = a sin(2 pi t + t_0) + b
"""
time = np.arange(startTime, endTime, sampleTime) # 시작, 끝, 간격
result = amp * np.sin(2 * np.pi * freq * time + startTime) + bias
plt.figure(figsize=(12,6))
plt.plot(time, result)
plt.grid(True)
plt.xlabel('time')
plt.ylabel('sin')
plt.title(str(amp) + '*sin(2*pi' + str(freq) + '*t+' + str(startTime) + ')' + str(bias))
plt.show()
plotSinWave(2,1,10,0.01,0.5,0)
""" 으로 묶인 부분은 나중에 shift+tab(독스트링)하면 나오는 설명부분을 작성한 것이다
1번같은 경우 함수에 필요한 인자가 많아서 나중에 사용하기에 순서가 헷갈릴 수 있다
2
def plotSinWave(**kwargs):
"""
plot sine wave
y = a sin(2 pi t + t_0) + b
"""
endTime = kwargs.get('endTIme',1)
sampleTime = kwargs.get('sampleTime',0.01)
amp = kwargs.get('amp',1)
freq = kwargs.get('freq',1)
startTime = kwargs.get('startTime',0)
bias = kwargs.get('bias',0)
figsize = kwargs.get('figsize',(12,6))
time = np.arange(startTime, endTime, sampleTime) # 시작, 끝, 간격
result = amp * np.sin(2 * np.pi * freq * time + startTime) + bias
plt.figure(figsize=(12,6))
plt.plot(time, result)
plt.grid(True)
plt.xlabel('time')
plt.ylabel('sin')
plt.title(str(amp) + '*sin(2*pi' + str(freq) + '*t+' + str(startTime) + ')' + str(bias))
plt.show()
plotSinWave(amp=2, freq=0.5, endTime=10)
%%writefile ./drawSinWave.py
import numpy as np
import matplotlib.pyplot as plt
def plotSinWave(**kwargs):
"""
plot sine wave
y = a sin(2 pi f t + t_0) + b
"""
endTime = kwargs.get("endTime", 1)
sampleTime = kwargs.get("sampleTime", 0.01)
amp = kwargs.get("amp", 1)
freq = kwargs.get("freq", 1)
startTime = kwargs.get("startTime", 0)
bias = kwargs.get("bias", 0)
figsize = kwargs.get("figsize", (12, 6))
time = np.arange(startTime, endTime, sampleTime)
result = amp * np.sin(2 * np.pi * freq * time + startTime) + bias
plt.figure(figsize=(12, 6))
plt.plot(time, result)
plt.grid(True)
plt.xlabel("time")
plt.ylabel("sin")
plt.title(str(amp) + "*sin(2*pi" + str(freq) + "*t+" + str(startTime) + ")+" + str(bias))
plt.show()
if __name__ == "__main__":
print("hello world~!!")
print("this is test graph!!")
plotSinWave(amp=1, endTime=2)
Overwriting ./drawSinWave.py
import drawSinWave as dS
dS.plotSinWave()
%%writefile ./set_matplotlib_hangul.py
import platform
import matplotlib.pyplot as plt
from matplotlib import font_manager, rc
path = "c:/Windows/Fonts/malgun.ttf"
if platform.system() == "Darwin":
print("Hangul OK in your MAC!!!")
rc("font", family="Arial Unicode MS")
elif platform.system() == "Windows":
font_name = font_manager.FontProperties(fname=path).get_name()
print("Hangul OK in your Windows!!!")
rc("font", family=font_name)
else:
print("Unknown system.. sorry~~~")
plt.rcParams["axes.unicode_minus"] = False
plt.title("한글")
-> 한글로 잘 나온다
import set_matplotlib_hangul
설치
pip install pandas-datareader | conda install pandas-datareader
pip install fbprophet | conda install -c conda-forge fbprophet
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
# 데이터 준비
time = np.linspace(0,1, 365*2) # 0부터 1까지 730등분
result = np.sin(2*np.pi*12*time)
ds = pd.date_range('2017-01-01', periods=365*2, freq='D') # freq='5D'이면 5일씩
df = pd.DataFrame({
'ds':ds,
'y':result
})
df.head()
df['y'].plot(figsize=(10,6));
1
from prophet import Prophet
# 학습
m = Prophet(yearly_seasonality=True, daily_seasonality=True)
m.fit(df)
# 이후 30일간의 데이터 예측
future = m.make_future_dataframe(periods=30)
forecast = m.predict(future)
m.plot(forecast)
2
time = np.linspace(0,1, 365*2)
result = np.sin(2*np.pi*12*time) + time
ds = pd.date_range('2017-01-01', periods=365*2, freq='D')
df = pd.DataFrame({
'ds':ds,
'y':result
})
df['y'].plot(figsize=(10,6));
m = Prophet(yearly_seasonality=True, daily_seasonality=True)
m.fit(df) # 학습
future = m.make_future_dataframe(periods=30)
forecast = m.predict(future)
m.plot(forecast)
3
time = np.linspace(0,1, 365*2)
result = np.sin(2*np.pi*12*time) + time + np.random.randn(365*2)/4
ds = pd.date_range('2017-01-01', periods=365*2, freq='D')
df = pd.DataFrame({
'ds':ds,
'y':result
})
df['y'].plot(figsize=(10,6));
m = Prophet(yearly_seasonality=True, daily_seasonality=True)
m.fit(df)
future = m.make_future_dataframe(periods=30)
forecast = m.predict(future)
m.plot(forecast)
import pandas as pd
import pandas_datareader as web
import numpy as np
import matplotlib.pyplot as plt
from fbprophet import Prophet
from datetime import datetime
%matplotlib inline
pinkwink_web = pd.read_csv(
"../data/05_PinkWink_Web_Traffic.csv",
encoding="utf-8",
thousands=",",
names=["date", "hit"],
index_col=0
)
pinkwink_web = pinkwink_web[pinkwink_web["hit"].notnull()]
pinkwink_web.head()
# 전체 데이터 그려보기
pinkwink_web['hit'].plot(figsize=(12,4), grid=True)
# trend 분석을 시각화하기 위한 x축 값을 만들기
time = np.arange(0,len(pinkwink_web))
traffic = pinkwink_web['hit'].values
fx = np.linspace(0, time[-1], 1000)
# 에러를 계산할 함수
def error(f,x,y):
return np.sqrt(np.mean(f(x) - y) ** 2) # 에러의 제곱의 평균에 루트를 씌운 것
fp1 = np.ployfit(time, traffic, 1) # 계수
f1 = np.poly1d(fp1) # 함수
f2p = np.ployfit(time, traffic, 2) # 계수
f2 = np.poly1d(f2p) # 함수
f3p = np.ployfit(time, traffic, 3) # 계수
f3 = np.poly1d(f3p) # 함수
f15p = np.ployfit(time, traffic, 15) # 계수
f15 = np.poly1d(f15p) # 함수
plt.figure(figsize=(12,4))
plt.scatter(time, traffic, s=10)
plt.plot(fx,f1(fx),lw=4, label='f1')
plt.plot(fx,f2(fx),lw=4, label='f2')
plt.plot(fx,f3(fx),lw=4, label='f3')
plt.plot(fx,f15(fx),lw=4, label='f15')
plt.grid(True, linestyle='-', color='0.75')
plt.legend(loc=2)
df = pd.DataFrame({'ds':pinkwink_web.index, 'y': pinkwink_web['hit']})
df.reset_index(inplace=True)
df['ds'] = pd.to_datetime(df['ds'], format='%y. %m. %d.')
del df['date']
df.head()
m = Prophet(yearly_seasonality=True, daily_seasonality=True)
m.fit(df) # prophet에 적용
future = m.make_future_dataframe(periods=60)
future.tail()
# 예측 결과는 상한/하한의 범위를 포함해서 얻어진다
forecast = m.predict(future)
forecast[['ds','yhat','yhat_lower','yhat_upper']].tail()
m.plot(forecast);
m.plot_components(forecast)
# 데이터 추출 : 네이버
req = Request(
"https://finance.yahoo.com/quote/035420.KS/history?p=035420.KS&guccounter=1",
headers={"User-Agent" : "Chrome"},
)
page = urlopen(req).read()
soup = BeautifulSoup(page, 'html.parser')
table = soup.find("table")
df_raw = pd.read_html(str(table))[0]
df_raw.head(3)
# fbprophet 형식에 맞추고 nan 제외
# 종가(Close*) 사용
df_tmp = pd.DataFrame({'ds' : df_raw['Date'], 'y' : df_raw["Close*"]})
df_target = df_tmp[:-1]
df_target.head()
# 날짜 형식 변경
df = df_target.copy()
df['ds'] = pd.to_datetime(df_target['ds'], format="%b %d, %Y")
df.head()
# 정보 확인
df.info()
# y컬럼 데이터 타입 변경
df["y"] = df['y'].astype("float")
# 예측
m = Prophet(yearly_seasonality=True, daily_seasonality=True)
m.fit(df);
future = m.make_future_dataframe(periods=30)
forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
# 실제 데이터와 예측 데이터 비교
plt.figure(figsize=(12, 6))
plt.plot()
plt.plot(df['ds'], df['y'], label = 'real')
plt.plot(forecast['ds'], forecast['yhat'], label = 'forecast')
plt.grid(True)
plt.show();
# 예측 그래프
m.plot(forecast);
# 트렌드 분석
m.plot_components(forecast);
yfinance 모듈
pip install yfinance
# 테스트 : 대한항공(003490)
start_date = "2010-03-01"
end_date = "2018-02-28"
KoreaAir = data.get_data_yahoo("003490.KS", start_date, end_date)
KoreaAir.head()
# 종가 그래프
KoreaAir["Close"].plot(figsize=(12, 6), grid=True);
# 2017-11-30 잘라내기 : 예측값과 비교해보기 위해
KoreaAir_trunc = KoreaAir[:"2017-11-30"]
KoreaAir_trunc.head()
# 데이터 정리
df = pd.DataFrame({'ds' : KoreaAir_trunc.index, 'y':KoreaAir_trunc["Close"]})
df.reset_index(inplace=True)
del df["Date"]
df.head()
# 예측
m = Prophet(yearly_seasonality=True, daily_seasonality=True)
m.fit(df)
future = m.make_future_dataframe(periods=90)
forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
# 실제와 예측값 비교 그래프
plt.figure(figsize=(12, 6))
plt.plot(KoreaAir.index, KoreaAir["Close"], label="real")
plt.plot(forecast["ds"], forecast["yhat"], label="forecast")
plt.grid(True)
plt.legend()
plt.show();
# 예측 그래프
m.plot(forecast);
# 트렌드 분석
m.plot_components(forecast);
비트코인 데이터 https://bitcoincharts.com/charts/bitstampUSD#rg60ztgSzm1g10zm2g25zv
# 웹 페이지 접근
# 2년 URL 주소
driver = webdriver.Chrome('../driver/chromedriver.exe')
driver.get("https://bitcoincharts.com/charts/bitstampUSD#rg730ztgSzm1g10zm2g25zv")
# raw_data 메뉴가 가려지기 때문에 스크롤 조정
xpath = '''//*[@id="content_chart"]/div/div[2]/a'''
variable = driver.find_element_by_xpath(xpath)
driver.execute_script('return arguments[0].scrollIntoView();', variable)
variable.click()
# 데이터 추출
html = driver.page_source
soup = BeautifulSoup(html, 'html.parser')
table = soup.find("table", id='chart_table')
table
read_html
# read_html로 데이터프레임 생성
df = pd.read_html(str(table))
bitcoin = df[0]
bitcoin.head()
# 셀레니움 종료
driver.close()
# 종가 그래프
bitcoin["Close"].plot(figsize=(12, 6), grid=True);
# 종가로 Prophet 적용
df = pd.DataFrame({'ds' : bitcoin["Timestamp"], 'y' : bitcoin["Close"]})
# 예측
m = Prophet(yearly_seasonality=True, daily_seasonality=True)
m.fit(df);
future = m.make_future_dataframe(periods=30)
forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
# 실제, 예측 비교 그래프
plt.figure(figsize=(12, 6))
plt.plot(df['ds'], df['y'], label='real')
plt.plot(forecast['ds'], forecast['yhat'], label='forecast')
plt.grid(True)
plt.legend(loc=2)
plt.show();
# 예측 그래프
m.plot(forecast);
# 트렌드 분석
m.plot_components(forecast);