資料縮放與 KNN 迴歸

Python 金融 Machine Learning

Nathan George

Data Science Professor

特徵重要度

Python 金融 Machine Learning

特徵選擇:移除星期幾

print(feature_names)
['10d_close_pct',
 '14-day SMA',
 '14-day RSI',
 '200-day SMA',
 '200-day RSI',
 'Adj_Volume_1d_change',
 'Adj_Volume_1d_change_SMA',
 'weekday_1',
 'weekday_2',
 'weekday_3',
 'weekday_4']
print(feature_names[:-4])
['10d_close_pct',
 '14-day SMA',
 '14-day RSI',
 '200-day SMA',
 '200-day RSI',
 'Adj_Volume_1d_change',
 'Adj_Volume_1d_change_SMA']
Python 金融 Machine Learning

移除星期幾

train_features = train_features.iloc[:, :-4]
test_features = test_features.iloc[:, :-4]
Python 金融 Machine Learning

2D 特徵圖

Python 金融 Machine Learning

KNN 預測未知點

Python 金融 Machine Learning

KNN 使用 2 個最近點預測

Python 金融 Machine Learning

Minkowski 距離

Python 金融 Machine Learning

大與小的特徵

Python 金融 Machine Learning

縮放選項

縮放選項:

  • min-max
  • 標準化
  • median-MAD
  • 對應到任意函式(例如 sigmoid、tanh)
Python 金融 Machine Learning

縮放前後的 2D 特徵圖

Python 金融 Machine Learning

sklearn 的 scale

from sklearn.preprocessing import scale

sc = scale()
scaled_train_features = sc.fit_transform(train_features)
scaled_test_features = sc.transform(test_features)
Python 金融 Machine Learning

標準化前後

Python 金融 Machine Learning

建立子圖

# create figure and list containing axes
f, ax = plt.subplots(nrows=2, ncols=1)

# plot histograms of before and after scaling train_features.iloc[:, 2].hist(ax=ax[0]) ax[1].hist(scaled_train_features[:, 2]) plt.show()
Python 金融 Machine Learning

縮放資料並使用 KNN!

Python 金融 Machine Learning

Preparing Video For Download...