TruncatedSVD svd_flip creates single-chunked array
- Dominant language
- Python
- Stars
- 951
- Forks
- 262
- PR merge metrics
- No merged PRs in 30d
Description
Currently we use this function on the outputs of svd
```python
def svd_flip(u, v):
u2, v2 = delayed(skm.svd_flip, 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
```
This forces potentially large (I think?) multi-chunked arrays into a single chunk
I wonder if the actual sklearn function might work on its own. It doesn't appear to use any functionality outside of dask.array (assuming that functions like np.sign and np.argmax function as ufuncs)
```python
def svd_flip(u, v, u_based_decision=True):
"""Sign correction to ensure deterministic output from SVD.
Adjusts the columns of u and the rows of v such that the loadings in the
columns in u that are largest in absolute value are always positive.
Parameters
----------
u : ndarray
u and v are the output of `linalg.svd` or
`sklearn.utils.extmath.randomized_svd`, with matching inner dimensions
so one can compute `np.dot(u * s, v)`.
v : ndarray
u and v are the output of `linalg.svd` or
`sklearn.utils.extmath.randomized_svd`, with matching inner dimensions
so one can compute `np.dot(u * s, v)`.
u_based_decision : boolean, (default=True)
If True, use the columns of u as the basis for sign flipping.
Otherwise, use the rows of v. The choice of which variable to base the
decision on is generally algorithm dependent.
Returns
-------
u_adjusted, v_adjusted : arrays with the same dimensions as the input.
"""
if u_based_decision:
# columns of u, rows of v
max_abs_cols = np.argmax(np.abs(u), axis=0)
signs = np.sign(u[max_abs_cols, xrange(u.shape[1])])
u *= signs
v *= signs[:, np.newaxis]
else:
# rows of v, columns of u
max_abs_rows = np.argmax(np.abs(v), axis=1)
signs = np.sign(v[xrange(v.shape[0]), max_abs_rows])
u *= signs
v *= signs[:, np.newaxis]
return u, v
```
Or maybe this is unnecessary?
Contributor guide
Assessment
This issue has not been assessed yet.