相关疑难解决方法(0)

如何在sklearn管道中仅标准化数值变量?

我正在尝试使用两个步骤创建一个sklearn管道:

  1. 标准化数据
  2. 使用KNN拟合数据

但是,我的数据包含数字和分类变量,我已使用它转换为虚拟变量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中的所有列.有没有办法在仅标准化数字列时执行此操作?

python scikit-learn

9
推荐指数
2
解决办法
5378
查看次数

标签 统计

python ×1

scikit-learn ×1