พื้นฐาน Machine Learning

Machine Learning สำหรับข้อมูล Time Series ใน Python

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]]) 
Machine Learning สำหรับข้อมูล Time Series ใน Python

เริ่มต้นด้วยการดูข้อมูลเสมอ

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
Machine Learning สำหรับข้อมูล Time Series ใน Python

แสดงภาพข้อมูลเสมอ

ตรวจสอบให้แน่ใจว่าข้อมูลมีรูปแบบที่ถูกต้องตามที่คาดไว้

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

# Using pandas
fig, ax = plt.subplots()
df.plot(..., ax=ax)
Machine Learning สำหรับข้อมูล Time Series ใน Python

Scikit-learn

Scikit-learn คือไลบรารี Machine Learning ที่ได้รับความนิยมสูงสุดใน Python

from sklearn.svm import LinearSVC
Machine Learning สำหรับข้อมูล Time Series ใน Python

เตรียมข้อมูลสำหรับ scikit-learn

  • scikit-learn ต้องการข้อมูลในรูปแบบที่กำหนด:

    (samples, features)

  • ต้องมีข้อมูลอย่างน้อย สองมิติ

  • มิติแรกต้องเป็น samples

Machine Learning สำหรับข้อมูล Time Series ใน Python

กรณีที่รูปร่างข้อมูลไม่ถูกต้อง

  • หากแกนสลับกัน:
array.T.shape
(10, 3)
Machine Learning สำหรับข้อมูล Time Series ใน Python

กรณีที่รูปร่างข้อมูลไม่ถูกต้อง

  • หากขาดแกน ให้ใช้ .reshape():
array.shape
(10,)
array.reshape(-1, 1).shape
(10, 1)
  • -1 จะเติมค่าที่เหลือในแกนนั้นโดยอัตโนมัติ
Machine Learning สำหรับข้อมูล Time Series ใน Python

การ Fit โมเดลด้วย 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)

Machine Learning สำหรับข้อมูล Time Series ใน Python

ตรวจสอบโมเดล

# There is one coefficient per input feature
model.coef_
array([[ 0.69417875, -0.5289162 ]])
Machine Learning สำหรับข้อมูล Time Series ใน Python

การพยากรณ์ด้วยโมเดลที่ Fit แล้ว

# Generate predictions
predictions = model.predict(X_test)
Machine Learning สำหรับข้อมูล Time Series ใน Python

มาฝึกกันเถอะ!

Machine Learning สำหรับข้อมูล Time Series ใน Python

Preparing Video For Download...