当前位置:   article > 正文

基于随机优化算法的特征选择_优化算法 特征选择

优化算法 特征选择

      【翻译自: Feature Selection with Stochastic Optimization Algorithms

      【说明:Jason Brownlee PhD大神的文章个人很喜欢,所以闲暇时间里会做一点翻译和学习实践的工作,这里是相应工作的实践记录,希望能帮到有需要的人!】

       通常,可以通过从训练数据集中删除输入特征(列)来开发更简单,性能更好的机器学习模型。这称为特征选择,可以使用许多不同类型的算法。可以将特征选择问题框架为优化问题。在输入要素很少的情况下,可以评估输入要素的所有可能组合,并确定地找到最佳子集。在输入特征数量众多的情况下,可以使用随机优化算法来探索搜索空间并找到特征的有效子集。

       在本教程中,您将发现如何在机器学习中使用优化算法进行特征选择。完成本教程后,您将知道:

  1. 特征选择的问题可以广义地定义为优化问题。
  2. 如何枚举数据集输入要素的所有可能子集。
  3. 如何应用随机优化来选择输入要素的最佳子集。

教程概述

本教程分为三个部分:他们是:

  1. 优化特征选择
  2. 枚举所有功能子集
  3. 优化功能子集
  4. 优化特征选择

      特征选择是在开发预测模型时减少输入变量数量的过程。

     希望减少输入变量的数量,以减少建模的计算成本,并且在某些情况下,还需要改善模型的性能。尽管可以将特征选择算法大致分为两种主要类型,但它们有很多不同的类型:包装器和过滤器方法。包装器特征选择方法会创建许多具有不同输入特征子集的模型,并根据性能指标选择那些导致最佳性能模型的特征。这些方法与变量类型无关,尽管它们在计算上可能很昂贵。 RFE是包装功能选择方法的一个很好的例子。过滤器特征选择方法使用统计技术来评估每个输入变量和目标变量之间的关系,这些得分将用作选择(过滤)将在模型中使用的那些输入变量的基础。

  1. 包装特征选择:搜索性能良好的特征子集。
  2. 过滤特征选择:根据特征子集与目标的关系选择特征子集。

    流行的包装方法是递归特征消除算法(RFE)。RFE的工作方式是:从训练数据集中的所有要素开始搜索要素的子集,然后成功删除要素,直到保留所需数量为止。这可以通过拟合模型核心中使用的给定机器学习算法,按重要性对特征进行排序,丢弃最不重要的特征以及重新拟合模型来实现。 重复此过程,直到保留指定数量的功能。

   下面是两篇相关的博文资料,感兴趣的可以一并阅读:

         如何使用机器学习选择特征

        递归特征消除(RFE)用于Python中的特征选择

      包装器特征选择的问题可被视为优化问题。 也就是说,找到可带来最佳模型性能的输入要素子集。RFE是一种系统解决此问题的方法,尽管它可能会受到众多功能的限制。当特征的数量很大时,另一种方法是使用随机优化算法,例如随机爬山算法。 当特征的数量相对较小时,可能会枚举所有可能的特征子集。

  1. 少量输入变量:枚举要素的所有可能子集。
  2. 许多输入要素:随机优化算法,用于查找要素的良好子集。

      既然我们熟悉了将特征选择作为一个优化问题来探讨的想法,那么让我们看一下如何枚举所有可能的特征子集。

枚举所有功能子集

     当输入变量的数量相对较小且模型评估相对较快时,则可能会枚举输入变量的所有可能子集。这意味着在给定每个可能的唯一输入变量组的情况下,使用测试工具评估模型的性能。我们将通过一个可行的示例探索如何做到这一点。首先,让我们定义一个小的二进制分类数据集,其中包含很少的输入功能。 我们可以使用make_classification()函数定义一个具有五个输入变量的数据集,其中两个是信息变量,并且有1,000行。下面的示例定义了数据集并总结了其形状。

  1. # define a small classification dataset
  2. from sklearn.datasets import make_classification
  3. # define dataset
  4. X, y = make_classification(n_samples=1000, n_features=5, n_informative=2, n_redundant=3, random_state=1)
  5. # summarize the shape of the dataset
  6. print(X.shape, y.shape)

      运行示例将创建数据集并确认其具有所需的形状。

