12/29 Data Study to do
- 시계열 분석 (Ch6. 시계열 분석, 1-10강)
오늘 공부 요약짤...🥲

%%writefile ./drawSinWave.py
import matplotlib.pyplot as plt
import numpy as np
def plotSineWave(**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))
'''
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(2pi' + str(freq) + '*t+' + str(startTime) + ')+' + str(bias))
plt.show()
if __name__ == '__main__':
print('Hello World~!!')
print('This is test grph!')
plotSineWave(amp=1, endTime=2)
- 함수 하단 작은 따옴표 3개 뒤에 작성한 글은 함수의 독스트링으로 활용
- 입력 인자 자리에 '**kwargs(이름은 정하면 됨)'을 쓰면 사용자의 입력 인자가 없을 경우 기본 값을 설정할 수 있음
→ 코드 작성 : kwargs.get('example', 기본값)
→ 사용 시 : def_name(exapmle=5) : example 값을 5로 선언
%%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. :)')
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. :(')
plt.rcParams['axes.unicode_minus'] = False
- 주피터 또는 vs code에서 파일 생성하는 방법
- %%writefile ./file_name.py (← 경로, 파일 이름, 파일 형식)
time = np.linspace(0, 1, 365*2)
result = np.sin(2*np.pi*12*time)
ds = pd.date_range('2018-01-01', periods=365*2, freq='D')
df = pd.DataFrame({'ds':ds, 'y':result})
df.head()
- linspace(a, b, c) : a부터 b까지 c간격으로 1차원 배열
- pd.date_range('2018-01-01', periods=365*2, freq='D')
- 데이를 기준으로 2018-01-01부터 730일 까지의 날짜 데이터 생성
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);
- Prophet 함수에 데이터 프레임 학습
- 30일 간의 미래 데이터 생성
- 그래프 생성
def error(f, x, y):
return np.sqrt(np.mean((f(x) - y) ** 2))
- f(x): 함수 f에 입력 데이터 x를 전달하여 예측값 획득
- (f(x) - y) 2**: 예측값과 실제값의 차이를 제곱
- np.mean((f(x) - y) 2)**: 모든 예측값과 실제값의 제곱 오차의 평균 계산
- np.sqrt(np.mean((f(x) - y) 2))**: 평균 제곱근 오차(RMSE)를 계산합니다. 이는 예측값과 실제값 간의 평균적인 차이를 나타내는 지표
- 값이 작을수록 예측이 더 정확
