sklearn特征选择模块

X_new = SelectKBest(chi2, k=2).fit_transform(X, y)

在使用类似SelectKBest的时候,使用fit.transform可以直接得到转换好的数据集

如果你使用的是稀疏的数据 (例如数据可以由稀疏矩阵来表示),

chi2 , mutual_info_regression , mutual_info_classif 可以处理数据并保持它的稀疏性。

 SelectFromModel函数使用

>>> from sklearn.svm import LinearSVC
>>> from sklearn.datasets import load_iris
>>> from sklearn.feature_selection import SelectFromModel
>>> iris = load_iris()
>>> X, y = iris.data, iris.target
>>> X.shape
(150, 4)
>>> lsvc = LinearSVC(C=0.01, penalty="l1", dual=False).fit(X, y)
>>> model = SelectFromModel(lsvc, prefit=True)   prefit参数设置为True表示,使用的模型是已经拟合的
>>> X_new = model.transform(X)
>>> X_new.shape
(150, 3)

以下为prefit 参数: bool, default False

Whether a prefit model is expected to be passed into the constructor directly or not. If True, transform must be called directly and SelectFromModel cannot be used with cross_val_scoreGridSearchCV and similar utilities that clone the estimator. Otherwise train the model using fit and then transform to do feature selection.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值