การจำแนกประเภทและการสกัดคุณลักษณะ

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

Chris Holdgraf

Fellow, Berkeley Institute for Data Science

ควรแสดงข้อมูลดิบก่อนเสมอ ก่อนนำไป Fit โมเดล

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

แสดงภาพข้อมูล Time Series!

ixs = np.arange(audio.shape[-1])
time = ixs / sfreq
fig, ax = plt.subplots()
ax.plot(time, audio)

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

จะใช้คุณลักษณะใดดี?

  • การใช้ข้อมูล Time Series ดิบมีสัญญาณรบกวนสูงเกินไปสำหรับการจำแนกประเภท
  • จำเป็นต้องคำนวณคุณลักษณะ (Features)
  • จุดเริ่มต้นที่ง่าย: สรุปข้อมูลเสียง
Machine Learning สำหรับข้อมูล Time Series ใน Python

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

การคำนวณคุณลักษณะหลายตัวพร้อมกัน

print(audio.shape)
# (n_files, time)
(20, 7000) 
means = np.mean(audio, axis=-1)
maxs = np.max(audio, axis=-1)
stds = np.std(audio, axis=-1)

print(means.shape)
# (n_files,)
(20,) 
Machine Learning สำหรับข้อมูล Time Series ใน Python

Fit ตัวจำแนกประเภทด้วย scikit-learn

  • บีบอัดชุดข้อมูล 2 มิติ (samples x time) ให้เหลือเป็นคุณลักษณะของชุดข้อมูล 1 มิติ (samples)
  • นำคุณลักษณะแต่ละตัวมารวมกันเป็น Input ของโมเดล
  • หากมี Label สำหรับแต่ละ Sample สามารถใช้ scikit-learn สร้างและ Fit ตัวจำแนกประเภทได้
Machine Learning สำหรับข้อมูล Time Series ใน Python

เตรียมคุณลักษณะสำหรับ scikit-learn

# Import a linear classifier
from sklearn.svm import LinearSVC

# Note that means are reshaped to work with scikit-learn
X = np.column_stack([means, maxs, stds])
y = labels.reshape(-1, 1)
model = LinearSVC()
model.fit(X, y)
Machine Learning สำหรับข้อมูล Time Series ใน Python

ประเมินคะแนนโมเดล scikit-learn

from sklearn.metrics import accuracy_score

# Different input data
predictions = model.predict(X_test)  

# Score our model with % correct
# Manually
percent_score = sum(predictions == labels_test) / len(labels_test)  
# Using a sklearn scorer
percent_score = accuracy_score(labels_test, predictions)  
Machine Learning สำหรับข้อมูล Time Series ใน Python

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

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

Preparing Video For Download...