为什么我的sklearn.pipeline中预处理方法的输出不对齐?

时间:2018-04-23 18:27:52

标签: python machine-learning scikit-learn

我正在学习这本书" Hands On Machine Learning"并编写一些转换管道代码来清理我的数据并找到相同管道方法的输出根据我选择输入的数据帧的大小而变化。这是代码:

from sklearn.base import BaseEstimator,TransformerMixin    
class DataFrameSelector(BaseEstimator, TransformerMixin):
    def __init__(self, attribute_names):
      self.attribute_names =attribute_names
    def fit(self,X,y=None):
      return self
    def transform(self,X):
      return X[self.attribute_names].values

from sklearn.pipeline import FeatureUnion

class CustomLabelBinarizer(BaseEstimator, TransformerMixin):
    def __init__(self, sparse_output=False):
      self.sparse_output = sparse_output
    def fit(self, X, y=None):
      return self
    def transform(self, X, y=None):
      enc = LabelBinarizer(sparse_output=self.sparse_output)
      return enc.fit_transform(X)

num_attribs = list(housing_num)
cat_attribs = ['ocean_proximity']

num_pipeline = Pipeline([
    ('selector', DataFrameSelector(num_attribs)),
    ('imputer', Imputer(strategy='median')),
    ('attribs_adder', CombinedAttributesAdder()),
    ('std_scalar', StandardScaler())
])

cat_pipeline = Pipeline([
    ('selector', DataFrameSelector(cat_attribs)),
    ('label_binarizer', CustomLabelBinarizer())
])

full_pipeline = FeatureUnion(transformer_list=[
    ('num_pipeline', num_pipeline),
    ('cat_pipeline', cat_pipeline)
])
housing_prepared = full_pipeline.fit_transform(housing)
data_prepared = full_pipeline.transform(housing.iloc[:5])
data_prepared1 = full_pipeline.transform(housing.iloc[:1000])
data_prepared2 = full_pipeline.transform(housing.iloc[:10000])
print(data_prepared.shape)
print(data_prepared1.shape)
print(data_prepared2.shape)

这三个印刷品的输出将是     (5,14)     (1000,15)     (10000,16) 有人能帮我解释一下吗?

1 个答案:

答案 0 :(得分:2)

这是因为,在CustomLabelBinarizer中,您在每次调用transform()时都适合LabelBinarizer,因此每次都会学习不同的标签,因此每次运行时会有不同的列数,具体取决于行。

将其更改为:

class CustomLabelBinarizer(BaseEstimator, TransformerMixin):
    def __init__(self, sparse_output=False):
      self.sparse_output = sparse_output
    def fit(self, X, y=None):
      self.enc = LabelBinarizer(sparse_output=self.sparse_output)
      self.enc.fit(X)
      return self
    def transform(self, X, y=None):
      return self.enc.transform(X)

现在我的代码形式正确:

(5, 14)
(1000, 14)
(10000, 14)

注意:同一问题有been asked here。我假设您使用link here代码。如果您正在使用任何其他网站,可能是代码中存在我链接的旧版本代码。尝试使用上面链接中的代码获取无错误的更新版本。