python - 使用 LinearSVC 进行特征选择

标签 python machine-learning svm scikit-learn

当我尝试使用我的数据(来自 this example )运行以下代码时

X_new = LinearSVC(C=0.01, penalty="l1", dual=False).fit_transform(X, y)

我得到:

"Invalid threshold: all features are discarded"

我尝试指定我自己的阈值:

clf = LinearSVC(C=0.01, penalty="l1", dual=False)
clf.fit(X,y)
X_new = clf.transform(X, threshold=my_threshold)

但我要么得到:

  • X 大小相同的数组X_new,只要my_threshold 是以下之一:

    • '意思'
    • '中位数'
  • “无效阈值” 错误(例如,将标量值传递给阈值时)

我无法发布整个矩阵 X,但下面是一些数据统计信息:

> X.shape 
Out: (29,312) 

> np.mean(X, axis=1)
Out: 
array([-0.30517191, -0.1147345 ,  0.03674294, -0.15926932, -0.05034101,
       -0.06357734, -0.08781186, -0.12865185,  0.14172452,  0.33640029,
        0.06778798, -0.00217696,  0.09097335, -0.17915627,  0.03701893,
       -0.1361117 ,  0.13132006,  0.14406628, -0.05081956,  0.20777349,
       -0.06028931,  0.03541849, -0.07100492,  0.05740661, -0.38585413,
        0.31837905,  0.14076042,  0.1182338 , -0.06903557])

> np.std(X, axis=1)                                               
Out: 
array([ 1.3267662 ,  0.75313658,  0.81796146,  0.79814621,  0.59175161,
        0.73149726,  0.8087903 ,  0.59901198,  1.13414141,  1.02433752,
        0.99884428,  1.11139231,  0.89254901,  1.92760784,  0.57181158,
        1.01322265,  0.66705546,  0.70248779,  1.17107696,  0.88254386,
        1.06930436,  0.91769016,  0.92915593,  0.84569395,  1.59371779,
        0.71257806,  0.94307434,  0.95083782,  0.88996455])

y = array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2,
           0, 0, 0, 0, 0, 0])

这一切都在 scikit-learn 0.14 中。

最佳答案

在尝试将其用作转换基础之前,您应该首先分析您的 SVM 模型是否训练良好。有可能您使用的太小的C 参数,这导致sklearn 训练一个简单的模型,从而导致删除所有特征。您可以通过对数据执行分类测试或至少打印找到的系数 (clf.coef_)

来检查它

最好运行网格搜索技术,在泛化属性方面获得最佳的C,然后将其用于转换。

关于python - 使用 LinearSVC 进行特征选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18213789/

相关文章:

python - 为了保护 MxNet 中的某些权重不被训练器更改,推荐的操作是什么?

machine-learning - 哪种分类器可以有效地处理不属于经过训练的类的测试查询?

machine-learning - 机器学习-SVM-计算向量W时如何计算偏差?

python - 包装使用 import * 导入的函数?

python - 如何使用 python 使用注册表确定 3d studio max 的位置?

python - 日期和图表对齐 - 经济分析

python - 如何使用 matplotlib 选择多个点?

machine-learning - tensorflow 模型第一层有多少个神经元?

python - scikit-learn, linearsvc - 如何从经过训练的 SVM 中获取支持向量?

python - 在 scikit learn 中结合网格搜索和交叉验证