【问题标题】:TypeError: All intermediate steps should be transformers and implement fit and transform or be the string 'passthrough' [closed]TypeError:所有中间步骤都应该是转换器并实现拟合和转换,或者是字符串'passthrough' [关闭]
【发布时间】:2021-09-12 19:01:20
【问题描述】:

我正在尝试为住房数据创建转换管道

from sklearn.base import BaseEstimator, TransformerMixin
rooms_ix, bedrooms_ix, population_ix, household_ix = 3,4,5,6
class CombineAttributesAdder(BaseEstimator, TransformerMixin):
    def __init__(self, add_bedrooms_per_room = True):
        self.add_bedrooms_per_room = add_bedrooms_per_room
    def fit(self, X, y=None):
        return self
    def transfrom(self, X, y=None):
        rooms_per_househond = X[:,rooms_ix]/X[:,household_ix]
        population_per_household = X[:,population_ix]/ X[:, household_ix]
        if self.add_bedrooms_per_room:
            bedrooms_per_room = X[:,bedrooms_ix]/X[:rooms_ix]
            return np.c_[X, rooms_per_househond, population_per_household, bedrooms_per_room]
        else:
            return np.c_[X, rooms_per_househond, population_per_household]

我用于管道的管道代码:-

from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from imblearn.pipeline import make_pipeline
num_pipline = Pipeline([
    ('imputer', SimpleImputer(missing_values=np.nan, strategy='median')),
    ('attribs_adder', CombineAttributesAdder(add_bedrooms_per_room=False)),
    ('stand_scaler', StandardScaler()),
])
housing_num_transform = num_pipline.fit_transform(housing_num)

它通过错误:-

【问题讨论】:

  • 您定义了transfrom 方法,但指的是transform
  • 请添加更多详细信息,我需要更改哪一行

标签: python python-3.x machine-learning scikit-learn


【解决方案1】:

CombineAttributesAdder 类的正文中,您拼错了单词 transform。方法定义应如下所示:

class CombineAttributesAdder(BaseEstimator, TransformerMixin):

    def __init__(self, add_bedrooms_per_room = True):
        self.add_bedrooms_per_room = add_bedrooms_per_room

    def fit(self, X, y=None):
        return self

    def transform(self, X, y=None):  # <-- not 'transfrom'
        ...

对于每个转换器,Pipeline 对象将调用 fit_transform,而 fit_transform 又将调用方法 fittransform。因为这个笔误,程序找不到对应的方法,抛出错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-27
    • 2018-07-23
    • 2021-10-06
    • 2020-08-23
    • 2015-10-28
    • 2015-01-21
    • 2013-10-21
    相关资源
    最近更新 更多