Python 的時間序列資料機器學習
Chris Holdgraf
Fellow, Berkeley Institute for Data Science



# Audio 是一個 Pandas DataFrame
print(audio.shape)
# (n_times, n_audio_files)
(5000, 20)
# 以 50 個樣本的視窗取移動平均,讓資料更平滑
window_size = 50
windowed = audio.rolling(window=window_size)
audio_smooth = windowed.mean()
先將音訊做「整流」,再進行平滑
audio_rectified = audio.apply(np.abs)
audio_envelope = audio_rectified.rolling(50).mean()



# 為每個聲音計算包絡的多個特徵
envelope_mean = np.mean(audio_envelope, axis=0)
envelope_std = np.std(audio_envelope, axis=0)
envelope_max = np.max(audio_envelope, axis=0)
# 建立分類器的訓練資料
X = np.column_stack([envelope_mean, envelope_std, envelope_max])
X = np.column_stack([envelope_mean, envelope_std, envelope_max])
y = labels.reshape(-1, 1)
cross_val_score 自動化以下流程:from sklearn.model_selection import cross_val_score
model = LinearSVC()
scores = cross_val_score(model, X, y, cv=3)
print(scores)
[0.60911642 0.59975305 0.61404035]
librosa 很適合做聽覺與時間序列特徵工程# 匯入 librosa 並計算 1 維聲音陣列的節奏
import librosa as lr
audio_tempo = lr.beat.tempo(y=audio, sr=sfreq,
hop_length=2**6)
Python 的時間序列資料機器學習