5
5
import pandas as pd
6
6
from pandas .api .types import is_numeric_dtype
7
7
8
- import scipy .sparse
8
+ from scipy .sparse import issparse , spmatrix
9
9
10
10
import sklearn .utils
11
11
from sklearn import preprocessing
14
14
from sklearn .utils .multiclass import type_of_target
15
15
16
16
from autoPyTorch .data .base_target_validator import BaseTargetValidator , SupportedTargetTypes
17
- from autoPyTorch .utils . common import SparseMatrixType
17
+ from autoPyTorch .data . utils import ispandas
18
18
19
19
20
- ArrayType = Union [np .ndarray , SparseMatrixType ]
20
+ ArrayType = Union [np .ndarray , spmatrix ]
21
21
22
22
23
23
def _check_and_to_array (y : SupportedTargetTypes ) -> ArrayType :
@@ -71,7 +71,7 @@ def _fit(
71
71
return self
72
72
73
73
if y_test is not None :
74
- if hasattr (y_train , "iloc" ):
74
+ if ispandas (y_train ):
75
75
y_train = pd .concat ([y_train , y_test ], ignore_index = True , sort = False )
76
76
elif isinstance (y_train , list ):
77
77
y_train = y_train + y_test
@@ -100,7 +100,7 @@ def _fit(
100
100
if ndim > 1 :
101
101
self .encoder .fit (y_train )
102
102
else :
103
- if hasattr (y_train , 'iloc' ):
103
+ if ispandas (y_train ):
104
104
y_train = cast (pd .DataFrame , y_train )
105
105
self .encoder .fit (y_train .to_numpy ().reshape (- 1 , 1 ))
106
106
else :
@@ -131,7 +131,7 @@ def _transform_by_encoder(self, y: SupportedTargetTypes) -> np.ndarray:
131
131
shape = np .shape (y )
132
132
if len (shape ) > 1 :
133
133
y = self .encoder .transform (y )
134
- elif hasattr ( y , 'iloc' ):
134
+ elif ispandas ( y ):
135
135
# The Ordinal encoder expects a 2 dimensional input.
136
136
# The targets are 1 dimensional, so reshape to match the expected shape
137
137
y = cast (pd .DataFrame , y )
@@ -192,7 +192,7 @@ def inverse_transform(self, y: SupportedTargetTypes) -> np.ndarray:
192
192
y = self .encoder .inverse_transform (y )
193
193
else :
194
194
# The targets should be a flattened array, hence reshape with -1
195
- if hasattr ( y , 'iloc' ):
195
+ if ispandas ( y ):
196
196
y = cast (pd .DataFrame , y )
197
197
y = self .encoder .inverse_transform (y .to_numpy ().reshape (- 1 , 1 )).reshape (- 1 )
198
198
else :
@@ -216,7 +216,7 @@ def _check_data(self, y: SupportedTargetTypes) -> None:
216
216
217
217
if not isinstance (y , (np .ndarray , pd .DataFrame ,
218
218
List , pd .Series )) \
219
- and not scipy . sparse . issparse (y ): # type: ignore[misc]
219
+ and not issparse (y ): # type: ignore[misc]
220
220
raise ValueError ("AutoPyTorch only supports Numpy arrays, Pandas DataFrames,"
221
221
" pd.Series, sparse data and Python Lists as targets, yet, "
222
222
"the provided input is of type {}" .format (
@@ -225,8 +225,8 @@ def _check_data(self, y: SupportedTargetTypes) -> None:
225
225
226
226
# Sparse data muss be numerical
227
227
# Type ignore on attribute because sparse targets have a dtype
228
- if scipy . sparse . issparse (y ) and not np .issubdtype (y .dtype .type , # type: ignore[union-attr]
229
- np .number ):
228
+ if issparse (y ) and not np .issubdtype (y .dtype .type , # type: ignore[union-attr]
229
+ np .number ):
230
230
raise ValueError ("When providing a sparse matrix as targets, the only supported "
231
231
"values are numerical. Please consider using a dense"
232
232
" instead."
@@ -245,10 +245,10 @@ def _check_data(self, y: SupportedTargetTypes) -> None:
245
245
246
246
# No Nan is supported
247
247
has_nan_values = False
248
- if hasattr ( y , 'iloc' ):
248
+ if ispandas ( y ):
249
249
has_nan_values = cast (pd .DataFrame , y ).isnull ().values .any ()
250
- if scipy . sparse . issparse (y ):
251
- y = cast (scipy . sparse . spmatrix , y )
250
+ if issparse (y ):
251
+ y = cast (spmatrix , y )
252
252
has_nan_values = not np .array_equal (y .data , y .data )
253
253
else :
254
254
# List and array like values are considered here
0 commit comments