Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的特征选择_Python_Scikit Learn_Feature Selection - Fatal编程技术网

python中的特征选择

python中的特征选择,python,scikit-learn,feature-selection,Python,Scikit Learn,Feature Selection,我正在尝试使用几种技术在python中执行功能选择。我尝试应用的第一种技术是仅使用特征的变化来选择特征。我的代码如下: def feature_selection_technique(train, test, lbls, technique): if technique == "variance": sel = VarianceThreshold(threshold=(0.00010 * (1 - .15))) model1 = sel1.fit_trans

我正在尝试使用几种技术在python中执行功能选择。我尝试应用的第一种技术是仅使用特征的变化来选择特征。我的代码如下:

def feature_selection_technique(train, test, lbls, technique):
   if technique == "variance":
        sel = VarianceThreshold(threshold=(0.00010 * (1 - .15)))
        model1 = sel1.fit_transform(face_train)
        new_train = model1.transform(train)
        new_test = model1.transform(test)

    return new_train, new_test

实际上,我想使用train数据集计算选定的特征,然后将其应用于测试数据集。在这种情况下,转换方法似乎无法实现。在这种情况下我能做什么?

我认为您使用的语法有问题。请参阅文档和示例。正确的语法如下所示:

def feature_selection_technique(train, test, lbls, technique):
   if technique == "variance":
        sel = VarianceThreshold(threshold=(0.00010 * (1 - .15)))
        new_train=sel.fit_transform(train)
        new_test = sel.transform(test)

    return new_train, new_test

也就是说,您应该初始化
sel
,然后将其适配到训练数据并进行转换,然后转换测试数据。

您是否尝试过在:model1=sel.fit\u transform(train)之后使用类似于:model2=sel.fit\u transform(test)的东西?是的,但这不是我想要的。该技术保持特征的方差大于建议的阈值。我想从训练集中保留这些特性,并将相同的特性保留到测试集中。在第一个sel=VarianceThreshold(阈值=(0.00010*(1-.15)),直接调用new_train=sel.fit_transform(训练)和new_test=sel.transform(测试),这应该可以正常工作
fit_transform
返回一组更改过的数据集(
model1
在您的情况下)。然后您使用的是
model1.transform(train)
?您确定您的代码正确吗?是的,这工作非常完美。谢谢您的解决方案。