開始使用免費開始

從成交量做特徵工程

我們將使用非線性模型來提升預測準確度。使用線性模型時,特徵必須與目標呈線性相關。其他機器學習模型則能以非線性的方式組合特徵。例如,若價格的移動平均正在上升,而成交量的移動平均正在下降,價格就可能上漲。要捕捉這類互動,要嘛將特徵相乘,要嘛使用能處理非線性的機器學習演算法(例如 random forests)。

為了納入更多可能與其他特徵互動的資訊,我們可以加入弱相關的特徵。首先,我們會把成交量資料加進來,這在 lng_dfAdj_Volume 欄位中。

開始之前請記得,TA-Lib 的函式(例如 SMA())需要的是 Numpy 陣列,而不是 pandas 物件。你可以使用 pandas Series 或 DataFrame 的 .values 屬性把它轉成 Numpy 陣列。

本練習屬於課程

Python 金融 Machine Learning

檢視課程

練習說明

  • 建立 1 天的成交量報酬(使用 pandas 的 pct_change()),並指定到 lng_dfAdj_Volume_1d_change 欄位。
  • 建立該 1 天成交量報酬的 5 天移動平均,並指定到 lng_dfAdj_Volume_1d_change_SMA 欄位。
  • 使用 new_features 清單繪製這兩個新特徵的直方圖。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create 2 new volume features, 1-day % change and 5-day SMA of the % change
new_features = ['Adj_Volume_1d_change', 'Adj_Volume_1d_change_SMA']
feature_names.extend(new_features)
lng_df[____] = lng_df['Adj_Volume'].____
lng_df[____] = talib.SMA(____[____].____,
                        timeperiod=____)

# Plot histogram of volume % change data
lng_df[____].plot(kind='hist', sharex=False, bins=50)
plt.show()
編輯並執行程式碼