特征工程学习01-sklearn单机特征工程

特征工程学习01-sklearn单机特征工程

小书匠 kindle

0.数据的导入

  1. from sklearn.datasets import load_iris



  2. #导入IRIS数据集 

  3. iris=load_iris() 


  4. #特征矩阵 

  5. print(iris.data[:5],len(iris.data)) 


  6. #目标向量 

  7. print(iris.target[:5],len(iris.target)) 

  1. [[ 5.1 3.5 1.4 0.2]


  2. [ 4.9 3. 1.4 0.2] 

  3. [ 4.7 3.2 1.3 0.2] 

  4. [ 4.6 3.1 1.5 0.2] 

  5. [ 5. 3.6 1.4 0.2]] 150 

  6. [0 0 0 0 0] 150 

1.数据预处理

1.1无量纲化

1.1.1标准化

  1. from sklearn.preprocessing import StandardScaler



  2. #标准化,返回值为标准化后的值 

  3. iris_standar=StandardScaler().fit_transform(iris.data) 

  4. print(iris_standar[:5]) 

  1. [[-0.90068117 1.03205722 -1.3412724 -1.31297673]


  2. [-1.14301691 -0.1249576 -1.3412724 -1.31297673] 

  3. [-1.38535265 0.33784833 -1.39813811 -1.31297673] 

  4. [-1.50652052 0.10644536 -1.2844067 -1.31297673] 

  5. [-1.02184904 1.26346019 -1.3412724 -1.31297673]] 

1.1.2区间缩放

  1. from sklearn.preprocessing import MinMaxScaler



  2. # 区间缩放,返回值为已经缩放到[0,1]的值 

  3. iris_minmax=MinMaxScaler().fit_transform(iris.data) 

  4. print(iris_minmax[:5]) 

  1. [[ 0.22222222 0.625 0.06779661 0.04166667]


  2. [ 0.16666667 0.41666667 0.06779661 0.04166667] 

  3. [ 0.11111111 0.5 0.05084746 0.04166667] 

  4. [ 0.08333333 0.45833333 0.08474576 0.04166667] 

  5. [ 0.19444444 0.66666667 0.06779661 0.04166667]] 

1.2对定量特征进行二值化

  1. from sklearn.preprocessing import Binarizer



  2. #二值化,分界线设置为3,返回二值化后的特征 

  3. iris_binarizer=Binarizer(threshold=3).fit_transform(iris.data) 

  4. print(iris_binarizer[:5]) 

  1. [[ 1. 1. 0. 0.]


  2. [ 1. 0. 0. 0.] 

  3. [ 1. 1. 0. 0.] 

  4. [ 1. 1. 0. 0.] 

  5. [ 1. 1. 0. 0.]] 

1.3对定性特征进行哑编码

  1. from sklearn.preprocessing import OneHotEncoder



  2. # 哑编码,对iris的目标集进行哑编码,返回编码后的值 

  3. iris_onehotencoder=OneHotEncoder().fit_transform(iris.target.reshape((-1,1))) 

  4. print(iris.target[-5:]) 

  5. print(iris.target.reshape((-1,1))[-5:]) 

  6. print(iris_onehotencoder[-5:]) 

  1. [2 2 2 2 2]


  2. [[2] 

  3. [2] 

  4. [2] 

  5. [2] 

  6. [2]] 

  7. (0, 2) 1.0 

  8. (1, 2) 1.0 

  9. (2, 2) 1.0 

  10. (3, 2) 1.0 

  11. (4, 2) 1.0 

