文章目录
官网: sklearn.feature_selection.SelectKBest
如何使用SelectKBest
根据给定的选择器
选择出前k个与标签最相关的特征。
class sklearn.feature_selection.SelectKBest(score_func=<function f_classif>, *, k=10)
参数说明:
Parameters
----------
score_func: 可调用的
函数输入两个数组X和y,并返回一对数组(分数,p-value)或带分数的
单个数组。默认值为f_classif(请参见下文“另请参见”)。默认功能仅
适用于分类任务。
k:int or “all”, optional, 默认=10
要选择的主要特征数(保留前几个最佳特征)。
“ all”选项绕过选择,用于参数搜索。
Attributes
----------
scores_:array-like of shape (n_features,)
特征分数。
pvalues_:array-like of shape (n_features,)
特征分数的p值,如果score_func仅返回分数,则为None 。
官网示例:
使用 chi2方法 进行筛选特征
从64个特征中保留了20个最佳特征
>>> 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)
方法
'fit(self, X, y)'
在(X,y)上运行得分功能并获得适当的功能。
相当于训练模型吧。
Parameters
X:array-like of shape (n_samples, n_features)
输入训练样本。
y:array-like of shape (n_samples,)
目标值(分类中的类标签,回归中的实数)。
Returns
self:object
'fit_transform(self, X[, y])'
用X训练模型,然后对其进行转换。
Parameters
X:{array-like, sparse matrix, dataframe} of shape (n_samples, n_features)
y:ndarray of shape (n_samples,), default=None
目标值。
**fit_paramsdict:其他拟合参数。
Returns
X_new:ndarray array of shape (n_samples, n_features_new)
转换后的数组。
'get_params(self[, deep])'
获取此估计量的参数。
Parameters
deep: bool, default=True
如果为True,则将返回此估算器和作为估算器的包含子对象的参数。
Returns
params: mapping of string to any
参数名称映射到其值。
'get_support(self[, indices])'
获取所选特征的掩码或整数索引
Get a mask, or integer index, of the features selected
'inverse_transform(self, X)'
反向转换操作
Parameters
X: array of shape [n_samples, n_selected_features]
输入样本。
Returns
X_r: array of shape [n_samples, n_original_features]
在X中被transform删除的特征位置加入零列。
'set_params(self, \*\*params)'
设置此估算器的参数。
Parameters
**params: dict
估算器参数.
Returns
self: object
估算器实例。
'transform(self, X)'
将X缩小为指定的k个特征。
Parameters
X: array of shape [n_samples, n_features]
输入样本。
Returns
X_new: array of shape [n_samples, n_selected_features]
仅保留k个最优特征的输入样本。
使用实例
会使用到:
scipy.stats.pearsonr - 皮尔森相关系数
sklearn.feature_selection.f_regression - 单变量线性回归测试
数据
- 样本数:1000
- 特征数:3(3维数据)
- 重要特征:1
from sklearn.datasets import make_regression
X,y = make_regression(n_samples=1000, n_features=3, n_informative=1, noise=100, random_state=9527)
分别计算每个特征与标签的相关系数
from scipy.stats import pearsonr
p1 = pearsonr(X[:,0],y)
p2 = pearsonr(X[:,1],y)
p3 = pearsonr(X[:,2],y)
可以看出X的第二个特征是重要特征(相关系数最高)
print(p1)
>>>(0.01293680050695129, 0.6828310401786694)
print(p2)
>>>(0.6680920624164118, 2.8345376164035335e-130)
print(p3)
>>>(0.03938982451397195, 0.21330062660673496)
这里使用的选择器为 f_regression
且只保留一个最佳(与结果相关系数最高的)特征
from sklearn import feature_selection as FS
best = FS.SelectKBest(score_func=FS.f_regression, k=1)
best.fit(X,y)
>>>SelectKBest(k=1, score_func=<function f_regression at 0x000002CDBFE02288>)
best.scores_
>>>array([1.67054044e-01, 8.04573104e+02, 1.55086141e+00])
X_new一个1维数据
X_new = best.transform(X)
X_new.shape
>>>(1000, 1)
通过下边可以看出,保留了第2个特征;
与上边计算的皮尔斯相关系数对应,且满足我们数据特性。
X_new[:5]
>>>array([[-0.6872076 ],
[ 2.31728703],
[-1.51368674],
[-0.24881066],
[ 0.39894185]])
X[:5]
>>>array([[-1.25468454, -0.6872076 , -0.60472765],
[-0.15208513, 2.31728703, -1.44588579],
[ 0.3246091 , -1.51368674, 0.01249735],
[-0.43843568, -0.24881066, 0.77710434],
[-0.36040751, 0.39894185, -0.61578169]])