Machine Learning cho Dữ liệu Chuỗi thời gian bằng Python
Chris Holdgraf
Fellow, Berkeley Institute for Data Science



# Audio là một Pandas DataFrame
print(audio.shape)
# (n_times, n_audio_files)
(5000, 20)
# Làm trơn dữ liệu bằng cách lấy trung bình trượt trên cửa sổ 50 mẫu
window_size = 50
windowed = audio.rolling(window=window_size)
audio_smooth = windowed.mean()
Trước tiên chỉnh lưu âm thanh, rồi làm trơn
audio_rectified = audio.apply(np.abs)
audio_envelope = audio_rectified.rolling(50).mean()



# Tính một số đặc trưng của envelope, mỗi âm thanh một giá trị
envelope_mean = np.mean(audio_envelope, axis=0)
envelope_std = np.std(audio_envelope, axis=0)
envelope_max = np.max(audio_envelope, axis=0)
# Tạo dữ liệu huấn luyện cho bộ phân loại
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 tự động hóa quy trình: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 là thư viện tuyệt vời để làm đặc trưng âm thanh và chuỗi thời gian# Import librosa và tính tempo của một mảng âm thanh 1-D
import librosa as lr
audio_tempo = lr.beat.tempo(y=audio, sr=sfreq,
hop_length=2**6)
Machine Learning cho Dữ liệu Chuỗi thời gian bằng Python