머신러닝 기초

Python으로 배우는 시계열 데이터 Machine Learning

Chris Holdgraf

Fellow, Berkeley Institute for Data Science

항상 데이터를 먼저 확인하세요

array.shape
(10, 5)
array[:3]
array([[ 0.735528  ,  1.00122818, -0.28315978],
       [-0.94478393,  0.18658748, -0.00241224],
       [-0.74822942, -1.46636618,  0.69835096]]) 
Python으로 배우는 시계열 데이터 Machine Learning

항상 데이터를 먼저 확인하세요

df.head()
       col1      col2      col3
0  0.735528  1.001228 -0.283160
1 -0.944784  0.186587 -0.002412
2 -0.748229 -1.466366  0.698351
3  1.038589 -0.171248  0.831457
4 -0.161904  0.003972 -0.321933
Python으로 배우는 시계열 데이터 Machine Learning

항상 데이터를 시각화하세요

예상한 대로 보이는지 확인하세요.

# Using matplotlib
fig, ax = plt.subplots()
ax.plot(...)

# Using pandas
fig, ax = plt.subplots()
df.plot(..., ax=ax)
Python으로 배우는 시계열 데이터 Machine Learning

Scikit-learn

Scikit-learn은 Python에서 가장 널리 사용되는 머신러닝 라이브러리입니다

from sklearn.svm import LinearSVC
Python으로 배우는 시계열 데이터 Machine Learning

scikit-learn을 위한 데이터 준비

  • scikit-learn은 특정 데이터 구조를 요구합니다:

    (samples, features)

  • 데이터가 최소 2차원인지 확인하세요

  • 첫 번째 차원이 samples인지 확인하세요

Python으로 배우는 시계열 데이터 Machine Learning

데이터 형태가 올바르지 않은 경우

  • 축이 교체된 경우:
array.T.shape
(10, 3)
Python으로 배우는 시계열 데이터 Machine Learning

데이터 형태가 올바르지 않은 경우

  • 축이 누락된 경우 .reshape() 사용:
array.shape
(10,)
array.reshape(-1, 1).shape
(10, 1)
  • -1은 나머지 값으로 해당 축을 자동으로 채움
Python으로 배우는 시계열 데이터 Machine Learning

scikit-learn으로 모델 훈련하기

# Import a support vector classifier
from sklearn.svm import LinearSVC

# Instantiate this model
model = LinearSVC()

# Fit the model on some data
model.fit(X, y)

y의 형태는 (samples, 1)인 경우가 일반적임

Python으로 배우는 시계열 데이터 Machine Learning

모델 살펴보기

# There is one coefficient per input feature
model.coef_
array([[ 0.69417875, -0.5289162 ]])
Python으로 배우는 시계열 데이터 Machine Learning

훈련된 모델로 예측하기

# Generate predictions
predictions = model.predict(X_test)
Python으로 배우는 시계열 데이터 Machine Learning

연습해 봅시다!

Python으로 배우는 시계열 데이터 Machine Learning

Preparing Video For Download...