Home:ALL Converter>Dtype issue using transformer function from sklearn

Dtype issue using transformer function from sklearn

Ask Time:2022-10-22T01:13:41         Author:Fideiro

Json Formatter

I've been receiving the following error after running the following line:

transformer = preprocessing.FunctionTransformer(func=np.log1p, inverse_func=np.expm1)
scaler = preprocessing.StandardScaler()
X1_t = transformer.fit_transform(X_t)

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In [103], line 3
      1 transformer = preprocessing.FunctionTransformer(func=np.log1p, inverse_func=np.expm1)
      2 scaler = preprocessing.StandardScaler()
----> 3 X1_t = transformer.fit_transform(X_t)
      4 X2_t = scaler.fit_transform(X1_t)
      5 print(X2_t.shape)

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/sklearn/base.py:867, in TransformerMixin.fit_transform(self, X, y, **fit_params)
    863 # non-optimized default implementation; override when a better
    864 # method is possible for a given clustering algorithm
    865 if y is None:
    866     # fit method of arity 1 (unsupervised transformation)
--> 867     return self.fit(X, **fit_params).transform(X)
    868 else:
    869     # fit method of arity 2 (supervised transformation)
    870     return self.fit(X, y, **fit_params).transform(X)

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/sklearn/preprocessing/_function_transformer.py:195, in FunctionTransformer.fit(self, X, y)
    193 X = self._check_input(X, reset=True)
    194 if self.check_inverse and not (self.func is None or self.inverse_func is None):
--> 195     self._check_inverse_transform(X)
    196 return self

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/sklearn/preprocessing/_function_transformer.py:160, in FunctionTransformer._check_inverse_transform(self, X)
    157 idx_selected = slice(None, None, max(1, X.shape[0] // 100))
    158 X_round_trip = self.inverse_transform(self.transform(X[idx_selected]))
--> 160 if not np.issubdtype(X.dtype, np.number):
    161     raise ValueError(
    162         "'check_inverse' is only supported when all the elements in `X` is"
    163         " numerical."
    164     )
    166 if not _allclose_dense_sparse(X[idx_selected], X_round_trip):

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/core/generic.py:5575, in NDFrame.__getattr__(self, name)
   5568 if (
   5569     name not in self._internal_names_set
   5570     and name not in self._metadata
   5571     and name not in self._accessors
   5572     and self._info_axis._can_hold_identifiers_and_holds_name(name)
   5573 ):
   5574     return self[name]
-> 5575 return object.__getattribute__(self, name)

AttributeError: 'DataFrame' object has no attribute 'dtype'

I was able to run this code before, but had to reinstall Jupyter notebook and when reinstalling and downloading all libraries, started getting this issue. My hypothesis is that it is related to combinations of versions of Jupyter + libraries (pandas, sklearn), but don't remember the versions I previously had.

Any idea?

Author:Fideiro,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/74157087/dtype-issue-using-transformer-function-from-sklearn
yy