Python 在SciKit'中使用包含ColumnTransformer的管道;s RFECV

Python 在SciKit'中使用包含ColumnTransformer的管道;s RFECV,python,scikit-learn,rfe,Python,Scikit Learn,Rfe,我正在尝试使用SciKit对转换后的数据执行RFECV 为此,我创建了一个管道,并将管道传递给RFECV。除非我将ColumnTransformer作为管道步骤,否则它可以正常工作。它给了我以下错误: ValueError: Specifying the columns using strings is only supported for pandas DataFrames 我已经检查了答案,但我不确定它们是否适用于这里。代码如下: import pandas as pd from skle

我正在尝试使用SciKit对转换后的数据执行
RFECV

为此,我创建了一个管道,并将管道传递给
RFECV
。除非我将
ColumnTransformer
作为管道步骤,否则它可以正常工作。它给了我以下错误:

ValueError: Specifying the columns using strings is only supported for pandas DataFrames
我已经检查了答案,但我不确定它们是否适用于这里。代码如下:

import pandas as pd
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import Normalizer
from sklearn.pipeline import Pipeline
from sklearn.feature_selection import RFECV
from sklearn.linear_model import LinearRegression

class CustomPipeline(Pipeline):
    @property
    def coef_(self):
        return self._final_estimator.coef_
    @property
    def feature_importances_(self):
        return self._final_estimator.feature_importances_

X = pd.DataFrame({
    'col1': [i for i in range(100)] , 
    'col2': [i*2 for i in range(100)],
})
y = pd.DataFrame({'out': [i*3 for i in range(100)]})
ct = ColumnTransformer([("norm", Normalizer(norm='l1'), ['col1'])])

pipe = CustomPipeline([
    ('col_transform', ct),
    ('lr', LinearRegression())
])

rfecv = RFECV(
    estimator=pipe, 
    step=1,
    cv=3,
)
#pipe.fit(X,y) # pipe can fit, no problems
rfecv.fit(X,y)
显然,我可以在管道之外执行此转换步骤,然后使用转换后的X,但我想知道是否有任何解决方法


我还想将此作为RFECV的设计问题提出(它首先将X转换为numpy数组,而其他内置交叉验证的方法,例如,
GridSearchCV
不这样做)

我很好奇您最终会做什么。。。我吃的是完全一样的issue@Guillermo.D,我在管道之前进行了转换,并将转换后的X传递给管道,这并不理想,但想不出更整洁的方法