特征选择 Feature Selection - Ex 6: Univariate Feature Selection

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

特征选择/范例六: Univariate Feature Selection

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

此范例示范单变量特征的选择。鸢尾花资料中会加入数个杂讯特征(不具影响力的特征资讯)并且选择单变量特征。选择过程会画出每个特征的 p-value 与其在支持向量机中的权重。可以从图表中看出主要影响力特征的选择会选出具有主要影响力的特征,并且这些特征会在支持向量机有相当大的权重。
在本范例的所有特征中,只有最前面的四个特征是对目标有意义的。我们可以看到这些特征的单变量特征评分很高。而支持向量机会赋予最主要的权重到这些具影响力的特征之一,但也会挑选剩下的特征来做判断。在支持向量机增加权重之前就确定那些特征较具有影响力,从而增加辨识率。

  1. 资料集:鸢尾花
  2. 特征:萼片(sepal)之长与宽以及花瓣(petal)之长与宽
  3. 预测目标:共有三种鸢尾花 setosa, versicolor, virginica
  4. 机器学习方法:线性分类
  5. 探讨重点:使用单变量选择(SelectPercentile)挑出训练特征,与直接将所有训练特征输入的分类器做比较
  6. 关键函式: sklearn.feature_selection.SelectPercentile

(一)修改原本的鸢尾花资料

datasets.load_iris()读取鸢尾花的资料做为具有影响力的特征,并以np.random.uniform建立二十个随机资料做为不具影响力的特征,并合併做为训练样本。

  1. # import some data to play with
  2. # The iris dataset
  3. iris = datasets.load_iris()
  4. # Some noisy data not correlated
  5. E = np.random.uniform(0, 0.1, size=(len(iris.data), 20))
  6. # Add the noisy data to the informative features
  7. X = np.hstack((iris.data, E))
  8. y = iris.target

(二)使用f-value作为判断的基准来找主要影响力特征

SelectPercentile作单变量特征的计算,以F-test(f_classif)来做为选择的统计方式,挑选函式输出结果大于百分之十的特征。并将计算出来的单便量特征分数结果做正规化,以便比较每特征在使用单变量计算与未使用单变量计算的差别。

  1. ###############################################################################
  2. # Univariate feature selection with F-test for feature scoring
  3. # We use the default selection function: the 10% most significant features
  4. selector = SelectPercentile(f_classif, percentile=10)
  5. selector.fit(X, y)
  6. scores = -np.log10(selector.pvalues_)
  7. scores /= scores.max()
  8. plt.bar(X_indices - .45, scores, width=.2,
  9. label=r'Univariate score ($-Log(p_{value})$)', color='g')

(三)找出不计算单变量特征的分类权重

以所有特征资料,以线性核函数丢入支持向量分类机,找出各特征的权重。

  1. ###############################################################################
  2. # Compare to the weights of an SVM
  3. clf = svm.SVC(kernel='linear')
  4. clf.fit(X, y)
  5. svm_weights = (clf.coef_ ** 2).sum(axis=0)
  6. svm_weights /= svm_weights.max()
  7. plt.bar(X_indices - .25, svm_weights, width=.2, label='SVM weight', color='r')

(四)找出以单变量特征选出的分类权重

以单变量特征选择选出的特征,做为分类的训练特征,差别在于训练的特征资料是使用selector.transform(X)SelectPercentile选择的结果读取出来,并算出以单变量特征选择做预先选择后,该分类器的判断权重。

  1. clf_selected = svm.SVC(kernel='linear')
  2. clf_selected.fit(selector.transform(X), y)
  3. svm_weights_selected = (clf_selected.coef_ ** 2).sum(axis=0)
  4. svm_weights_selected /= svm_weights_selected.max()
  5. plt.bar(X_indices[selector.get_support()] - .05, svm_weights_selected,
  6. width=.2, label='SVM weights after selection', color='b')

(五)原始码出处

Python source code: plot_feature_selection.py

  1. print(__doc__)
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from sklearn import datasets, svm
  5. from sklearn.feature_selection import SelectPercentile, f_classif
  6. ###############################################################################
  7. # import some data to play with
  8. # The iris dataset
  9. iris = datasets.load_iris()
  10. # Some noisy data not correlated
  11. E = np.random.uniform(0, 0.1, size=(len(iris.data), 20))
  12. # Add the noisy data to the informative features
  13. X = np.hstack((iris.data, E))
  14. y = iris.target
  15. ###############################################################################
  16. plt.figure(1)
  17. plt.clf()
  18. X_indices = np.arange(X.shape[-1])
  19. ###############################################################################
  20. # Univariate feature selection with F-test for feature scoring
  21. # We use the default selection function: the 10% most significant features
  22. selector = SelectPercentile(f_classif, percentile=10)
  23. selector.fit(X, y)
  24. scores = -np.log10(selector.pvalues_)
  25. scores /= scores.max()
  26. plt.bar(X_indices - .45, scores, width=.2,
  27. label=r'Univariate score ($-Log(p_{value})$)', color='g')
  28. ###############################################################################
  29. # Compare to the weights of an SVM
  30. clf = svm.SVC(kernel='linear')
  31. clf.fit(X, y)
  32. svm_weights = (clf.coef_ ** 2).sum(axis=0)
  33. svm_weights /= svm_weights.max()
  34. plt.bar(X_indices - .25, svm_weights, width=.2, label='SVM weight', color='r')
  35. clf_selected = svm.SVC(kernel='linear')
  36. clf_selected.fit(selector.transform(X), y)
  37. svm_weights_selected = (clf_selected.coef_ ** 2).sum(axis=0)
  38. svm_weights_selected /= svm_weights_selected.max()
  39. plt.bar(X_indices[selector.get_support()] - .05, svm_weights_selected,
  40. width=.2, label='SVM weights after selection', color='b')
  41. plt.title("Comparing feature selection")
  42. plt.xlabel('Feature number')
  43. plt.yticks(())
  44. plt.axis('tight')
  45. plt.legend(loc='upper right')
  46. plt.show()