diff --git a/sklearn/manifold/_isomap.py b/sklearn/manifold/_isomap.py index 914a012a0f0a0..e2451f31cc1c2 100644 --- a/sklearn/manifold/_isomap.py +++ b/sklearn/manifold/_isomap.py @@ -409,7 +409,13 @@ def transform(self, X): n_samples_fit = self.nbrs_.n_samples_fit_ n_queries = distances.shape[0] - G_X = np.zeros((n_queries, n_samples_fit)) + + if hasattr(X, "dtype") and X.dtype == np.float32: + dtype = np.float32 + else: + dtype = np.float64 + + G_X = np.zeros((n_queries, n_samples_fit), dtype) for i in range(n_queries): G_X[i] = np.min(self.dist_matrix_[indices[i]] + distances[i][:, None], 0) diff --git a/sklearn/utils/estimator_checks.py b/sklearn/utils/estimator_checks.py index 7026159f16287..7dc36ca8cd58e 100644 --- a/sklearn/utils/estimator_checks.py +++ b/sklearn/utils/estimator_checks.py @@ -1743,18 +1743,20 @@ def check_transformer_preserve_dtypes(name, transformer_orig): X_cast = X.astype(dtype) transformer = clone(transformer_orig) set_random_state(transformer) - X_trans = transformer.fit_transform(X_cast, y) - - if isinstance(X_trans, tuple): - # cross-decompostion returns a tuple of (x_scores, y_scores) - # when given y with fit_transform; only check the first element - X_trans = X_trans[0] - - # check that the output dtype is preserved - assert X_trans.dtype == dtype, ( - f"Estimator transform dtype: {X_trans.dtype} - " - f"original/expected dtype: {dtype.__name__}" - ) + X_trans1 = transformer.fit_transform(X_cast, y) + X_trans2 = transformer.fit(X_cast, y).transform(X_cast) + + for Xt, method in zip([X_trans1, X_trans2], ["fit_transform", "transform"]): + if isinstance(Xt, tuple): + # cross-decompostion returns a tuple of (x_scores, y_scores) + # when given y with fit_transform; only check the first element + Xt = Xt[0] + + # check that the output dtype is preserved + assert Xt.dtype == dtype, ( + f"{name} (method={method}) does not preserve dtype. " + f"Original/Expected dtype={dtype.__name__}, got dtype={Xt.dtype}." + ) @ignore_warnings(category=FutureWarning)