1.4缺失值计算

  1. from numpy import vstack, array, nan


  2. from sklearn.preprocessing import Imputer 


  3. #缺失值计算,返回值为计算缺失值后的数据 

  4. #参数missing_value为缺失值的表示形式,默认为NaN 

  5. #参数strategy为缺失值填充方式,默认为mean(均值) 

  6. iris_imputer=Imputer().fit_transform(vstack((array([nan, nan, nan, nan]), iris.data))) 

  7. print(iris_imputer[:5],len(iris_imputer)) 

  1. [[ 5.84333333 3.054 3.75866667 1.19866667]


  2. [ 5.1 3.5 1.4 0.2 ] 

  3. [ 4.9 3. 1.4 0.2 ] 

  4. [ 4.7 3.2 1.3 0.2 ] 

  5. [ 4.6 3.1 1.5 0.2 ]] 151 

1.5数据变换

  1. from sklearn.preprocessing import PolynomialFeatures



  2. #多项式转换 

  3. #参数degree为度,默认值为2 

  4. iris_pol=PolynomialFeatures().fit_transform(iris.data) 

  5. print(iris_pol[:5]) 

  1. [[ 1. 5.1 3.5 1.4 0.2 26.01 17.85 7.14 1.02 12.25


  2. 4.9 0.7 1.96 0.28 0.04] 

  3. [ 1. 4.9 3. 1.4 0.2 24.01 14.7 6.86 0.98 9. 4.2 

  4. 0.6 1.96 0.28 0.04] 

  5. [ 1. 4.7 3.2 1.3 0.2 22.09 15.04 6.11 0.94 10.24 

  6. 4.16 0.64 1.69 0.26 0.04] 

  7. [ 1. 4.6 3.1 1.5 0.2 21.16 14.26 6.9 0.92 9.61 

  8. 4.65 0.62 2.25 0.3 0.04] 

  9. [ 1. 5. 3.6 1.4 0.2 25. 18. 7. 1. 12.96 

  10. 5.04 0.72 1.96 0.28 0.04]] 

  1. from numpy import log1p


  2. from sklearn.preprocessing import FunctionTransformer 


  3. #自定义转换函数为对数函数的数据变换 

  4. #第一个参数是单变元函数 

  5. iris_ftf=FunctionTransformer(log1p).fit_transform(iris.data) 

  6. print(iris_ftf[:5]) 

  1. [[ 1.80828877 1.5040774 0.87546874 0.18232156]


  2. [ 1.77495235 1.38629436 0.87546874 0.18232156] 

  3. [ 1.74046617 1.43508453 0.83290912 0.18232156] 

  4. [ 1.7227666 1.41098697 0.91629073 0.18232156] 

  5. [ 1.79175947 1.5260563 0.87546874 0.18232156]] 

2.特征选择

2.1Filter

