如何使用 ColumnTransformer API將多個(gè)轉(zhuǎn)換器應(yīng)用到單個(gè)pandas DataFrame 列?例如,我想取立方根,然后標(biāo)準(zhǔn)化 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')我得到兩列獨(dú)立的列,一列包含立方根,另一列包含標(biāo)準(zhǔn)化原始值。如何一次性應(yīng)用兩個(gè)變壓器?
1 回答

POPMUISE
TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊
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]])
添加回答
舉報(bào)
0/150
提交
取消