sklearn:使用 ColumnTransformer 链接多个变压器

如何使用 ColumnTransformer API将多个转换器应用到单个pandas DataFrame 列?


例如,我想取立方根,然后标准化 DataFrame 列中的值:


df = pd.DataFrame(

  np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]),

  columns=['a', 'b', 'c']

)

transformer = ColumnTransformer(

  [

    ('root3_std', StandardScaler() + FunctionTransformer(np.cbrt), 'a') <-- pseudocode

  ],

  remainder='passthrough'

)

如果我写


transformer = ColumnTransformer(

  [

    ('root3', FunctionTransformer(np.cbrt), 'a'),

    ('standardize', StandardScaler(), 'a')

  ],

  remainder='passthrough'

)

我得到两列独立的列,一列包含立方根,另一列包含标准化原始值。如何一次性应用两个变压器?


森栏
浏览 107回答 1
1回答

POPMUISE

from sklearn.pipeline import Pipelineimport pandas as pdimport numpy as npfrom sklearn.preprocessing import FunctionTransformer, StandardScalerdf = pd.DataFrame(np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]),columns=['a', 'b', 'c'])pipe = Pipeline([('function_transformer', FunctionTransformer(np.cbrt)),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;('standard_scalar', StandardScaler())])pipe.fit_transform(df[['a']])#oparray([[-1.32381804],&nbsp; &nbsp;[ 0.23106179],&nbsp; &nbsp;[ 1.09275626]])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python