2.1.1方差选择法

  1. from sklearn.feature_selection import VarianceThreshold



  2. #方差选择法,返回值为特征选择后的数据 

  3. #参数threshold为方差的阈值 

  4. iris_vt=VarianceThreshold(threshold=3).fit_transform(iris.data) 

  5. print(iris_vt,len(iris_vt)) 

  1. [[ 1.4]


  2. [ 1.4] 

  3. [ 1.3] 

  4. [ 1.5] 

  5. [ 1.4] 

  6. [ 1.7] 

  7. [ 1.4] 

  8. [ 1.5] 

  9. [ 1.4] 

  10. [ 1.5] 

  11. [ 1.5] 

  12. [ 1.6] 

  13. [ 1.4] 

  14. [ 1.1] 

  15. [ 1.2] 

  16. [ 1.5] 

  17. [ 1.3] 

  18. [ 1.4] 

  19. [ 1.7] 

  20. [ 1.5] 

  21. [ 1.7] 

  22. [ 1.5] 

  23. [ 1. ] 

  24. [ 1.7] 

  25. [ 1.9] 

  26. [ 1.6] 

  27. [ 1.6] 

  28. [ 1.5] 

  29. [ 1.4] 

  30. [ 1.6] 

  31. [ 1.6] 

  32. [ 1.5] 

  33. [ 1.5] 

  34. [ 1.4] 

  35. [ 1.5] 

  36. [ 1.2] 

  37. [ 1.3] 

  38. [ 1.5] 

  39. [ 1.3] 

  40. [ 1.5] 

  41. [ 1.3] 

  42. [ 1.3] 

  43. [ 1.3] 

  44. [ 1.6] 

  45. [ 1.9] 

  46. [ 1.4] 

  47. [ 1.6] 

  48. [ 1.4] 

  49. [ 1.5] 

  50. [ 1.4] 

  51. [ 4.7] 

  52. [ 4.5] 

  53. [ 4.9] 

  54. [ 4. ] 

  55. [ 4.6] 

  56. [ 4.5] 

  57. [ 4.7] 

  58. [ 3.3] 

  59. [ 4.6] 

  60. [ 3.9] 

  61. [ 3.5] 

  62. [ 4.2] 

  63. [ 4. ] 

  64. [ 4.7] 

  65. [ 3.6] 

  66. [ 4.4] 

  67. [ 4.5] 

  68. [ 4.1] 

  69. [ 4.5] 

  70. [ 3.9] 

  71. [ 4.8] 

  72. [ 4. ] 

  73. [ 4.9] 

  74. [ 4.7] 

  75. [ 4.3] 

  76. [ 4.4] 

  77. [ 4.8] 

  78. [ 5. ] 

  79. [ 4.5] 

  80. [ 3.5] 

  81. [ 3.8] 

  82. [ 3.7] 

  83. [ 3.9] 

  84. [ 5.1] 

  85. [ 4.5] 

  86. [ 4.5] 

  87. [ 4.7] 

  88. [ 4.4] 

  89. [ 4.1] 

  90. [ 4. ] 

  91. [ 4.4] 

  92. [ 4.6] 

  93. [ 4. ] 

  94. [ 3.3] 

  95. [ 4.2] 

  96. [ 4.2] 

  97. [ 4.2] 

  98. [ 4.3] 

  99. [ 3. ] 

  100. [ 4.1] 

  101. [ 6. ] 

  102. [ 5.1] 

  103. [ 5.9] 

  104. [ 5.6] 

  105. [ 5.8] 

  106. [ 6.6] 

  107. [ 4.5] 

  108. [ 6.3] 

  109. [ 5.8] 

  110. [ 6.1] 

  111. [ 5.1] 

  112. [ 5.3] 

  113. [ 5.5] 

  114. [ 5. ] 

  115. [ 5.1] 

  116. [ 5.3] 

  117. [ 5.5] 

  118. [ 6.7] 

  119. [ 6.9] 

  120. [ 5. ] 

  121. [ 5.7] 

  122. [ 4.9] 

  123. [ 6.7] 

  124. [ 4.9] 

  125. [ 5.7] 

  126. [ 6. ] 

  127. [ 4.8] 

  128. [ 4.9] 

  129. [ 5.6] 

  130. [ 5.8] 

  131. [ 6.1] 

  132. [ 6.4] 

  133. [ 5.6] 

  134. [ 5.1] 

  135. [ 5.6] 

  136. [ 6.1] 

  137. [ 5.6] 

  138. [ 5.5] 

  139. [ 4.8] 

  140. [ 5.4] 

  141. [ 5.6] 

  142. [ 5.1] 

  143. [ 5.1] 

  144. [ 5.9] 

  145. [ 5.7] 

  146. [ 5.2] 

  147. [ 5. ] 

  148. [ 5.2] 

  149. [ 5.4] 

  150. [ 5.1]] 150 

