使用sklearn.compose.ColumnTransformer进行批量数据转换
作者:互联网
sklearn.compose.ColumnTransformer
可以用来构建一个数据转换器,用允许单独转换输入的不同列或列子集,并且每个转换器生成的特征将串联起来以形成单个特征空间。这对于异构或列式数据非常有用,可以将多个特征提取机制或转换组合到单个转换器中。
配合sklearn.pipeline.make_pipeline
可以高效地构建一个可以复用的数据转换器,常用的可以配合使用StandardScaler
以及Normalizer
from sklearn.pipeline import make_pipeline
from sklearn.compose import make_column_transformer
from sklearn.preprocessing import StandardScaler, MinMaxScaler
X = train.drop('target', axis=1).copy()
y = train.target.copy()
test_X = test.copy()
# Scaling and Nomalization
transformer = make_pipeline(
StandardScaler()
)
columns = X.columns[:-11]
transformer_new = make_pipeline(
StandardScaler()
)
new_columns = X.columns[-11:]
preprocessor = make_column_transformer(
(transformer, columns),
(transformer_new, new_columns),
)
使用:
train_X = preprocessor.fit_transform(train_X)
val_X = preprocessor.transform(val_X)
标签:transformer,compose,ColumnTransformer,make,pipeline,train,columns,sklearn 来源: https://www.cnblogs.com/Asp1rant/p/16243398.html