from sklearn.feature_selection import SelectKBest
之前做過一段時間的特征選擇部分的研究赞枕〕呵福總結(jié)了一些知識和使用的方法,今天再看相關(guān)文章的時候迹卢,提到了一個
SelectKBest
看名字挺牛的惠豺,本來因為是一個第三方庫银还,結(jié)果百度了一下,發(fā)現(xiàn)是sklearn里的一部分耕腾,而且這還是我一直用過的feature_selection里面的见剩,于是跟進去看了一下源碼。
我的心都涼了扫俺,這是啥玩意苍苞。。狼纬。羹呵。。疗琉。
class SelectKBest(_BaseFilter):
"""Select features according to the k highest scores.
Read more in the :ref:`User Guide <univariate_feature_selection>`.
Parameters
----------
score_func : callable
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.
k : int or "all", optional, default=10
Number of top features to select.
The "all" option bypasses selection, for use in a parameter search.
Attributes
----------
scores_ : array-like, shape=(n_features,)
Scores of features.
pvalues_ : array-like, shape=(n_features,)
p-values of feature scores, None if `score_func` returned only scores.
Notes
-----
Ties between features with equal scores will be broken in an unspecified
way.
See also
--------
f_classif: ANOVA F-value between label/feature for classification tasks.
mutual_info_classif: Mutual information for a discrete target.
chi2: Chi-squared stats of non-negative features for classification tasks.
f_regression: F-value between label/feature for regression tasks.
mutual_info_regression: Mutual information for a continuous target.
SelectPercentile: Select features based on percentile of the highest scores.
SelectFpr: Select features based on a false positive rate test.
SelectFdr: Select features based on an estimated false discovery rate.
SelectFwe: Select features based on family-wise error rate.
GenericUnivariateSelect: Univariate feature selector with configurable mode.
"""
下面是官網(wǎng)的例子
看懂了吧冈欢,意思就是說,計算公式要自己給盈简,要取前k個的k值也自己給凑耻,等于說這個包什么都沒做。柠贤。香浩。只是做了一部分計算而已。臼勉。邻吭。。宴霸。囱晴。心有點累了
看著名字很牛膏蚓,其實啥都沒有。
參數(shù)
1畸写、score_func : callable驮瞧,函數(shù)取兩個數(shù)組X和y,返回一對數(shù)組(scores, pvalues)或一個分數(shù)的數(shù)組艺糜。默認函數(shù)為f_classif剧董,默認函數(shù)只適用于分類函數(shù)。
2破停、k:int or "all", optional, default=10翅楼。所選擇的topK個特征≌媛“all”選項則繞過選擇毅臊,用于參數(shù)搜索。
屬性
1黑界、scores_ : array-like, shape=(n_features,)管嬉,特征的得分
2、pvalues_ : array-like, shape=(n_features,)朗鸠,特征得分的p_value值蚯撩,如果score_func只返回分數(shù),則返回None烛占。
score_func里可選的公式
方法
1胎挎、fit(X,y),在(X忆家,y)上運行記分函數(shù)并得到適當(dāng)?shù)奶卣鳌?br>
2犹菇、fit_transform(X[, y]),擬合數(shù)據(jù)芽卿,然后轉(zhuǎn)換數(shù)據(jù)揭芍。
3、get_params([deep])卸例,獲得此估計器的參數(shù)称杨。
4、get_support([indices])筷转,獲取所選特征的掩碼或整數(shù)索引列另。
5、inverse_transform(X)旦装,反向變換操作。
6摊滔、set_params(**params)阴绢,設(shè)置估計器的參數(shù)店乐。
7、transform(X)呻袭,將X還原為所選特征眨八。
我試了一下,別的不好用左电,只有這個好用廉侧,暫時就這樣吧。
---------------------------------------更新一--------------------------------------------
---------------------------------更新二-----------------------------------------------------
發(fā)現(xiàn)有人提問篓足,我回答一下段誊,哈哈哈,有點小得意呢栈拖。
首先连舍,如何返回選擇特征的名稱或者索引。其實在上面的方法中已經(jīng)提了一下了涩哟,那就是get_support()
之前的digit數(shù)據(jù)是不帶特征名稱的索赏,我選擇了帶特征的波士頓房價數(shù)據(jù),因為是回歸數(shù)據(jù)贴彼,所以計算的評價指標也跟著變換了潜腻,f_regression,這里需要先fit一下器仗,才能使用get_support()融涣。里面的參數(shù)如果索引選擇True,
返回值就是feature的索引青灼,可能想直接返回feature name在這里不能這么直接的調(diào)用了暴心,但是在dataset里面去對應(yīng)一下應(yīng)該很容易的。這里我給出的K是5杂拨,選擇得分最高的前5個特征专普,分別是第2,5,9,10,12個屬性。
如果里面的參數(shù)選擇了False弹沽,返回值就是該特征是否被選擇的Boolean值檀夹。