(1000, 5) (1000,)

      接下来,我们可以使用对整个数据集评估的模型来建立性能基准。我们将使用DecisionTreeClassifier作为模型,因为它的性能对输入变量的选择非常敏感。我们将使用良好的实践来评估模型,例如具有三个重复和10折的重复分层k折交叉验证。下面列出了完整的示例。

  1. # evaluate a decision tree on the entire small dataset
  2. from numpy import mean
  3. from numpy import std
  4. from sklearn.datasets import make_classification
  5. from sklearn.model_selection import cross_val_score
  6. from sklearn.model_selection import RepeatedStratifiedKFold
  7. from sklearn.tree import DecisionTreeClassifier
  8. # define dataset
  9. X, y = make_classification(n_samples=1000, n_features=3, n_informative=2, n_redundant=1, random_state=1)
  10. # define model
  11. model = DecisionTreeClassifier()
  12. # define evaluation procedure
  13. cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
  14. # evaluate model
  15. scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
  16. # report result
  17. print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores)))

      运行示例将评估整个数据集上的决策树,并报告均值和标准差分类准确性。

      注意:由于算法或评估程序的随机性,或者数值精度不同,您的结果可能会有所不同。 考虑运行该示例几次并比较平均结果。在这种情况下,我们可以看到该模型实现了约80.5%的精度。

Mean Accuracy: 0.805 (0.030)

       接下来,我们可以尝试通过使用输入功能的子集来改善模型性能。首先,我们必须选择一种表示方式进行枚举。在这种情况下,我们将枚举一组布尔值,每个输入要素都有一个值:如果要使用该要素,则为True;如果不将该要素用作输入,则为False。例如,对于五个输入要素,序列[True,True,True,True,True]将使用所有输入要素,而[True,False,False,False,False,False]仅将第一个输入要素用作输入。我们可以使用product()Python函数枚举length = 5的所有布尔值序列。 我们必须指定有效值[True,False]和序列中的步数,该步数等于输入变量的数量。该函数返回一个可迭代的函数,我们可以直接为每个序列枚举。

  1. # determine the number of columns
  2. n_cols = X.shape[1]
  3. best_subset, best_score = None, 0.0
  4. # enumerate all combinations of input features
  5. for subset in product([True, False], repeat=n_cols):

       对于给定的布尔值序列,我们可以对其进行枚举并将其转换为该序列中每个True的列索引序列。

  1. # convert into column indexes
  2. ix = [i for i, x in enumerate(subset) if x]

      如果序列没有列索引(对于所有False值),那么我们可以跳过该序列。

  1. # check for now column (all False)
  2. if len(ix) == 0:
  3. continue

      然后,我们可以使用列索引来选择数据集中的列。

  1. # select columns
  2. X_new = X[:, ix]

      然后可以像以前一样评估数据集的此子集。

  1. # define model
  2. model = DecisionTreeClassifier()
  3. # define evaluation procedure
  4. cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
  5. # evaluate model
  6. scores = cross_val_score(model, X_new, y, scoring='accuracy', cv=cv, n_jobs=-1)
  7. # summarize scores
  8. result = mean(scores)

     如果模型的准确性优于到目前为止找到的最佳序列,则可以存储它。

  1. # check if it is better than the best so far
  2. if best_score is None or result >= best_score:
  3. # better result
  4. best_subset, best_score = ix, result

      就是这样。结合在一起,下面列出了通过枚举所有可能的特征子集进行特征选择的完整示例。

  1. # feature selection by enumerating all possible subsets of features
  2. from itertools import product
  3. from numpy import mean
  4. from sklearn.datasets import make_classification
  5. from sklearn.model_selection import cross_val_score
  6. from sklearn.model_selection import RepeatedStratifiedKFold
  7. from sklearn.tree import DecisionTreeClassifier
  8. # define dataset
  9. X, y = make_classification(n_samples=1000, n_features=5, n_informative=2, n_redundant=3, random_state=1)
  10. # determine the number of columns
  11. n_cols = X.shape[1]
  12. best_subset, best_score = None, 0.0
  13. # enumerate all combinations of input features
  14. for subset in product([True, False], repeat=n_cols):
  15. # convert into column indexes
  16. ix = [i for i, x in enumerate(subset) if x]
  17. # check for now column (all False)
  18. if len(ix) == 0:
  19. continue
  20. # select columns
  21. X_new = X[:, ix]
  22. # define model
  23. model = DecisionTreeClassifier()
  24. # define evaluation procedure
  25. cv = RepeatedStratifiedKFold(n_splits=10, n_repeats=3, random_state=1)
  26. # evaluate model
  27. scores = cross_val_score(model, X_new, y, scoring='accuracy', cv=cv, n_jobs=-1)
  28. # summarize scores
  29. result = mean(scores)
  30. # report progress
  31. print('>f(%s) = %f ' % (ix, result))
  32. # check if it is better than the best so far
  33. if best_score is None or result >= best_score:
  34. # better result
  35. best_subset, best_score = ix, result
  36. # report best
  37. print('Done!')
  38. print('f(%s) = %f' % (best_subset, best_score))

      运行示例将报告所考虑特征的每个子集的模型的平均分类精度。 然后在运行结束时报告最佳子集。

     注意:由于算法或评估程序的随机性,或者数值精度不同,您的结果可能会有所不同。 考虑运行该示例几次并比较平均结果。在这种情况下,我们可以看到要素的最佳子集涉及索引[2、3、4]处的要素,这些要素的平均分类精度约为83.0%,这比以前使用所有输入要素报告的结果要好。

  1. >f([0, 1, 2, 3, 4]) = 0.813667
  2. >f([0, 1, 2, 3]) = 0.827667
  3. >f([0, 1, 2, 4]) = 0.815333
  4. >f([0, 1, 2]) = 0.824000
  5. >f([0, 1, 3, 4]) = 0.821333
  6. >f([0, 1, 3]) = 0.825667
  7. >f([0, 1, 4]) = 0.807333
  8. >f([0, 1]) = 0.817667
  9. >f([0, 2, 3, 4]) = 0.830333
  10. >f([0, 2, 3]) = 0.819000
  11. >f([0, 2, 4]) = 0.828000
  12. >f([0, 2]) = 0.818333
  13. >f([0, 3, 4]) = 0.830333
  14. >f([0, 3]) = 0.821333
  15. >f([0, 4]) = 0.816000
  16. >f([0]) = 0.639333
  17. >f([1, 2, 3, 4]) = 0.823667
  18. >f([1, 2, 3]) = 0.821667
  19. >f([1, 2, 4]) = 0.823333
  20. >f([1, 2]) = 0.818667
  21. >f([1, 3, 4]) = 0.818000
  22. >f([1, 3]) = 0.820667
  23. >f([1, 4]) = 0.809000
  24. >f([1]) = 0.797000
  25. >f([2, 3, 4]) = 0.827667
  26. >f([2, 3]) = 0.755000
  27. >f([2, 4]) = 0.827000
  28. >f([2]) = 0.516667
  29. >f([3, 4]) = 0.824000
  30. >f([3]) = 0.514333
  31. >f([4]) = 0.777667
  32. Done!
  33. f([0, 3, 4]) = 0.830333

       现在,我们知道了如何枚举所有可能的特征子集,让我们看一下如何使用随机优化算法选择特征子集。

