gpt4 book ai didi

python - 如何将 sklearn 的 TransformedTargetRegressor 与自定义数据转换器一起使用?

转载 作者:行者123 更新时间:2023-12-04 15:30:26 26 4
gpt4 key购买 nike

我想使用 sklearn.compose.TransformedTargetRegressor,如 this answer 中所示.但是,转换器是自定义转换器,我遇到了错误。

在这个最小的示例中,目标值应乘以 10,然后在预测时再次除以 10。 (在我的实际应用中,目标值必须从非数字格式转换为数字格式。)

import numpy as np
import sklearn
from sklearn.compose import TransformedTargetRegressor
from sklearn.linear_model import LinearRegression

class MyTransform(sklearn.base.TransformerMixin):
def fit(self, *_, **__):
return self

def transform(self, X):
return np.array(X)*10

def inverse_transform(self, X):
return np.array(X)/10


def MyLinearRegression():
return TransformedTargetRegressor(
regressor=LinearRegression(),
transformer=MyTransform()
)


if __name__ == '__main__':
model = MyLinearRegression()
model.fit(X=[[1], [2], [3]], y=[1, 2, 3]) # raises TypeError

这引发了:

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm Community Edition 2019.3.3\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/Users/me/.PyCharmCE2019.3/config/scratches/scratch.py", line 26, in <module>
model.fit(X=[[1], [2], [3]], y=[1, 2, 3]) # raises TypeError
File "C:\Users\me\.virtualenvs\project--3333Ox_\lib\site-packages\sklearn\compose\_target.py", line 185, in fit
self._fit_transformer(y_2d)
File "C:\Users\me\.virtualenvs\project--3333Ox_\lib\site-packages\sklearn\compose\_target.py", line 127, in _fit_transformer
self.transformer_ = clone(self.transformer)
File "C:\Users\me\.virtualenvs\project--3333Ox_\lib\site-packages\sklearn\base.py", line 64, in clone
raise TypeError("Cannot clone object '%s' (type %s): "
TypeError: Cannot clone object '<__main__.MyTransform object at 0x000001653B2FA9A0>' (type <class '__main__.MyTransform'>): it does not seem to be a scikit-learn estimator as it does not implement a 'get_params' methods.

最佳答案

您只需要继承 sklearn.base.BaseEstimator 以及 transformermixin :)。类型错误说:

it does not seem to be a scikit-learn estimator

所以你只需要让它成为一个 :D。下面的代码应该可以工作。

import numpy as np
import sklearn
from sklearn.compose import TransformedTargetRegressor
from sklearn.linear_model import LinearRegression

class MyTransform(sklearn.base.BaseEstimator, sklearn.base.TransformerMixin):
def fit(self, *_, **__):
return self

def transform(self, X):
return np.array(X)*10

def inverse_transform(self, X):
return np.array(X)/10


def MyLinearRegression():
return TransformedTargetRegressor(
regressor=LinearRegression(),
transformer=MyTransform()
)



model = MyLinearRegression()
model.fit(X=[[1], [2], [3]], y=[1, 2, 3])

关于python - 如何将 sklearn 的 TransformedTargetRegressor 与自定义数据转换器一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61365612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com