gpt4 book ai didi

python - 使用自定义转换器子类对 sklearn 管道进行评分时出现 AttributeError,但在拟合时则不会

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:27 25 4
gpt4 key购买 nike

我在理解如何创建 sklearn 转换器的子类时遇到问题。我想为冗长的代码示例道歉,我试图使最小的可重复性,但无法重新创建错误。希望您会看到大部分代码示例都是我记录的。

转换器在下面的代码片段中进行了描述。

class PCAVarThreshSelector(PCA):
"""
Description
-----------
Selects the columns that can explain a certain percentage of the variance in a data set

Authors
-------
Eden Trainor

Notes
-----
1. PCA has a principole component limit of 4459 components, no matter how many more features you put into
it this is a hrad limit of how many components it will return to you.

"""

def __init__(self,
n_components=None,
copy=True,
whiten=False,
svd_solver='auto',
tol=0.0,
iterated_power='auto',
random_state=None,
explained_variance_thresh = 0.8):


super(PCAVarThreshSelector, self).__init__(n_components, copy, whiten, svd_solver, tol, iterated_power, random_state)


self.explained_variance_thresh = explained_variance_thresh

def find_nearest_index(self, array, value):
"""
Description
-----------
Finds the index of the coefficient in an array nearest a certain value.


Args
----
array: np.ndarray, (number_of_componants,)
Array containing coeffficients

value: int,
Index of coefficient in array closset to this value is found.


Returns
-------
index: int,
Index of coefficient in array closest to value.
"""

index = (np.abs(array - value)).argmin()

return index

def fit(self, X, y = None):
"""
Description
-----------
Fits the PCA and calculates the index threshold index of the cumulative explained variance ratio array.


Args
----
X: DataFrame, (examples, features)
Pandas DataFrame containing training example features

y: array/DataFrame, (examples,)
(Optional) Training example labels

Returns
-------
self: PCAVarThreshSelector instance
Returns transfromer instance with fitted instance variables on training data.
"""

#PCA fit the dataset
super(PCAVarThreshSelector, self).fit(X)

#Get the cumulative explained variance ratio array (ascending order of cumulative variance explained)
cumulative_EVR = self.explained_variance_ratio_.cumsum()

#Finds the index corresponding to the threshold amount of variance explained
self.indx = self.find_nearest_index(array = cumulative_EVR,
value = self.explained_variance_thresh)


return self

def transform(self, X):
"""
Description
-----------
Selects all the principle components up to the threshold variance.


Args
----
X: DataFrame, (examples, features)
Pandas DataFrame containing training example features


Returns
-------
self: np.ndarray, (examples, indx)
Array containing the minimum number of principle componants required by explained_variance_thresh.
"""

all_components = super(PCAVarThreshSelector, self).transform(X) #To the sklean limit

return all_components[:, :self.indx]

我用我的数据测试了这个类,它在一个前面有 RobustScaler 的简单管道中按预期工作。在这个简单的管道中,类将按预期适应和转换。

然后我将简单的管道放入另一个管道中,并使用估计器,希望对管道进行 .fit() 和 .score() :

model_pipe = Pipeline([('ppp', Pipeline([('rs', RobustScaler()),
('pcavts', PCAVarThreshSelector(whiten = True))])),
('lin_reg', LinearRegression())])

管道配合无误。然而,当我尝试评分时,我得到一个 AttributeError:

AttributeError                            Traceback (most recent call last)
<ipython-input-92-cf336db13fe1> in <module>()
----> 1 model_pipe.score(X_test, y_test)

~\Anaconda3\lib\site-packages\sklearn\utils\metaestimators.py in <lambda>(*args, **kwargs)
113
114 # lambda, but not partial, allows help() to work with update_wrapper
--> 115 out = lambda *args, **kwargs: self.fn(obj, *args, **kwargs)
116 # update the docstring of the returned function
117 update_wrapper(out, self.fn)

~\Anaconda3\lib\site-packages\sklearn\pipeline.py in score(self, X, y, sample_weight)
484 for name, transform in self.steps[:-1]:
485 if transform is not None:
--> 486 Xt = transform.transform(Xt)
487 score_params = {}
488 if sample_weight is not None:

~\Anaconda3\lib\site-packages\sklearn\pipeline.py in _transform(self, X)
424 for name, transform in self.steps:
425 if transform is not None:
--> 426 Xt = transform.transform(Xt)
427 return Xt
428

<ipython-input-88-9153ece48646> in transform(self, X)
114 all_components = super(PCAVarThreshSelector, self).transform(X) #To the sklean limit
115
--> 116 return all_components[:, :self.indx]
117

AttributeError: 'PCAVarThreshSelector' object has no attribute 'indx'

我最初认为这与我在类里面调用 super() 的方式有关。根据this博客文章中,我认为当管道被 .score() 编辑时,该类正在重新启动,因此在 fit 方法中创建的属性在评分时不再存在。我尝试了其他一些调用父类方法的方法,包括:super().method、PCA.method(),以及博客文章中建议的方法,但都给出了相同的错误。

我认为博客的解决方案可能特定于 Python 2,而我的代码是 Python 3。

但是,当尝试以最小可重现的方式重现此问题时,我不再收到错误。

from sklearn.datasets import make_regression
from sklearn.base import TransformerMixin, BaseEstimator
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import Pipeline

X, y = make_regression() #Just some dummy regression data for demonstrative purposes.

class BaseTransformer(TransformerMixin, BaseEstimator):

def __init__(self):
print("Base Init")

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

def transform(self, X):
return X

class DerivedTransformer(BaseTransformer):

def __init__(self):
super(DerivedTransformer, self).__init__()
print("Dervied init")

def fit(self, X, y = None):
super(DerivedTransformer, self).fit(X, y)
self.new_attribute = 0.0001
return self

def transform(self, X):
output = super(DerivedTransformer, self).transform(X)
output += self.new_attribute

return output

base_pipeline = Pipeline([('base_transformer', BaseTransformer()),
('linear_regressor', LinearRegression())])

derived_pipeline = Pipeline([('derived_transformer', DerivedTransformer()),
('linear_regressor', LinearRegression())])

上面的代码按预期运行,没有错误。我不知所措。谁能帮我解决这个错误?

最佳答案

那是因为您还没有覆盖(实现)fit_transform()方法。

只需将以下部分添加到您的 PCAVarThreshSelector 中将解决问题:

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

原因:管道将尝试首先调用 fit_transform() 所有步骤的方法(不包括最后一个步骤)。

这个fit_transform()方法只是调用 fit() 的简写然后transform()其定义与我上面的定义类似。

但在某些情况下,例如PCA ,或CountVectorizer在 scikit-learn 等中,此方法的实现方式有所不同,以使处理速度更快,因为:

  • 与检查 fit() 中的数据相比,检查/验证(以及转换)数据到适当的形式只需执行一次然后在 transform() 中再次检查
  • 其他一些重复性任务可以轻松简化

由于您继承自PCA,当您调用model_pipe.fit()时,它使用fit_transform()来自 PCA,因此永远不会进入 fit()您定义的方法(因此您的类对象永远不会包含任何 indx 属性。

但是当你调用score()时,只有transform()在管道的所有中间步骤上调用并转到您实现的 transform() 。因此出现错误。

如果您实现 fit_transform(),则您关于 BaseTransformer 和 DerivedTransformer 的示例可以重现您的问题。在BaseTransformer有点不同。

关于python - 使用自定义转换器子类对 sklearn 管道进行评分时出现 AttributeError,但在拟合时则不会,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52989405/

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