Scikit-Learn 使用技巧
Scikit-Learn 使用技巧
1. 使用 ColumnTransformer 对 dataframe 不同的列分别进行不同的处理
from sklearn.preprocessing import OneHotEncoder
from sklearn.impute import SimpleImputer
from sklearn.compose import ColumnTransformer # 用到的函数,需要 scikit-learn 0.2以上的版本
ohe = OneHotEncoder()
si = SimpleImputer()
ct = ColumnTransfomer(
(ohe, ['c1', 'c2']), # 对 c1, c2 列进行 onehot 编码
(si, ['c3']), # 对 c3 进行 simpleimpute
remainder='passthrough' # 参数表示对上面没列出的剩下的列进行怎样的处理,参数值为 passthrough 表示保留原来的值
)
ct.fit_transform(X)

浙公网安备 33010602011771号