【发布时间】:2018-10-23 02:56:05
【问题描述】:
我想在 pandas 数据框列的子集上使用 sklearn.preprocessing.StandardScaler。在管道之外,这是微不足道的:
df[['A', 'B']] = scaler.fit_transform(df[['A', 'B']])
但现在假设我在 df 中具有字符串类型的列“C”和以下管道定义
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
pipeline = Pipeline([
('standard', StandardScaler())
])
df_scaled = pipeline.fit_transform(df)
如何告诉 StandardScaler 仅缩放 A 列和 B 列?
我习惯了 SparkML 管道,可以将要缩放的特征传递给缩放器组件的构造函数:
normalizer = Normalizer(inputCol="features", outputCol="features_norm", p=1.0)
注意:特征列包含一个稀疏向量,其中包含由 Spark 的 VectorAssembler 创建的所有数字特征列
【问题讨论】:
标签: python pandas scikit-learn