Python Typeerror:所有中间步骤都应该是转换器并实现拟合和转换
Posted
技术标签:
【中文标题】Python Typeerror:所有中间步骤都应该是转换器并实现拟合和转换【英文标题】:Python Typeerror: All intermediate steps should be transformers and implement fit and transform 【发布时间】:2021-01-27 00:00:49 【问题描述】:我目前正在阅读 Aurélien Géron 的《使用 Scikit-Learn 和 TensorFlow 进行机器学习实践》一书。当我运行以下代码(我复制的)时,我收到一条错误消息。错误信息似乎很清楚,但老实说我还是不明白。显然,我缺乏理解,但即使经过大量审查,我也找不到问题所在。谁能帮帮忙?
from sklearn.base import BaseEstimator, TransformerMixin
rooms_ix, bedrooms_ix, population_ix, households_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):
rooms_per_household = X[:, rooms_ix] / X[:, households_ix]
population_per_household = X[:, population_ix] / X[:, households_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(最近一次调用 最后)在 20 从 sklearn.pipeline 导入管道 21 从 sklearn.preprocessing 导入 StandardScaler ---> 22 num_pipeline = Pipeline([('imputer', SimpleImputer(strategy ="median")), ('attribs_adder', CombinedAttributesAdder()), ('std_scaler', StandardScaler()),]) 23 Housing_num_tr = num_pipeline.fit_transform(housing_num)
~\Miniconda3\lib\site-packages\sklearn\utils\validation.py 在 inner_f(*args, **kwargs) 70 未来警告) 71 kwargs.update(k: arg for k, arg in zip(sig.parameters, args)) ---> 72 返回 f(**kwargs) 第73章 74
~\Miniconda3\lib\site-packages\sklearn\pipeline.py in init(self, 步骤,记忆,详细) 112 自我记忆 = 记忆 113 self.verbose = 详细 --> 114 self._validate_steps() 115 116 def get_params(self, deep=True):
~\Miniconda3\lib\site-packages\sklearn\pipeline.py 在 _validate_steps(自我) 157 if (not (hasattr(t, "fit") or hasattr(t, "fit_transform")) 158 hasattr(t,“转换”)): --> 159 raise TypeError("所有中间步骤都应该是" 160《变形金刚与实现适配变形》 161 "或者是字符串'passthrough'"
TypeError:所有中间步骤都应该是转换器并实现 适合并转换或成为字符串“直通” 'CombinedAttributesAdder()'(类型
main.CombinedAttributesAdder'>) 没有
非常感谢!
【问题讨论】:
【参考方案1】:对我来说,问题可能出在您的缩进错误(除非只是输入错误)。方法 fit()
和 transform()
没有正确缩进(attr_adder
和 housing_extra_attribs
分配也是如此)。这样,您在管道中使用的类 CombinedAttributesAdder
的实例不是导致错误的转换器。
【讨论】:
呃,我怎么能忽略这一点.. 非常感谢 Alessandro!以上是关于Python Typeerror:所有中间步骤都应该是转换器并实现拟合和转换的主要内容,如果未能解决你的问题,请参考以下文章
Python 3:获取TypeError:切片必须是整数...但我相信它们
redux-promise:未捕获的 TypeError:中间件不是函数
Python 错误:TypeError:字符串格式化期间并非所有参数都转换