特徵選擇方法的三大類型 [1]:
1.filter method :利用一些統計指標進行特徵選擇,和模型沒有關係
2.wrapper method:結合模型來做,每次加入或者減少特徵看對模型的準確度是否有提升,如果有提升,那麼就增加或者減少,所以
需要不斷構建模型來判斷是否要加入特徵
3.embedded method:結合模型來做,和模型訓練一起做,即模型訓練完,特徵就出來了;
所以,wrapper method 要不斷的構建模型,花費的資源是比較多的!
filter的部分方法 [2] :
- information gain 信息增益
- chi-square test 卡方統計量
- fisher score [4]
- correlation coefficient [1] 相關係數,一般用pearson/spearman/kendall 相關係數,一定要注意下圖中的assumptions,即適用的條件
可視化:
from sklearn.datasets import load_iris
import pandas as pd
import seaborn as sb
import matplotlib.pyplot as plt
iris=load_iris()
df=pd.DataFrame(iris.data,columns=["x1","x2","x3","x4"])
# print(df.info()) # 均非空
plt.figure(figsize=(8,6))
df_corr=df.corr(method="pearson") # 這裏可以指定響應的方法
sb.heatmap(df_corr,annot=True)
plt.show()
假設把x1作爲y,那麼x2與x1的相關係數爲-0.12 , x3 與x1的相關係數爲0.87,x4與x1的相關係數爲0.82 ,認爲x2與x1相關性很弱,可以刪除,常見的做法,是設定一個閾值,來判斷
# 定位x1的相關係數
x1_corr=abs(df_corr["x1"])
print(x1_corr[x1_corr>0.5])
- variance threshold 方差選擇 [3] ,刪除方差小於某個閾值的特徵,默認爲0
from sklearn.feature_selection import VarianceThreshold
X = [[0, 2, 0, 3], [0, 1, 4, 3], [0, 1, 1, 3]]
selector=VarianceThreshold() # 默認是0
X=selector.fit_transform(X)
print(selector.variances_)
print()
print(X)
# 輸出
[0. 0.22222222 2.88888889 0. ]
[[2 0]
[1 4]
[1 1]]
wapper的部分方法[2]:
- recursive feature elimination
- sequential feature selection algorithms
- genetic algorithms
embedded的部分方法[2]:
- L1 (LASSO) regularization
- decision tree
refer:
[1] https://towardsdatascience.com/feature-selection-with-pandas-e3690ad8504b
[3] https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.VarianceThreshold.html