'tuple'对象不可调用

时间:2019-12-05 16:18:13

标签: python scikit-learn

我为自定义转换定义了以下类,并使用Scikit-Learn实现了必要的功能性方法:

from sklearn.base import BaseEstimator, TransformerMixin
rooms_ix, bedrooms_ix, population_ix, household_ix = 3, 4, 5, 6

class CombinedAttributesAdder(BaseEstimator, TransformerMixin):
    def __init__(self, add_bedrooms_per_room = True): # no *args or **kargs
        self.add_bedrooms_per_room = add_bedrooms_per_room
    def fit(self, X, y=None):
        return self # nothing else to do
    def transform(self, X, y=None):
        rooms_per_household = 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_household, population_per_household,bedrooms_per_room]
        else:
            return np.c_[X, rooms_per_household, population_per_household]

attr_adder = CombinedAttributesAdder(add_bedrooms_per_room=False)
housing_extra_attribs = attr_adder.transform(housing.values)

然后我在这样的管道中调用该类和其他类:

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

num_pipeline = Pipeline([
    ('imputer', SimpleImputer(strategy="median"))
    ('attribs_adder', CombinedAttributesAdder()),
    ('std_scaler', StandardScaler()),
])

housing_num_tr = num_pipeline.fit_transform(housing_num)

这将产生以上错误消息:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-52-bcea5f2689c0> in <module>
      4 num_pipeline = Pipeline([
      5     ('imputer', SimpleImputer(strategy="median"))
----> 6     ('attribs_adder', CombinedAttributesAdder()),
      7     ('std_scaler', StandardScaler()),
      8 ])

TypeError: 'tuple' object is not callable

1 个答案:

答案 0 :(得分:2)

此行后您缺少逗号:

('imputer', SimpleImputer(strategy="median"))