8000 ENH Add dtype preservation to SkewedChi2Sampler by rprkh · Pull Request #24349 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

ENH Add dtype preservation to SkewedChi2Sampler #24349

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

Closed
Closed
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
6 changes: 6 additions & 0 deletions doc/whats_new/v1.2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ Changelog
:pr:`11860` by :user:`Pierre Ablin <pierreablin>`,
:pr:`22527` by :user:`Meekail Zain <micky774>` and `Thomas Fan`_.

:mod:`sklearn.kernel_approximation`
............................

- |Enhancement| :class:`kernel_approximation.SkewedChi2Sampler` now preserves
dtype for `numpy.float32` inputs. :pr:`24349` by `Rahil Parikh <rprkh>`.

:mod:`sklearn.linear_model`
...........................

Expand Down
9 changes: 9 additions & 0 deletions sklearn/kernel_approximation.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,9 @@ class SkewedChi2Sampler(
1.0
"""

def _more_tags(self):
return {"preserves_dtype": [np.float64, np.float32]}

def __init__(self, *, skewedness=1.0, n_components=100, random_state=None):
self.skewedness = skewedness
self.n_components = n_components
Expand Down Expand Up @@ -477,6 +480,12 @@ def fit(self, X, y=None):
# transform by inverse CDF of sech
self.random_weights_ = 1.0 / np.pi * np.log(np.tan(np.pi / 2.0 * uniform))
self.random_offset_ = random_state.uniform(0, 2 * np.pi, size=self.n_components)

# With this we preserve the dtype of X in transform() later
if X.dtype == np.float32:
self.random_weights_ = self.random_weights_.astype(X.dtype, copy=False)
self.random_offset_ = self.random_offset_.astype(X.dtype, copy=False)

self._n_features_out = self.n_components
return self

Expand Down
0