diff --git a/python/cuml/cuml/decomposition/incremental_pca.py b/python/cuml/cuml/decomposition/incremental_pca.py index 72c3b4d55b..b89a1e73ca 100644 --- a/python/cuml/cuml/decomposition/incremental_pca.py +++ b/python/cuml/cuml/decomposition/incremental_pca.py @@ -428,11 +428,14 @@ def transform(self, X, *, convert_dtype=False) -> CumlArray: convert_dtype=convert_dtype, ) - n_samples = X.shape[0] + n_samples, n_features = X.shape + batch_size = getattr( + self, "batch_size_", self.batch_size or 5 * n_features + ) output = [] for batch in _gen_batches( n_samples, - self.batch_size_, + batch_size, min_batch_size=self.n_components or 0, ): output.append(self._transform_sparse(X[batch])) diff --git a/python/cuml/tests/test_incremental_pca.py b/python/cuml/tests/test_incremental_pca.py index f7ef3b3f31..7871cc2198 100644 --- a/python/cuml/tests/test_incremental_pca.py +++ b/python/cuml/tests/test_incremental_pca.py @@ -133,6 +133,21 @@ def test_exceptions(): cuIPCA(n_components=8).fit(X[:, :5]) +@pytest.mark.parametrize("batch_size", [None, 50]) +def test_partial_fit_then_sparse_transform(batch_size): + X_dense, _ = make_blobs( + n_samples=200, n_features=10, random_state=0, dtype="float64" + ) + X_sparse = cupyx.scipy.sparse.csr_matrix(X_dense) + + ipca = cuIPCA(n_components=4, batch_size=batch_size) + for i in range(0, 200, 50): + ipca.partial_fit(X_dense[i : i + 50]) + + result = ipca.transform(X_sparse) + assert result.shape == (200, 4) + + def test_svd_flip(): x = cp.array(range(-10, 80)).reshape((9, 10)) u, s, v = cp.linalg.svd(x, full_matrices=False)