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

Function svd_flip

dask/array/utils.py:529–568  ·  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

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

Callers 5

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 3

test_svd_flip_correctionFunction · 0.72
test_svd_flip_signFunction · 0.72