分类与特征工程

Python 中的时间序列机器学习

Chris Holdgraf

Fellow, Berkeley Institute for Data Science

拟合模型前务必先可视化原始数据

Python 中的时间序列机器学习

可视化你的时间序列数据!

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

Python 中的时间序列机器学习

用哪些特征?

  • 原始时间序列用于分类噪声过大
  • 需要计算特征!
  • 简单起步:汇总音频数据
Python 中的时间序列机器学习

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,) 
Python 中的时间序列机器学习

用 scikit-learn 拟合分类器

  • 我们将二维数据集(样本 × 时间)压缩为一维数据集(样本)的多种特征
  • 可将各特征组合,用作模型输入
  • 若每个样本都有标签,可用 scikit-learn 创建并训练分类器
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)
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)  
Python 中的时间序列机器学习

Passons à la pratique !

Python 中的时间序列机器学习

Preparing Video For Download...