Python 的時間序列資料機器學習
Chris Holdgraf
Fellow, Berkeley Institute for Data Science
array.shape
(10, 5)
array[:3]
array([[ 0.735528 , 1.00122818, -0.28315978],
[-0.94478393, 0.18658748, -0.00241224],
[-0.74822942, -1.46636618, 0.69835096]])
df.head()
col1 col2 col3
0 0.735528 1.001228 -0.283160
1 -0.944784 0.186587 -0.002412
2 -0.748229 -1.466366 0.698351
3 1.038589 -0.171248 0.831457
4 -0.161904 0.003972 -0.321933
確認它看起來符合預期。
# 使用 matplotlib
fig, ax = plt.subplots()
ax.plot(...)
# 使用 pandas
fig, ax = plt.subplots()
df.plot(..., ax=ax)
Scikit-learn 是 Python 中最熱門的機器學習函式庫
from sklearn.svm import LinearSVC
scikit-learn 需要特定的資料結構:
(samples, features)確保資料「至少是二維」
確保第一個維度是「samples」
array.T.shape
(10, 3)
.reshape(): array.shape
(10,)
array.reshape(-1, 1).shape
(10, 1)
-1 會自動以剩餘元素推算該軸大小# 匯入支援向量分類器
from sklearn.svm import LinearSVC
# 建立模型實例
model = LinearSVC()
# 以資料訓練模型
model.fit(X, y)
y 常見的形狀為 (samples, 1)。
# 每個輸入特徵對應一個係數
model.coef_
array([[ 0.69417875, -0.5289162 ]])
# 產生預測
predictions = model.predict(X_test)
Python 的時間序列資料機器學習