2.1.2相关系数法(此处使用第二篇博客进行修改)

  1. from sklearn.feature_selection import SelectKBest,chi2


  2. from scipy.stats import pearsonr 


  3. #选择K个最好的特征,返回选择特征后的数据 

  4. #第一个参数为计算评估特征是否好的函数,该函数输入特征矩阵和目标向量,输出二元组(评分,P值)的数组,数组第i项为第i个特征的评分和P值。在此定义为计算相关系数 

  5. #参数k为选择的特征个数 

  6. iris_pear=SelectKBest(chi2, k=2).fit_transform(iris.data, iris.target) 

  7. print(iris_pear,len(iris_pear)) 

  1. [[ 1.4 0.2]


  2. [ 1.4 0.2] 

  3. [ 1.3 0.2] 

  4. [ 1.5 0.2] 

  5. [ 1.4 0.2] 

  6. [ 1.7 0.4] 

  7. [ 1.4 0.3] 

  8. [ 1.5 0.2] 

  9. [ 1.4 0.2] 

  10. [ 1.5 0.1] 

  11. [ 1.5 0.2] 

  12. [ 1.6 0.2] 

  13. [ 1.4 0.1] 

  14. [ 1.1 0.1] 

  15. [ 1.2 0.2] 

  16. [ 1.5 0.4] 

  17. [ 1.3 0.4] 

  18. [ 1.4 0.3] 

  19. [ 1.7 0.3] 

  20. [ 1.5 0.3] 

  21. [ 1.7 0.2] 

  22. [ 1.5 0.4] 

  23. [ 1. 0.2] 

  24. [ 1.7 0.5] 

  25. [ 1.9 0.2] 

  26. [ 1.6 0.2] 

  27. [ 1.6 0.4] 

  28. [ 1.5 0.2] 

  29. [ 1.4 0.2] 

  30. [ 1.6 0.2] 

  31. [ 1.6 0.2] 

  32. [ 1.5 0.4] 

  33. [ 1.5 0.1] 

  34. [ 1.4 0.2] 

  35. [ 1.5 0.1] 

  36. [ 1.2 0.2] 

  37. [ 1.3 0.2] 

  38. [ 1.5 0.1] 

  39. [ 1.3 0.2] 

  40. [ 1.5 0.2] 

  41. [ 1.3 0.3] 

  42. [ 1.3 0.3] 

  43. [ 1.3 0.2] 

  44. [ 1.6 0.6] 

  45. [ 1.9 0.4] 

  46. [ 1.4 0.3] 

  47. [ 1.6 0.2] 

  48. [ 1.4 0.2] 

  49. [ 1.5 0.2] 

  50. [ 1.4 0.2] 

  51. [ 4.7 1.4] 

  52. [ 4.5 1.5] 

  53. [ 4.9 1.5] 

  54. [ 4. 1.3] 

  55. [ 4.6 1.5] 

  56. [ 4.5 1.3] 

  57. [ 4.7 1.6] 

  58. [ 3.3 1. ] 

  59. [ 4.6 1.3] 

  60. [ 3.9 1.4] 

  61. [ 3.5 1. ] 

  62. [ 4.2 1.5] 

  63. [ 4. 1. ] 

  64. [ 4.7 1.4] 

  65. [ 3.6 1.3] 

  66. [ 4.4 1.4] 

  67. [ 4.5 1.5] 

  68. [ 4.1 1. ] 

  69. [ 4.5 1.5] 

  70. [ 3.9 1.1] 

  71. [ 4.8 1.8] 

  72. [ 4. 1.3] 

  73. [ 4.9 1.5] 

  74. [ 4.7 1.2] 

  75. [ 4.3 1.3] 

  76. [ 4.4 1.4] 

  77. [ 4.8 1.4] 

  78. [ 5. 1.7] 

  79. [ 4.5 1.5] 

  80. [ 3.5 1. ] 

  81. [ 3.8 1.1] 

  82. [ 3.7 1. ] 

  83. [ 3.9 1.2] 

  84. [ 5.1 1.6] 

  85. [ 4.5 1.5] 

  86. [ 4.5 1.6] 

  87. [ 4.7 1.5] 

  88. [ 4.4 1.3] 

  89. [ 4.1 1.3] 

  90. [ 4. 1.3] 

  91. [ 4.4 1.2] 

  92. [ 4.6 1.4] 

  93. [ 4. 1.2] 

  94. [ 3.3 1. ] 

  95. [ 4.2 1.3] 

  96. [ 4.2 1.2] 

  97. [ 4.2 1.3] 

  98. [ 4.3 1.3] 

  99. [ 3. 1.1] 

  100. [ 4.1 1.3] 

  101. [ 6. 2.5] 

  102. [ 5.1 1.9] 

  103. [ 5.9 2.1] 

  104. [ 5.6 1.8] 

  105. [ 5.8 2.2] 

  106. [ 6.6 2.1] 

  107. [ 4.5 1.7] 

  108. [ 6.3 1.8] 

  109. [ 5.8 1.8] 

  110. [ 6.1 2.5] 

  111. [ 5.1 2. ] 

  112. [ 5.3 1.9] 

  113. [ 5.5 2.1] 

  114. [ 5. 2. ] 

  115. [ 5.1 2.4] 

  116. [ 5.3 2.3] 

  117. [ 5.5 1.8] 

  118. [ 6.7 2.2] 

  119. [ 6.9 2.3] 

  120. [ 5. 1.5] 

  121. [ 5.7 2.3] 

  122. [ 4.9 2. ] 

  123. [ 6.7 2. ] 

  124. [ 4.9 1.8] 

  125. [ 5.7 2.1] 

  126. [ 6. 1.8] 

  127. [ 4.8 1.8] 

  128. [ 4.9 1.8] 

  129. [ 5.6 2.1] 

  130. [ 5.8 1.6] 

  131. [ 6.1 1.9] 

  132. [ 6.4 2. ] 

  133. [ 5.6 2.2] 

  134. [ 5.1 1.5] 

  135. [ 5.6 1.4] 

  136. [ 6.1 2.3] 

  137. [ 5.6 2.4] 

  138. [ 5.5 1.8] 

  139. [ 4.8 1.8] 

  140. [ 5.4 2.1] 

  141. [ 5.6 2.4] 

  142. [ 5.1 2.3] 

  143. [ 5.1 1.9] 

  144. [ 5.9 2.3] 

  145. [ 5.7 2.5] 

  146. [ 5.2 2.3] 

  147. [ 5. 1.9] 

  148. [ 5.2 2. ] 

  149. [ 5.4 2.3] 

  150. [ 5.1 1.8]] 150 

