Skip to content
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

Fix bug due to precision in CategoricalMatrix._get_col_stds #391

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Changelog

- Added a new function, :func:`tabmat.from_polars`, to convert a :class:`polars.DataFrame` into a :class:`tabmat.SplitMatrix`.

**Bug fix:**

- Fixed a bug in :meth:`tabmat.CategoricalMatrix.standardize` that sometimes returned ``nan`` values for the standard deviation due to numerical instability if using ``np.float32`` precision.

4.0.1 - 2024-06-25
------------------

Expand Down
5 changes: 4 additions & 1 deletion src/tabmat/categorical_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,10 @@ def _get_col_stds(self, weights: np.ndarray, col_means: np.ndarray) -> np.ndarra
# but because X_ij is either {0, 1}
# we don't actually need to square.
mean = self.transpose_matvec(weights)
return np.sqrt(mean - col_means**2)
vars = mean - col_means**2
# If using float32, we can get negative values due to precision errors
vars[vars < 0] = 0
return np.sqrt(vars)
mlondschien marked this conversation as resolved.
Show resolved Hide resolved

def __getitem__(self, item):
row, col = _check_indexer(item)
Expand Down