哪些特征选择 fit_transform? [英] Which features selects fit_transform?

查看:57
本文介绍了哪些特征选择 fit_transform?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 LinearSVC 选择特征.所有功能都是二进制文件.这是它的样子:

输入>X0.shape出>(6876299, 49)在>lsvc = LinearSVC(C=0.01,惩罚=l1",双=假)在>X_new = lsvc.fit_transform(X0, y0)在>X_new.shape出>(6876299, 41)

我的问题很简单,但我还没有找到任何具体的解决方案.我应该如何知道 fit_transform 选择了哪些特征?

谢谢!

解决方案

你可以看看lsvc.coef_.具有非零系数的特征将是已选择的特征.例如,以下将为您提供所有非零特征的掩码:

<预><代码>>>>从 sklearn.datasets 导入 load_iris>>>虹膜 = load_iris()>>>X, y = iris.data, iris.target>>>X形(150, 4)>>>lsvc = LinearSVC(C=0.01,惩罚=l1",双=假)>>>X_new = lsvc.fit_transform(X, y)>>>X_new.shape(150, 3)>>>lsvc.coef_数组([[ 0. , 0.21680351, -0.28727891, 0. ],[ 0. , -0.09186784, 0. , 0. ],[-0.03501512, -0.17022421, 0.13485806, 0. ]])>>>~np.all(lsvc.coef_==0, 轴=0)数组([真,真,真,假],dtype=bool)

I'm selecting features using LinearSVC. All the features are binaries. This is how it looks like:

In>  X0.shape
Out> (6876299, 49)
In>  lsvc = LinearSVC(C=0.01, penalty="l1", dual=False)
In>  X_new = lsvc.fit_transform(X0, y0)
In>  X_new.shape
Out> (6876299, 41)

My problem is very simple, but I haven't found any specific solution. How am I supposed to know which features have been selected by fit_transform?

Thks!

解决方案

You can take a look at lsvc.coef_. The features with non-zero coefficients will be the ones that have been chosen. For example the following will give you a mask of all non-zero features:

>>> from sklearn.datasets import load_iris
>>> iris = load_iris()
>>> X, y = iris.data, iris.target
>>> X.shape
(150, 4)

>>> lsvc = LinearSVC(C=0.01, penalty="l1", dual=False)
>>> X_new = lsvc.fit_transform(X, y)
>>> X_new.shape
(150, 3)

>>> lsvc.coef_
array([[ 0.        ,  0.21680351, -0.28727891,  0.        ],
       [ 0.        , -0.09186784,  0.        ,  0.        ],
       [-0.03501512, -0.17022421,  0.13485806,  0.        ]])

>>> ~np.all(lsvc.coef_==0, axis=0)
array([ True,  True,  True, False], dtype=bool)

这篇关于哪些特征选择 fit_transform?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