我正在尝试使用两个步骤创建一个sklearn管道:
但是,我的数据包含数字和分类变量,我已使用它转换为虚拟变量pd.get_dummies.我想标准化数值变量,但保留虚拟对象.我这样做是这样的:
X = dataframe containing both numeric and categorical columns
numeric = [list of numeric column names]
categorical = [list of categorical column names]
scaler = StandardScaler()
X_numeric_std = pd.DataFrame(data=scaler.fit_transform(X[numeric]), columns=numeric)
X_std = pd.merge(X_numeric_std, X[categorical], left_index=True, right_index=True)
Run Code Online (Sandbox Code Playgroud)
但是,如果我要创建一个管道,如:
pipe = sklearn.pipeline.make_pipeline(StandardScaler(), KNeighborsClassifier())
Run Code Online (Sandbox Code Playgroud)
它会标准化我的DataFrame中的所有列.有没有办法在仅标准化数字列时执行此操作?