from sklearn.pipeline import Pipeline
cat_pipeline = Pipeline([
('selector', DataFrameSelector(cat_attribs)),
('label_binarizer', LabelBinarizer()),
])
教程中cat_pipeline 的流水线在运行的时候会出错,主要有两个原因
1、DataFrameselertor 是一个需要自定义的类,并且是按照流水线的子模块定义规则
from sklearn.base import BaseEstimator,TransformerMixin
class DataFrameSelector(BaseEstimator,TransformerMixin):
def __init__(self,attribs):
self.attribs = attribs
def fit(self,X,y=None):
return self
def transform(self,X):#这种情况下,X是dataframe格式
return X[self.attribs].values
2、LabelBinarizer()本身有fit_transform方法,但是 没有transform方法,故不能正常运行在pipeline流水线程序中
encoder = LabelBinarizer()
class LabelB_change(BaseEstimator,TransformerMixin): #
def __init__(self,label_model):
self.label_model = label_model
def fit(self,X,y=None):
return self
def transform(self,X):
return self.label_model.fit_transform(X)
通过将LabelBinarizer()封装在一个自定义的新类中(类包含transform方法)。将新的类作为pipeline的子模块就可以执行。