特征选择 Feature Selection - Ex 4: Feature Selection using SelectFromModel

优质
小牛编辑
127浏览
2023-12-01

特征选择/范例四: Feature selection using SelectFromModel and LassoCV

http://scikit-learn.org/stable/auto_examples/feature_selection/plot_select_from_model_boston.html

此范例是示范以LassoCV来挑选特征,Lasso是一种用来计算稀疏矩阵的线性模形。在某些情况下是非常有用的,因为在此演算过程中会以较少数的特征来找最佳解,基于参数有相依性的情况下,使变数的数目有效的缩减。因此,Lasso法以及它的变形式可算是压缩参数关係基本方法。在某些情况下,此方法可以准确的侦测非零权重的值。

Lasso最佳化的目标函数:

Ex 4: Feature Selection using SelectFromModel - 图1

  1. LassoCV法来计算目标资讯性特征数目较少的资料
  2. SelectFromModel设定特征重要性的门槛值来选择特征
  3. 提高SelectFromModel.threshold使目标资讯性特征数逼近预期的数目

(一)取得波士顿房产资料

  1. from sklearn.datasets import load_boston
  2. from sklearn.feature_selection import SelectFromModel
  3. from sklearn.linear_model import LassoCV
  4. # Load the boston dataset.
  5. boston = load_boston()
  6. X, y = boston['data'], boston['target']

(二)使用LassoCV功能来筛选具有影响力的特征

  1. 由于资料的类型为连续数字,选用LassoCV来做最具有代表性的特征选取。
  2. 当设定好门槛值,并做训练后,可以用transform(X)取得计算过后,被认为是具有影响力的特征以及对应的样本,可以由其列的数目知道总影响力特征有几个。
  3. 后面使用了增加门槛值来达到限制最后特征数目的
  4. 使用门槛值来决定后来选取的参数,其说明在下一个标题。
  5. 需要用后设转换

(三)设定选取参数的门槛值

  1. while n_features > 2:
  2. sfm.threshold += 0.1
  3. X_transform = sfm.transform(X)
  4. n_features = X_transform.shape[1]

(四)原始码之出处

Python source code: plot_select_from_model_boston.py

  1. # Author: Manoj Kumar <mks542@nyu.edu>
  2. # License: BSD 3 clause
  3. print(__doc__)
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. from sklearn.datasets import load_boston
  7. from sklearn.feature_selection import SelectFromModel
  8. from sklearn.linear_model import LassoCV
  9. # Load the boston dataset.
  10. boston = load_boston()
  11. X, y = boston['data'], boston['target']
  12. # We use the base estimator LassoCV since the L1 norm promotes sparsity of features.
  13. clf = LassoCV()
  14. # Set a minimum threshold of 0.25
  15. sfm = SelectFromModel(clf, threshold=0.25)
  16. sfm.fit(X, y)
  17. n_features = sfm.transform(X).shape[1]
  18. # Reset the threshold till the number of features equals two.
  19. # Note that the attribute can be set directly instead of repeatedly
  20. # fitting the metatransformer.
  21. while n_features > 2:
  22. sfm.threshold += 0.1
  23. X_transform = sfm.transform(X)
  24. n_features = X_transform.shape[1]
  25. # Plot the selected two features from X.
  26. plt.title(
  27. "Features selected from Boston using SelectFromModel with "
  28. "threshold %0.3f." % sfm.threshold)
  29. feature1 = X_transform[:, 0]
  30. feature2 = X_transform[:, 1]
  31. plt.plot(feature1, feature2, 'r.')
  32. plt.xlabel("Feature number 1")
  33. plt.ylabel("Feature number 2")
  34. plt.ylim([np.min(feature2), np.max(feature2)])
  35. plt.show()