优化特征子集

     我们可以将随机优化算法应用于输入特征子集的搜索空间。首先,让我们定义一个更大的问题,该问题具有更多功能,这会使模型评估速度太慢,并且搜索空间太大,无法枚举所有子集。我们将定义一个具有10,000行和500个输入要素的分类问题,其中10个是相关的,其余490个是多余的。

  1. # define a large classification dataset
  2. from sklearn.datasets import make_classification
  3. # define dataset
  4. X, y = make_classification(n_samples=10000, n_features=500, n_informative=10, n_redundant=490, random_state=1)
  5. # summarize the shape of the dataset
  6. print(X.shape, y.shape)

       运行示例将创建数据集并确认其具有所需的形状。

(10000, 500) (10000,)

      我们可以通过评估具有所有输入特征的数据集上的模型来建立性能基准。由于数据集很大且模型评估缓慢,因此我们将修改模型的评估,以使用3倍交叉验证,例如 更少的褶皱,没有重复。下面列出了完整的示例。

  1. # evaluate a decision tree on the entire larger dataset
  2. from numpy import mean
  3. from numpy import std
  4. from sklearn.datasets import make_classification
  5. from sklearn.model_selection import cross_val_score
  6. from sklearn.model_selection import StratifiedKFold
  7. from sklearn.tree import DecisionTreeClassifier
  8. # define dataset
  9. X, y = make_classification(n_samples=10000, n_features=500, n_informative=10, n_redundant=490, random_state=1)
  10. # define model
  11. model = DecisionTreeClassifier()
  12. # define evaluation procedure
  13. cv = StratifiedKFold(n_splits=3)
  14. # evaluate model
  15. scores = cross_val_score(model, X, y, scoring='accuracy', cv=cv, n_jobs=-1)
  16. # report result
  17. print('Mean Accuracy: %.3f (%.3f)' % (mean(scores), std(scores)))

       运行示例将评估整个数据集上的决策树,并报告均值和标准差分类准确性。

      注意:由于算法或评估程序的随机性,或者数值精度不同,您的结果可能会有所不同。 考虑运行该示例几次并比较平均结果。在这种情况下,我们可以看到该模型的准确度约为91.3%。这提供了使用功能选择我们预期会胜过的基准。

