[英]Write custom transformer in sklearn which returns .predict of estimator in .transform
We have a custom transformer 我们有一个定制的变压器
class EstimatorTransformer(base.BaseEstimator, base.TransformerMixin):
def __init__(self, estimator):
self.estimator = estimator
def fit(self, X, y):
self = self.estimator.fit(X,y)
return self
def transform(self, X):
return self.estimator.predict(X)
And there is an assert statement 并且有一个断言语句
city_trans = EstimatorTransformer(city_est)
city_trans.fit(features,target)
assert ([r[0] for r in city_trans.transform(data[:5])]
== city_est.predict(data[:5]))
where 哪里
city_est
is the estimator we can pass.city_est
是我们可以通过的估计量。 I am usingcity_est = city_est = Ridge(alpha = 1)
我正在使用
city_est = city_est = Ridge(alpha = 1)
but I get an error in self = self.estimator.fit(X,y)
. 但我在
self = self.estimator.fit(X,y)
遇到错误。 What I might be doing wrong here. 我在这里可能做错了。 I know that
fit()
returns self
. 我知道
fit()
返回self
。 How should I make this assertion work? 我应该如何使这个断言起作用?
You are doing wrong assignment in this line: 您在这一行中分配错误:
self = self.estimator.fit(X,y)
Here, self is the current class (EstimatorTransformer) and you are trying to assign it a different class. 在这里,self是当前的类(EstimatorTransformer),您正在尝试为其分配其他类。
You can just write: 您可以这样写:
def fit(self, X, y):
self.estimator.fit(X,y)
return self
and it will work. 它会工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.