1. 程式人生 > 其它 >機器學習sklearn(十七): 特徵工程(八)特徵選擇(三)卡方選擇(二)卡方檢驗

機器學習sklearn(十七): 特徵工程(八)特徵選擇(三)卡方選擇(二)卡方檢驗

Python有包可以直接實現特徵選擇,也就是看自變數對因變數的相關性。今天我們先開看一下如何用卡方檢驗實現特徵選擇。

1. 首先import包和實驗資料:

from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import chi2
from sklearn.datasets import load_iris
 
#匯入IRIS資料集
iris = load_iris()
iris.data#檢視資料

結果輸出:

array([[ 5.1,  3.5,  1.4,  0.2],
       [ 
4.9, 3. , 1.4, 0.2], [ 4.7, 3.2, 1.3, 0.2], [ 4.6, 3.1, 1.5, 0.2], [ 5. , 3.6, 1.4, 0.2], [ 5.4, 3.9, 1.7, 0.4], [ 4.6, 3.4, 1.4, 0.3],

2. 使用卡方檢驗來選擇特徵

model1 = SelectKBest(chi2, k=2)#選擇k個最佳特徵
model1.fit_transform(iris.data, iris.target)#iris.data是特徵資料,iris.target是標籤資料,該函式可以選擇出k個特徵

結果輸出為:
array([[ 1.4, 0.2],
[ 1.4, 0.2],
[ 1.3, 0.2],
[ 1.5, 0.2],
[ 1.4, 0.2],
[ 1.7, 0.4],
[ 1.4, 0.3],

可以看出後使用卡方檢驗,選擇出了後兩個特徵。如果我們還想檢視卡方檢驗的p值和得分,可以使用第3步。

3. 檢視p-values和scores

model1.scores_ #得分

得分輸出為:

array([ 10.81782088, 3.59449902, 116.16984746, 67.24482759])


可以看出後兩個特徵得分最高,與我們第二步的結果一致;

model1.pvalues_ #p-values

p值輸出為:
array([ 4.47651499e-03, 1.65754167e-01, 5.94344354e-26, 2.50017968e-15])
可以看出後兩個特徵的p值最小,置信度也最高,與前面的結果一致。

API

classsklearn.feature_selection.SelectKBest(score_func=<function f_classif>,*,k=10)

Select features according to the k highest scores.

Read more in theUser Guide.

Parameters
score_funccallable, default=f_classif

Function taking two arrays X and y, and returning a pair of arrays (scores, pvalues) or a single array with scores. Default is f_classif (see below “See Also”). The default function only works with classification tasks.

New in version 0.18.

kint or “all”, default=10

Number of top features to select. The “all” option bypasses selection, for use in a parameter search.

Attributes
scores_array-like of shape (n_features,)

Scores of features.

pvalues_array-like of shape (n_features,)

p-values of feature scores, None ifscore_funcreturned only scores.

>>> from sklearn.datasets import load_digits
>>> from sklearn.feature_selection import SelectKBest, chi2
>>> X, y = load_digits(return_X_y=True)
>>> X.shape
(1797, 64)
>>> X_new = SelectKBest(chi2, k=20).fit_transform(X, y)
>>> X_new.shape
(1797, 20)