Skip to content

Fix svd flip #595

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 8 commits into from
Dec 30, 2019
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
12 changes: 11 additions & 1 deletion dask_ml/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@
logger = logging.getLogger()


def _svd_flip_copy(x, y):
# If the array is locked, copy the array and transpose it
# This happens with a very large array > 1TB
# GH: issue 592
try:
return skm.svd_flip(x, y)
except ValueError:
return skm.svd_flip(x.copy(), y.copy())


def svd_flip(u, v):
u2, v2 = delayed(skm.svd_flip, nout=2)(u, v)
u2, v2 = delayed(_svd_flip_copy, nout=2)(u, v)
u = da.from_delayed(u2, shape=u.shape, dtype=u.dtype)
v = da.from_delayed(v2, shape=v.shape, dtype=v.dtype)
return u, v
Expand Down
1 change: 1 addition & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Version 1.1.1
~~~~~~~~~~~~~

- Fixed an issue with the 1.1.0 wheel (:issue:`575`)
- Make svd_flip work even when arrays are read only (:issue:`592`)

Version 1.1.0
~~~~~~~~~~~~~
Expand Down
21 changes: 20 additions & 1 deletion tests/test_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import dask_ml.decomposition as dd
from dask_ml.decomposition._compat import _assess_dimension_, _infer_dimension_
from dask_ml.utils import assert_estimator_equal
from dask_ml.utils import assert_estimator_equal, svd_flip

with warnings.catch_warnings():
warnings.simplefilter("ignore", FutureWarning)
Expand Down Expand Up @@ -782,3 +782,22 @@ def test_pca_sklearn_inputs(input_type, solver):
a.fit(Y)
with pytest.raises(TypeError, match="unsupported type"):
a.fit_transform(Y)


def test_svd_flip():
rng = np.random.RandomState(0)
u = rng.randn(8, 3)
v = rng.randn(3, 10)
u = da.from_array(u, chunks=(-1, -1))
v = da.from_array(v, chunks=(-1, -1))
u2, v2 = svd_flip(u, v)

def set_readonly(x):
x.setflags(write=False)
return x

u = u.map_blocks(set_readonly)
v = v.map_blocks(set_readonly)
u, v = svd_flip(u, v)
assert_eq(u, u2)
assert_eq(v, v2)