Python使用 sklearn pipeline进行数据清洗

本文介绍了如何使用Python的sklearn库创建Pipeline来处理数据。首先,通过Imputer进行中值填充缺失值,然后利用CombinedAttributesAdder添加比例列,接着使用StandardScaler进行数据标准化。同时,定义了一个DataFrameSelector类来选择DataFrame中的特定列。最后,将数值型和类别型特征的处理流程结合,通过FeatureUnion进行联合处理,完成整个数据预处理过程。
摘要由CSDN通过智能技术生成
  1. setup pipeline

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

num_pipeline = Pipeline([
(‘imputer’, Imputer(strategy=“median”)), #中值写入
(‘attribs_adder’, CombinedAttributesAdder()),#增加比例列
(‘std_scaler’, StandardScaler()),#标准化
])
housing_num_tr = num_pipeline.fit_transform(housing_num)

And a transformer to just select a subset of the Pandas DataFrame columns:
from sklearn.base import BaseEstimator, TransformerMixin

Create a class to select numerical or categorical columns

since Scikit-Learn doesn’t handle DataFrames yet

class DataFrameSelector(BaseEstimator, TransformerMixin):
def init(self, attribute_names):
self.attribute_names = attribute_names
def

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值