Machine Learning for Time Series Data in Python
Chris Holdgraf
Fellow, Berkeley Institute for Data Science
# Audio is a Pandas DataFrame
print(audio.shape)
# (n_times, n_audio_files)
(5000, 20)
# Smooth our data by taking the rolling mean in a window of 50 samples
window_size = 50
windowed = audio.rolling(window=window_size)
audio_smooth = windowed.mean()
First rectify your audio, then smooth it
audio_rectified = audio.apply(np.abs)
audio_envelope = audio_rectified.rolling(50).mean()
# Calculate several features of the envelope, one per sound
envelope_mean = np.mean(audio_envelope, axis=0)
envelope_std = np.std(audio_envelope, axis=0)
envelope_max = np.max(audio_envelope, axis=0)
# Create our training data for a classifier
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
automates the process of: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
is a great library for auditory and timeseries feature engineering# Import librosa and calculate the tempo of a 1-D sound array
import librosa as lr
audio_tempo = lr.beat.tempo(audio, sr=sfreq,
hop_length=2**6, aggregate=None)
Machine Learning for Time Series Data in Python