数据缩放与KNN回归

Python 金融机器学习

Nathan George

Data Science Professor

特征重要性

Python 金融机器学习

特征选择:移除工作日

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 金融机器学习

移除工作日

train_features = train_features.iloc[:, :-4]
test_features = test_features.iloc[:, :-4]
Python 金融机器学习

二维特征图

Python 金融机器学习

KNN 预测未知点

Python 金融机器学习

KNN 用最近2点预测

Python 金融机器学习

闵可夫斯基距离

Python 金融机器学习

大与小特征

Python 金融机器学习

缩放选项

缩放选项:

  • 最小-最大
  • 标准化
  • 中位数-MAD
  • 映射到任意函数(如 sigmoid、tanh)
Python 金融机器学习

缩放前后二维特征图

Python 金融机器学习

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 金融机器学习

标准化前后

Python 金融机器学习

创建子图

# 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 金融机器学习

缩放数据并用 KNN!

Python 金融机器学习

Preparing Video For Download...