MCPcopy Create free account
hub / github.com/dask/dask / svd_flip

Function svd_flip

dask/array/utils.py:544–583  ·  view source on GitHub ↗

Sign correction to ensure deterministic output from SVD. This function is useful for orienting eigenvectors such that they all lie in a shared but arbitrary half-space. This makes it possible to ensure that results are equivalent across SVD implementations and random number generato

(u, v, u_based_decision=False)

Source from the content-addressed store, hash-verified

542
543
544def svd_flip(u, v, u_based_decision=False):
545 """Sign correction to ensure deterministic output from SVD.
546
547 This function is useful for orienting eigenvectors such that
548 they all lie in a shared but arbitrary half-space. This makes
549 it possible to ensure that results are equivalent across SVD
550 implementations and random number generator states.
551
552 Parameters
553 ----------
554
555 u : (M, K) array_like
556 Left singular vectors (in columns)
557 v : (K, N) array_like
558 Right singular vectors (in rows)
559 u_based_decision: bool
560 Whether or not to choose signs based
561 on `u` rather than `v`, by default False
562
563 Returns
564 -------
565
566 u : (M, K) array_like
567 Left singular vectors with corrected sign
568 v: (K, N) array_like
569 Right singular vectors with corrected sign
570 """
571 # Determine half-space in which all singular vectors
572 # lie relative to an arbitrary vector; summation
573 # equivalent to dot product with row vector of ones
574 if u_based_decision:
575 dtype = u.dtype
576 signs = np.sum(u, axis=0, keepdims=True)
577 else:
578 dtype = v.dtype
579 signs = np.sum(v, axis=1, keepdims=True).T
580 signs = 2.0 * ((signs >= 0) - 0.5).astype(dtype)
581 # Force all singular vectors into same half-space
582 u, v = u * signs, v * signs.T
583 return u, v
584
585
586def scipy_linalg_safe(func_name, *args, **kwargs):

Callers 6

svd_compressedFunction · 0.90
svdFunction · 0.90
test_svd_flip_correctionFunction · 0.90
test_svd_flip_signFunction · 0.90

Calls 2

sumMethod · 0.45
astypeMethod · 0.45

Tested by 4

test_svd_flip_correctionFunction · 0.72
test_svd_flip_signFunction · 0.72