2.1.3卡方检验

  1. from sklearn.feature_selection import SelectKBest


  2. from sklearn.feature_selection import chi2 


  3. #选择K个最好的特征,返回选择特征后的数据 

  4. iris_chi2=SelectKBest(chi2, k=2).fit_transform(iris.data, iris.target) 

  5. print(iris_chi2[:5],len(iris_chi2)) 

  1. [[ 1.4 0.2]


  2. [ 1.4 0.2] 

  3. [ 1.3 0.2] 

  4. [ 1.5 0.2] 

  5. [ 1.4 0.2]] 150 

2.1.4互信息法

  1. from sklearn.feature_selection import SelectKBest


  2. from minepy import MINE 


  3. #由于MINE的设计不是函数式的,定义mic方法将其为函数式的,返回一个二元组,二元组的第2项设置成固定的P值0.5 

  4. def mic(x, y): 

  5. m = MINE() 

  6. m.compute_score(x, y) 

  7. return (m.mic(), 0.5) 


  8. #选择K个最好的特征,返回特征选择后的数据 

  9. SelectKBest(lambda X, Y: array(map(lambda x:mic(x, Y), X.T)).T, k=2).fit_transform(iris.data, iris.target) 

  1. ---------------------------------------------------------------------------



  2. ImportError Traceback (most recent call last) 


  3. <ipython-input-47-807ad1fcacee> in <module>() 

  4. 1 from sklearn.feature_selection import SelectKBest 

  5. ----> 2 from minepy import MINE 

  6. 3  

  7. 4 #由于MINE的设计不是函数式的,定义mic方法将其为函数式的,返回一个二元组,二元组的第2项设置成固定的P值0.5 

  8. 5 def mic(x, y): 



  9. ImportError: No module named ‘minepy‘ 

2.2Wrapper

3.2.1 递归特征消除法

  1. from sklearn.feature_selection import RFE


  2. from sklearn.linear_model import LogisticRegression 


  3. #递归特征消除法,返回特征选择后的数据 

  4. #参数estimator为基模型 

  5. #参数n_features_to_select为选择的特征个数 

  6. iris_pfe=RFE(estimator=LogisticRegression(), n_features_to_select=2).fit_transform(iris.data, iris.target) 

  7. print(iris_pfe[:5]) 

  1. [[ 3.5 0.2]


  2. [ 3. 0.2] 

  3. [ 3.2 0.2] 

  4. [ 3.1 0.2] 

  5. [ 3.6 0.2]] 

3.3 Embedded

3.3.1 基于惩罚项的特征选择法

  1. from sklearn.feature_selection import SelectFromModel


  2. from sklearn.linear_model import LogisticRegression 


  3. #带L1惩罚项的逻辑回归作为基模型的特征选择 

  4. iris_sfm=SelectFromModel(LogisticRegression(penalty="l1", C=0.1)).fit_transform(iris.data, iris.target) 

  5. print(iris_sfm[:5]) 

  1. [[ 5.1 3.5 1.4]


  2. [ 4.9 3. 1.4] 

  3. [ 4.7 3.2 1.3] 

  4. [ 4.6 3.1 1.5] 

  5. [ 5. 3.6 1.4]] 

  1. from sklearn.linear_model import LogisticRegression



  2. class LR(LogisticRegression): 

  3. def __init__(self, threshold=0.01, dual=False, tol=1e-4, C=1.0, 

  4. fit_intercept=True, intercept_scaling=1, class_weight=None, 

  5. random_state=None, solver=‘liblinear‘, max_iter=100, 

  6. multi_class=‘ovr‘, verbose=0, warm_start=False, n_jobs=1): 


  7. #权值相近的阈值 

  8. self.threshold = threshold 

  9. LogisticRegression.__init__(self, penalty=‘l1‘, dual=dual, tol=tol, C=C, 

  10. fit_intercept=fit_intercept, intercept_scaling=intercept_scaling, class_weight=class_weight, 

  11. random_state=random_state, solver=solver, max_iter=max_iter, 

  12. multi_class=multi_class, verbose=verbose, warm_start=warm_start, n_jobs=n_jobs) 

  13. #使用同样的参数创建L2逻辑回归 

  14. self.l2 = LogisticRegression(penalty=‘l2‘, dual=dual, tol=tol, C=C, fit_intercept=fit_intercept, intercept_scaling=intercept_scaling, class_weight = class_weight, random_state=random_state, solver=solver, max_iter=max_iter, multi_class=multi_class, verbose=verbose, warm_start=warm_start, n_jobs=n_jobs) 


  15. def fit(self, X, y, sample_weight=None): 

  16. #训练L1逻辑回归 

  17. super(LR, self).fit(X, y, sample_weight=sample_weight) 

  18. self.coef_old_ = self.coef_.copy() 

  19. #训练L2逻辑回归 

  20. self.l2.fit(X, y, sample_weight=sample_weight) 


  21. cntOfRow, cntOfCol = self.coef_.shape 

  22. #权值系数矩阵的行数对应目标值的种类数目 

  23. for i in range(cntOfRow): 

  24. for j in range(cntOfCol): 

  25. coef = self.coef_[i][j] 

  26. #L1逻辑回归的权值系数不为0 

  27. if coef != 0: 

  28. idx = [j] 

  29. #对应在L2逻辑回归中的权值系数 

  30. coef1 = self.l2.coef_[i][j] 

  31. for k in range(cntOfCol): 

  32. coef2 = self.l2.coef_[i][k] 

  33. #在L2逻辑回归中,权值系数之差小于设定的阈值,且在L1中对应的权值为0 

  34. if abs(coef1-coef2) < self.threshold and j != k and self.coef_[i][k] == 0: 

  35. idx.append(k) 

  36. #计算这一类特征的权值系数均值 

  37. mean = coef / len(idx) 

  38. self.coef_[i][idx] = mean 

  39. return self 

  1. from sklearn.feature_selection import SelectFromModel



  2. #带L1和L2惩罚项的逻辑回归作为基模型的特征选择 

  3. #参数threshold为权值系数之差的阈值 

  4. iris_sfm2=SelectFromModel(LR(threshold=0.5, C=0.1)).fit_transform(iris.data, iris.target) 

  5. print(iris_sfm2[:5]) 

  1. [[ 5.1 3.5 1.4 0.2]


  2. [ 4.9 3. 1.4 0.2] 

  3. [ 4.7 3.2 1.3 0.2] 

  4. [ 4.6 3.1 1.5 0.2] 

  5. [ 5. 3.6 1.4 0.2]] 

