From 4a9138e6422e3e85c60da424884b2eb13c791979 Mon Sep 17 00:00:00 2001 From: awinml <97467100+awinml@users.noreply.github.com> Date: Sat, 8 Oct 2022 12:56:05 +0530 Subject: [PATCH 1/3] Ensures that sklearn.utils.extmath.fast_logdet passes numpydoc validation --- sklearn/tests/test_docstrings.py | 1 - sklearn/utils/extmath.py | 52 +++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/sklearn/tests/test_docstrings.py b/sklearn/tests/test_docstrings.py index 591107502c296..bf660de17c659 100644 --- a/sklearn/tests/test_docstrings.py +++ b/sklearn/tests/test_docstrings.py @@ -12,7 +12,6 @@ numpydoc_validation = pytest.importorskip("numpydoc.validate") FUNCTION_DOCSTRING_IGNORE_LIST = [ - "sklearn.utils.extmath.fast_logdet", "sklearn.utils.extmath.randomized_svd", "sklearn.utils.extmath.svd_flip", "sklearn.utils.gen_batches", diff --git a/sklearn/utils/extmath.py b/sklearn/utils/extmath.py index 4afa3a5540f98..223ea7ef3002e 100644 --- a/sklearn/utils/extmath.py +++ b/sklearn/utils/extmath.py @@ -82,15 +82,57 @@ def row_norms(X, squared=False): def fast_logdet(A): - """Compute log(det(A)) for A symmetric. + """Compute logarithm of determinant of a square matrix. - Equivalent to : np.log(nl.det(A)) but more robust. - It returns -Inf if det(A) is non positive or is not defined. + The (natural) logarithm of the determinant of a square matrix + is returned if det(A) is non-negative and well defined. + If the determinant is zero or negative returns -Inf. + + Log Det = log(det(A)) for A, where A is square matrix. + + Equivalent to : np.log(np.det(A)) but more robust. Parameters ---------- - A : array-like - The matrix. + A : array_like of shape (n, n) + Input array, has to be a square 2-D array. + The square matrix whose (natural) logarithm of the determinant + needs to be found. + + Returns + ------- + logdet : float + When det(A) is non-negative (natural) logarithm of the determinant + is returned. When the determinant is non-positive or not defined, then -inf + is returned. + + See Also + -------- + numpy.linalg.slogdet : Compute the sign and (natural) logarithm of the determinant + of an array. + + Examples + -------- + There can be three cases: + Case-1: Matrix with a Positive determinant (Value of the logarithm of the + determinant is returned) + + >>> from sklearn.utils.extmath import fast_logdet + >>> a = np.array([[5, 1], [2, 8]]) + >>> fast_logdet(a) + 3.6375861597263857 + + Case-2: Matrix with a determinant equal to zero (-inf is returned) + + >>> b = np.array([[1, 2], [1, 2]]) + >>> fast_logdet(b) + -inf + + Case-3: Matrix with a Negative determinant (-inf is returned) + + >>> c = np.array([[1, 2], [3, 4]]) + >>> fast_logdet(c) + -inf """ sign, ld = np.linalg.slogdet(A) if not sign > 0: From 099a3f7e30ccc8e955f522f6b68962f0e35e02b5 Mon Sep 17 00:00:00 2001 From: Ashwin Mathur <97467100+awinml@users.noreply.github.com> Date: Sat, 8 Oct 2022 20:33:12 +0530 Subject: [PATCH 2/3] Update extmath.py --- sklearn/utils/extmath.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sklearn/utils/extmath.py b/sklearn/utils/extmath.py index 223ea7ef3002e..2d250e2d2a0af 100644 --- a/sklearn/utils/extmath.py +++ b/sklearn/utils/extmath.py @@ -117,6 +117,7 @@ def fast_logdet(A): Case-1: Matrix with a Positive determinant (Value of the logarithm of the determinant is returned) + >>> import numpy as np >>> from sklearn.utils.extmath import fast_logdet >>> a = np.array([[5, 1], [2, 8]]) >>> fast_logdet(a) From 2a113a7028520beba088c08dd3350f17da509323 Mon Sep 17 00:00:00 2001 From: jeremie du boisberranger Date: Fri, 14 Oct 2022 11:54:21 +0200 Subject: [PATCH 3/3] simplify some parts, example in particular --- sklearn/utils/extmath.py | 27 +++------------------------ 1 file changed, 3 insertions(+), 24 deletions(-) diff --git a/sklearn/utils/extmath.py b/sklearn/utils/extmath.py index 09913b96e4e11..675c51844e01f 100644 --- a/sklearn/utils/extmath.py +++ b/sklearn/utils/extmath.py @@ -88,23 +88,18 @@ def fast_logdet(A): is returned if det(A) is non-negative and well defined. If the determinant is zero or negative returns -Inf. - Log Det = log(det(A)) for A, where A is square matrix. - Equivalent to : np.log(np.det(A)) but more robust. Parameters ---------- A : array_like of shape (n, n) - Input array, has to be a square 2-D array. - The square matrix whose (natural) logarithm of the determinant - needs to be found. + The square matrix. Returns ------- logdet : float - When det(A) is non-negative (natural) logarithm of the determinant - is returned. When the determinant is non-positive or not defined, then -inf - is returned. + When det(A) is strictly positive, log(det(A)) is returned. + When det(A) is non-positive or not defined, then -inf is returned. See Also -------- @@ -113,27 +108,11 @@ def fast_logdet(A): Examples -------- - There can be three cases: - Case-1: Matrix with a Positive determinant (Value of the logarithm of the - determinant is returned) - >>> import numpy as np >>> from sklearn.utils.extmath import fast_logdet >>> a = np.array([[5, 1], [2, 8]]) >>> fast_logdet(a) 3.6375861597263857 - - Case-2: Matrix with a determinant equal to zero (-inf is returned) - - >>> b = np.array([[1, 2], [1, 2]]) - >>> fast_logdet(b) - -inf - - Case-3: Matrix with a Negative determinant (-inf is returned) - - >>> c = np.array([[1, 2], [3, 4]]) - >>> fast_logdet(c) - -inf """ sign, ld = np.linalg.slogdet(A) if not sign > 0: