建立特徵與目標
我們「幾乎」已經有可用於機器學習的特徵與目標了——特徵來自當前的價格變動(5d_close_pct)與指標(移動平均與 RSI),而目標則是我們建立的未來價格變動(5d_close_future_pct)。現在需要把它們拆成各自的 numpy 陣列,才能餵給機器學習演算法。
由於計算方式的關係,這些指標也會在 DataFrame 的開頭產生遺漏值。我們可以向後填補、以單一數值填補,或是直接刪除列。為了避免機器學習演算法受到回填或以 0 充填資料的影響,刪除這些列是個不錯的選擇。Pandas 提供 .dropna() 函式,我們會用它來刪除任何含有遺漏值的列。
本練習屬於課程
Python 金融 Machine Learning
練習說明
- 使用 pandas 的
.dropna()從lng_df中刪除遺漏值。 - 建立一個包含目標的變數,也就是
'5d_close_future_pct'的值。 - 建立一個同時包含目標(
5d_close_future_pct)與特徵(位於既有清單feature_names中)的 DataFrame,好讓我們檢查相關性。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Drop all na values
lng_df = lng_df.____
# Create features and targets
# use feature_names for features; '5d_close_future_pct' for targets
features = lng_df[feature_names]
targets = lng_df[____]
# Create DataFrame from target column and feature columns
feature_and_target_cols = ['5d_close_future_pct'] + ____
feat_targ_df = lng_df[feature_and_target_cols]
# Calculate correlation matrix
corr = feat_targ_df.corr()
print(corr)