3.3.2 基于树模型的特征选择法

  1. from sklearn.feature_selection import SelectFromModel


  2. from sklearn.ensemble import GradientBoostingClassifier 


  3. #GBDT作为基模型的特征选择 

  4. iris_sfm3=SelectFromModel(GradientBoostingClassifier()).fit_transform(iris.data, iris.target) 

  5. print(iris_sfm3[:5]) 

  1. [[ 1.4 0.2]


  2. [ 1.4 0.2] 

  3. [ 1.3 0.2] 

  4. [ 1.5 0.2] 

  5. [ 1.4 0.2]] 

4 降维

4.1 主成分分析法(PCA)

  1. from sklearn.decomposition import PCA



  2. #主成分分析法,返回降维后的数据 

  3. #参数n_components为主成分数目 

  4. iris_pca=PCA(n_components=2).fit_transform(iris.data) 

  5. print(iris_pca[:5]) 

  1. [[-2.68420713 0.32660731]


  2. [-2.71539062 -0.16955685] 

  3. [-2.88981954 -0.13734561] 

  4. [-2.7464372 -0.31112432] 

  5. [-2.72859298 0.33392456]] 

4.2 线性判别分析法(LDA)

  1. from sklearn.lda import LDA



  2. #线性判别分析法,返回降维后的数据 

  3. #参数n_components为降维后的维数 

  4. LDA(n_components=2).fit_transform(iris.data, iris.target) 

  1. ---------------------------------------------------------------------------



  2. ImportError Traceback (most recent call last) 


  3. <ipython-input-56-21fd5d727aec> in <module>() 

  4. ----> 1 from sklearn.lda import LDA 

  5. 2  

  6. 3 #线性判别分析法,返回降维后的数据 

  7. 4 #参数n_components为降维后的维数 

  8. 5 LDA(n_components=2).fit_transform(iris.data, iris.target) 



  9. ImportError: No module named ‘sklearn.lda‘ 

参考文章

1.使用sklearn做单机特征工程

2.利用scikit-learn进行FeatureSelection

原文地址:https://www.cnblogs.com/wushaogui/p/8782362.html

时间: 2024-08-01 01:11:55

特征工程学习01-sklearn单机特征工程的相关文章

使用sklearn做单机特征工程

目录 1 特征工程是什么?2 数据预处理 2.1 无量纲化 2.1.1 标准化 2.1.2 区间缩放法 2.1.3 标准化与归一化的区别 2.2 对定量特征二值化 2.3 对定性特征哑编码 2.4 缺失值计算 2.5 数据变换 2.6 回顾3 特征选择 3.1 Filter 3.1.1 方差选择法 3.1.2 相关系数法 3.1.3 卡方检验 3.1.4 互信息法 3.2 Wrapper 3.2.1 递归特征消除法 3.3 Embedded 3.3.1 基于惩罚项的特征选择法 3.3.2 基于树

使用Python做单机特征工程

