Skip to content

Add tests for sparse mean_var accuracy #92

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 3 commits into from
Apr 29, 2025
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
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ overrides.matrix.extras.features = [
]
overrides.matrix.extras.dependencies = [
{ if = [ "full" ], value = "scipy-stubs" },
{ if = [ "full" ], value = "scikit-learn" },
]

[[tool.hatch.envs.hatch-test.matrix]]
Expand Down
36 changes: 36 additions & 0 deletions tests/test_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,42 @@ def test_mean_var(
np.testing.assert_array_almost_equal(var, var_expected) # type: ignore[arg-type]


@pytest.mark.skipif(not find_spec("sklearn"), reason="sklearn not installed")
@pytest.mark.array_type(Flags.Sparse, skip=Flags.Matrix | Flags.Dask | Flags.Disk | Flags.Gpu)
@pytest.mark.parametrize("axis", [0, 1])
def test_mean_var_sparse_64(array_type: ArrayType[types.CSArray], axis: Literal[0, 1]) -> None:
"""Test that we’re equivalent for 64 bit."""
from sklearn.utils.sparsefuncs import mean_variance_axis

mtx = array_type.random((10000, 1000), dtype=np.float64)

mean_fau, var_fau = stats.mean_var(mtx, axis=axis)
mean_skl, var_skl = mean_variance_axis(mtx, axis)

np.testing.assert_allclose(mean_fau, mean_skl, rtol=1.0e-5, atol=1.0e-8)
np.testing.assert_allclose(var_fau, var_skl, rtol=1.0e-5, atol=1.0e-8)


@pytest.mark.skipif(not find_spec("sklearn"), reason="sklearn not installed")
@pytest.mark.array_type(Flags.Sparse, skip=Flags.Matrix | Flags.Dask | Flags.Disk | Flags.Gpu)
def test_mean_var_sparse_32(array_type: ArrayType[types.CSArray]) -> None:
"""Test whether we are more accurate for 32 bit."""
from sklearn.utils.sparsefuncs import mean_variance_axis

mtx64 = array_type.random((10000, 1000), dtype=np.float64)
mtx32 = mtx64.astype(np.float32)

fau, skl = {}, {}
for n_bit, mtx in [(32, mtx32), (64, mtx64)]:
fau[n_bit] = stats.mean_var(mtx, axis=0)
skl[n_bit] = mean_variance_axis(mtx, 0)

for stat, _ in enumerate(["mean", "var"]):
resid_fau = np.mean(np.abs(fau[64][stat] - fau[32][stat]))
resid_skl = np.mean(np.abs(skl[64][stat] - skl[32][stat]))
assert resid_fau < resid_skl


@pytest.mark.array_type(skip={Flags.Disk, *ATS_CUPY_SPARSE})
@pytest.mark.parametrize(
("axis", "expected"),
Expand Down
13 changes: 13 additions & 0 deletions typings/sklearn/utils/sparsefuncs.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-License-Identifier: MPL-2.0
from typing import Literal

import numpy as np
from numpy.typing import NDArray
from scipy.sparse import csc_array, csc_matrix, csr_array, csr_matrix

def mean_variance_axis(
X: csc_array | csc_matrix | csr_array | csr_matrix, # noqa: N803
axis: Literal[0, 1],
weights: NDArray[np.floating] | None = None,
return_sum_weights: bool = False,
) -> tuple[NDArray[np.float64], NDArray[np.float64]]: ...
Loading