Thanks to visit codestin.com
Credit goes to github.com

Skip to content

MAINT Extend dtype preserved common test to check transform #24982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion sklearn/manifold/_isomap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
26 changes: 14 additions & 12 deletions sklearn/utils/estimator_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down