如何使用 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')我得到两列独立的列,一列包含立方根,另一列包含标准化原始值。如何一次性应用两个变压器?
1 回答
POPMUISE
TA贡献1765条经验 获得超5个赞
from sklearn.pipeline import Pipeline
import pandas as pd
import numpy as np
from sklearn.preprocessing import FunctionTransformer, StandardScaler
df = pd.DataFrame(
np.array([[1., 2., 3.], [4., 5., 6.], [7., 8., 9.]]),
columns=['a', 'b', 'c']
)
pipe = Pipeline([('function_transformer', FunctionTransformer(np.cbrt)),
('standard_scalar', StandardScaler())])
pipe.fit_transform(df[['a']])
#op
array([[-1.32381804],
[ 0.23106179],
[ 1.09275626]])
添加回答
举报
0/150
提交
取消