目录 1 特征工程是什么?2 数据预处理 2.1 无量纲化 2.1.1 标准化 2.1.2 区间缩放法 2.1.3 无量纲化与正则化的区别 2.2 对定量特征二值化 2.3 对定性特征哑编码 2.4 缺失值计算 2.5 数据变换 2.6 回顾3 特征选择 3.1 Filter 3.1.1 方差选择法 3.1.2 相关系数法 3.1.3 卡方检验 3.1.4 互信息法 3.2 Wrapper 3.2.1 递归特征消除法 3.3 Embedded 3.3.1 基于惩罚项的特征选择法 3.3.2 基于

压缩跟踪(CT)代码详细学习_模块2(特征的提取和计算)

0.下载安装Opencv,当前版本为249. 1.下载Python,当前OPencv版本为249,不过其支持的最新版本的Python为2.7,所以可以下载276版本. 2.下载numpy,开始我使用了1.6,没有通过,错误如图.下载了最新的1.8.1版本. 3.将Opencv安装目录下opencv\build\python\2.7\x86中的cv2.pyd复制到python安装目录Lib\site-packages下. 4.找到opencv源文件内的draw.py运行. 压缩跟踪(CT)代码详细

OpenCV学习笔记[5]FLANN特征匹配

OpenCV学习笔记:FLANN特征匹配 本次给出FLANN特征匹配的Java实现. [简介] 特征匹配记录下目标图像与待匹配图像的特征点(KeyPoint),并根据特征点集合构造特征量(descriptor),对这个特征量进行比较.筛选,最终得到一个匹配点的映射集合.我们也可以根据这个集合的大小来衡量两幅图片的匹配程度. 特征匹配与模板匹配不同,由于是计算特征点集合的相关度,转置操作对匹配影响不大,但它容易受到失真.缩放的影响. [特征匹配] FeatureMatching.java: imp

网络工程学习利器2---IEEE 802 标准集合

摘自:https://openhpi.cn 网络工程学习利器2---IEEE 802 标准集合

学习OpenCV——Surf(特征点篇)&amp;flann

Surf(Speed Up Robust Feature) Surf算法的原理                                                                            1.构建Hessian矩阵构造高斯金字塔尺度空间 其实surf构造的金字塔图像与sift有很大不同,就是因为这些不同才加快了其检测的速度.Sift采用的是DOG图像,而surf采用的是Hessian矩阵行列式近似值图像.Hessian矩阵是Surf算法的核心,为了方便运

OSX学习01之更新头像

前不久在官网上守株待兔,买了一个官翻版865,其实最想买294的,可是米不足啊——所以,在同时下了865和293的订单,并纠结了一天后,确定了865,剩余的钱够一个Mac mini了,如果不买也可以日后买iMac. 相关的内容等会儿写另一帖,废话就不说了. 拆机后简单的设置下账户信息,但是没觉得账户图片好看,又没发现又什么方式设置账户图片,纠结了很久. 本机没有什么图片,这时候可以先把图片下载下来再说. 进入系统偏好设置-->用户与群组-->当前用户,点击现有头像-->最近使用-->

概率论快速学习01:计数

2014-05-15 22:02 by Jeff Li 前言 系列文章:[传送门] 马上快要期末考试了,为了学点什么.就准备这系列的博客,记录复习的成果. 正文-计数  概率 概率论研究随机事件.它源于赌徒的研究.即使是今天,概率论也常用于赌博.随机事件的结果是否只凭运气呢?高明的赌徒发现了赌博中的规律.尽管我无法预知事件的具体结果,但我可以了解每种结果出现的可能性.这是概率论的核心. "概率"到底是什么?这在数学上还有争议."频率派"认为概率是重复尝试多次,某种结

ThinkPhp学习01

原文:ThinkPhp学习01 一.ThinkPHP的介绍           MVC  M - Model 模型                工作:负责数据的操作  V - View  视图(模板)        工作:负责前台页面显示  C - Controller 控制器(模块) 工作:描述功能 框架二.ThinkPHP的获取            http://www.thinkphp.cn三.ThinkPHP核心文件介绍      ├─ThinkPHP.php     框架入口文件