資料集偏移

在 Python 設計機器學習工作流程

Dr. Chris Anagnostopoulos

Honorary Associate Professor

什麼是資料集偏移?

elec 資料集:

  • 2 年的資料。
  • class=1 表示相對於過去 24 小時價格「上漲」,0 表示「下跌」。
   day    period  nswprice  ...    vicdemand  transfer  class
0    2  0.000000  0.056443  ...     0.422915  0.414912      1
1    2  0.553191  0.042482  ...     0.422915  0.414912      0
2    2  0.574468  0.044374  ...     0.422915  0.414912      1

[3 rows x 8 columns]
在 Python 設計機器學習工作流程

到底在「偏移」什麼?

兩個類別在二維空間中,由彎曲的決策邊界分隔。

在 Python 設計機器學習工作流程

到底在「偏移」什麼?

兩個類別在二維空間中,由彎曲的決策邊界分隔;旁邊另一張圖類別位置相似,但決策邊界形狀不同。

在 Python 設計機器學習工作流程

視窗

滑動視窗

window = (t_now-window_size+1):t_now
sliding_window = elec.loc[window]

僅突顯近期資料點的資料集。

擴增視窗

window = 0:t_now
expanding_window = elec.loc[window]

終端機中顯示的資料集。

在 Python 設計機器學習工作流程

偵測資料集偏移

# t_now = 40000, window_size = 20000
clf_full = RandomForestClassifier().fit(X, y)
clf_sliding = RandomForestClassifier().fit(sliding_X, sliding_y)
# Use future data as test
test = elec.loc[t_now:elec.shape[0]]
test_X = test.drop('class', 1); test_y = test['class']
roc_auc_score(test_y, clf_full.predict(test_X))
roc_auc_score(test_y, clf_sliding.predict(test_X))
0.775
0.780
在 Python 設計機器學習工作流程

視窗大小

for w_size in range(10, 100, 10):
    sliding = arrh.loc[
      (t_now - w_size + 1):t_now
    ]
    X = sliding.drop('class', 1)
    y = sliding['class']
    clf = GaussianNB()
    clf.fit(X, y)
    preds = clf.predict(test_X)
    roc_auc_score(test_y, preds)

AUC 對視窗大小作圖;在視窗 50 時達到 0.83 的峰值,之後再度下降。

在 Python 設計機器學習工作流程

領域偏移

arrhythmia 資料集:

   age  sex  height  ...    chV6_TwaveAmp  chV6_QRSA  chV6_QRSTA  class
0   75    0     190  ...              2.9       23.3        49.4      0
1   56    1     165  ...              2.1       20.4        38.8      0
2   54    0     172  ...              3.4       12.3        49.0      0
3   55    0     175  ...              2.6       34.6        61.6      1
4   75    0     190  ...              3.9       25.4        62.8      0

[5 rows x 280 columns]
在 Python 設計機器學習工作流程

更多資料不一定更好!

在 Python 設計機器學習工作流程

Preparing Video For Download...