Mean Accuracy: 0.913 (0.001)

      我们将使用简单的随机爬山算法作为优化算法。首先,我们必须定义目标函数。 它将数据集和要素子集用作输入,并返回估计的模型准确度,范围从0(最差)到1(最佳)。 这是一个最大化的优化问题。这个目标函数只是对上一节中序列和模型评估步骤的解码。下面的Objective()函数实现了此目的,并返回了得分和用于帮助报告的列的已解码子集。

  1. # objective function
  2. def objective(X, y, subset):
  3. # convert into column indexes
  4. ix = [i for i, x in enumerate(subset) if x]
  5. # check for now column (all False)
  6. if len(ix) == 0:
  7. return 0.0
  8. # select columns
  9. X_new = X[:, ix]
  10. # define model
  11. model = DecisionTreeClassifier()
  12. # evaluate model
  13. scores = cross_val_score(model, X_new, y, scoring='accuracy', cv=3, n_jobs=-1)
  14. # summarize scores
  15. result = mean(scores)
  16. return result, ix

       我们还需要一个可以在搜索空间中迈出一步的函数。给定一个现有的解决方案,它必须对其进行修改并返回一个新的解决方案。 在这种情况下,我们将通过随机翻转子序列中列的包含/排除来实现此目的。序列中的每个位置都将被独立考虑,并且在翻转的概率为超参数的情况下,概率将被翻转。下面的mutate()函数在给定的候选解决方案(布尔序列)和突变超参数的情况下实现了这一点,创建并返回了修改后的解决方案(搜索空间中的步骤)。p_mutate值越大(在0到1的范围内),搜索空间中的步长越大。

  1. # mutation operator
  2. def mutate(solution, p_mutate):
  3. # make a copy
  4. child = solution.copy()
  5. for i in range(len(child)):
  6. # check for a mutation
  7. if rand() < p_mutate:
  8. # flip the inclusion
  9. child[i] = not child[i]
  10. return child

      现在,我们可以实现爬山算法。初始解决方案是随机生成的序列,然后对其进行评估。

  1. # generate an initial point
  2. solution = choice([True, False], size=X.shape[1])
  3. # evaluate the initial point
  4. solution_eval, ix = objective(X, y, solution)

       然后,我们循环进行固定次数的迭代,创建当前解决方案的变异版本,对其进行评估,并在分数更高时保存它们。

  1. # run the hill climb
  2. for i in range(n_iter):
  3. # take a step
  4. candidate = mutate(solution, p_mutate)
  5. # evaluate candidate point
  6. candidate_eval, ix = objective(X, y, candidate)
  7. # check if we should keep the new point
  8. if candidate_eval >= solution_eval:
  9. # store the new point
  10. solution, solution_eval = candidate, candidate_eval
  11. # report progress
  12. print('>%d f(%s) = %f' % (i+1, len(ix), solution_eval))

       下面的hillclimbing()函数以数据集,目标函数和超参数作为参数来实现此目的,并返回数据集列的最佳子集和模型的估计性能。

  1. # hill climbing local search algorithm
  2. def hillclimbing(X, y, objective, n_iter, p_mutate):
  3. # generate an initial point
  4. solution = choice([True, False], size=X.shape[1])
  5. # evaluate the initial point
  6. solution_eval, ix = objective(X, y, solution)
  7. # run the hill climb
  8. for i in range(n_iter):
  9. # take a step
  10. candidate = mutate(solution, p_mutate)
  11. # evaluate candidate point
  12. candidate_eval, ix = objective(X, y, candidate)
  13. # check if we should keep the new point
  14. if candidate_eval >= solution_eval:
  15. # store the new point
  16. solution, solution_eval = candidate, candidate_eval
  17. # report progress
  18. print('>%d f(%s) = %f' % (i+1, len(ix), solution_eval))
  19. return solution, solution_eval

     然后,我们可以调用此函数并传入我们的综合数据集以对特征选择进行优化。在这种情况下,我们将对算法进行100次迭代,并对给定突变的序列进行约五次翻转,这是非常保守的。

  1. # define dataset
  2. X, y = make_classification(n_samples=10000, n_features=500, n_informative=10, n_redundant=490, random_state=1)
  3. # define the total iterations
  4. n_iter = 100
  5. # probability of including/excluding a column
  6. p_mut = 10.0 / 500.0
  7. # perform the hill climbing search
  8. subset, score = hillclimbing(X, y, objective, n_iter, p_mut)

       在运行结束时,我们将布尔序列转换为列索引(因此,如果需要,我们可以拟合最终模型),并报告最佳子序列的性能。

  1. # convert into column indexes
  2. ix = [i for i, x in enumerate(subset) if x]
  3. print('Done!')
  4. print('Best: f(%d) = %f' % (len(ix), score))

     结合在一起,下面列出了完整的示例。

  1. # stochastic optimization for feature selection
  2. from numpy import mean
  3. from numpy.random import rand
  4. from numpy.random import choice
  5. from sklearn.datasets import make_classification
  6. from sklearn.model_selection import cross_val_score
  7. from sklearn.tree import DecisionTreeClassifier
  8. # objective function
  9. def objective(X, y, subset):
  10. # convert into column indexes
  11. ix = [i for i, x in enumerate(subset) if x]
  12. # check for now column (all False)
  13. if len(ix) == 0:
  14. return 0.0
  15. # select columns
  16. X_new = X[:, ix]
  17. # define model
  18. model = DecisionTreeClassifier()
  19. # evaluate model
  20. scores = cross_val_score(model, X_new, y, scoring='accuracy', cv=3, n_jobs=-1)
  21. # summarize scores
  22. result = mean(scores)
  23. return result, ix
  24. # mutation operator
  25. def mutate(solution, p_mutate):
  26. # make a copy
  27. child = solution.copy()
  28. for i in range(len(child)):
  29. # check for a mutation
  30. if rand() < p_mutate:
  31. # flip the inclusion
  32. child[i] = not child[i]
  33. return child
  34. # hill climbing local search algorithm
  35. def hillclimbing(X, y, objective, n_iter, p_mutate):
  36. # generate an initial point
  37. solution = choice([True, False], size=X.shape[1])
  38. # evaluate the initial point
  39. solution_eval, ix = objective(X, y, solution)
  40. # run the hill climb
  41. for i in range(n_iter):
  42. # take a step
  43. candidate = mutate(solution, p_mutate)
  44. # evaluate candidate point
  45. candidate_eval, ix = objective(X, y, candidate)
  46. # check if we should keep the new point
  47. if candidate_eval >= solution_eval:
  48. # store the new point
  49. solution, solution_eval = candidate, candidate_eval
  50. # report progress
  51. print('>%d f(%s) = %f' % (i+1, len(ix), solution_eval))
  52. return solution, solution_eval
  53. # define dataset
  54. X, y = make_classification(n_samples=10000, n_features=500, n_informative=10, n_redundant=490, random_state=1)
  55. # define the total iterations
  56. n_iter = 100
  57. # probability of including/excluding a column
  58. p_mut = 10.0 / 500.0
  59. # perform the hill climbing search
  60. subset, score = hillclimbing(X, y, objective, n_iter, p_mut)
  61. # convert into column indexes
  62. ix = [i for i, x in enumerate(subset) if x]
  63. print('Done!')
  64. print('Best: f(%d) = %f' % (len(ix), score))

     运行示例将报告所考虑特征的每个子集的模型的平均分类精度。 然后在运行结束时报告最佳子集。

     注意:由于算法或评估程序的随机性,或者数值精度不同,您的结果可能会有所不同。 考虑运行该示例几次并比较平均结果。在这种情况下,我们可以看到,通过239个特征的子集和大约91.8%的分类精度,可以实现最佳性能。这比在所有输入要素上评估的模型要好。尽管结果更好,但我们知道我们可以做得更好,可能是优化优化算法的超参数,或者是使用替代优化算法。

  1. >80 f(240) = 0.918099
  2. >81 f(236) = 0.918099
  3. >82 f(238) = 0.918099
  4. >83 f(236) = 0.918099
  5. >84 f(239) = 0.918099
  6. >85 f(240) = 0.918099
  7. >86 f(239) = 0.918099
  8. >87 f(245) = 0.918099
  9. >88 f(241) = 0.918099
  10. >89 f(239) = 0.918099
  11. >90 f(239) = 0.918099
  12. >91 f(241) = 0.918099
  13. >92 f(243) = 0.918099
  14. >93 f(245) = 0.918099
  15. >94 f(239) = 0.918099
  16. >95 f(245) = 0.918099
  17. >96 f(244) = 0.918099
  18. >97 f(242) = 0.918099
  19. >98 f(238) = 0.918099
  20. >99 f(248) = 0.918099
  21. >100 f(238) = 0.918099
  22. Done!
  23. Best: f(239) = 0.918099

    最后给出来一些相关的教程和可供参考学习的API链接。

相关教程
 

递归特征消除(RFE)用于Python中的特征选择
如何为机器学习选择特征选择方法

接口

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/运维做开发/article/detail/871974
推荐阅读
相关标签
  

闽ICP备14008679号