Skip to content
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

New term entry on the built in function for Numpy named 'svd' #4547

Merged
merged 16 commits into from
May 26, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
93 changes: 93 additions & 0 deletions content/numpy/concepts/built-in-functions/terms/svd/svd.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
---
Title: '.svd()'
Description: 'Singular Value Decomposition'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Description: 'Singular Value Decomposition'
Description: 'The `.svd()` built-in function performs the Singular Value Decomposition (SVD) on a matrix, breaking it down into singular vectors and values.'

Subjects:
- 'Computer Science'
- 'Data Science'
- 'Machine Learning'
Tags:
- 'Linear Algebra'
- 'Machine Learning'
- 'NumPy'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
- 'paths/data-science'
- 'paths/data-science-foundaions'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- 'paths/data-science-foundaions'
- 'paths/data-science-foundations'

---

The **`.svd()`** is a mathematical technique that decomposes a matrix into three simpler matrices.Factorizes the matrix `a` into two unitary matrices `U` and `Vh`,
along with a 1-D array `s` of singular values (real and non-negative), such that `a` equals `U @ S @ Vh`, where `S` is a suitably shaped matrix of zeros with `s` as
its main diagonal.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The **`.svd()`** is a mathematical technique that decomposes a matrix into three simpler matrices.Factorizes the matrix `a` into two unitary matrices `U` and `Vh`,
along with a 1-D array `s` of singular values (real and non-negative), such that `a` equals `U @ S @ Vh`, where `S` is a suitably shaped matrix of zeros with `s` as
its main diagonal.
The **`.svd()`** function is a mathematical technique that decomposes a matrix into three simpler matrices: it factorizes the matrix `a` into two unitary matrices `U` and `Vh`, along with a 1-D array `s` of singular values (real and non-negative). This decomposition satisfies the equation `a = U @ S @ Vh`, where `S` is a suitably shaped matrix of zeros with `s` as its main diagonal.



Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the extra one line spacing here.

## Syntax
mamtawardhani marked this conversation as resolved.
Show resolved Hide resolved

```pseudo
numpy.linalg.svd(a, full_matrices=True, compute_uv=True, hermitian=False)
```

`.svd()` provides the following arguments:
mamtawardhani marked this conversation as resolved.
Show resolved Hide resolved

- `a`: This is the input matrix to be decomposed with a.ndim >= 2. It is the matrix you want to perform Singular Value Decomposition on.
- `full_matrices`:This parameter determines whether to compute the full-sized or reduced-sized matrices U and VT. If full_matrices is set to True, the function
computes the full-sized matrices. If set to False, it computes only the essential parts of U and VT. By default, it's set to True.
- `compute_uv`: This parameter specifies whether to compute the left-singular vectors (U) and right-singular vectors (transpose of V) in addition to the singular
values. If compute_uv is set to True, the function computes U and VT. If set to False, it only computes the singular values. By default, it's set to True.
- `hermitian':This parameter indicates whether the input matrix a is Hermitian (equal to its conjugate transpose). If hermitian is set to True, the function assumes
that a is Hermitian and uses a more efficient algorithm tailored for Hermitian matrices. If set to False, it uses a general algorithm. By default, it's set to
False.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `a`: This is the input matrix to be decomposed with a.ndim >= 2. It is the matrix you want to perform Singular Value Decomposition on.
- `full_matrices`:This parameter determines whether to compute the full-sized or reduced-sized matrices U and VT. If full_matrices is set to True, the function
computes the full-sized matrices. If set to False, it computes only the essential parts of U and VT. By default, it's set to True.
- `compute_uv`: This parameter specifies whether to compute the left-singular vectors (U) and right-singular vectors (transpose of V) in addition to the singular
values. If compute_uv is set to True, the function computes U and VT. If set to False, it only computes the singular values. By default, it's set to True.
- `hermitian':This parameter indicates whether the input matrix a is Hermitian (equal to its conjugate transpose). If hermitian is set to True, the function assumes
that a is Hermitian and uses a more efficient algorithm tailored for Hermitian matrices. If set to False, it uses a general algorithm. By default, it's set to
False.
- `a`: This parameter represents the input matrix to be decomposed, where `a.ndim>=2`. It is the matrix on which Singular Value Decomposition will be performed.
- `full_matrices`: This parameter determines whether the function computes full-sized or reduced-sized matrices `U` and `Vh`. If `full_matrices` is set to `True`, the function computes the full-sized matrices. If set to `False`, it computes only the essential parts of `U` and `Vh`. The default value is `True`.
- `compute_uv`: This parameter specifies whether the function computes the left-singular vectors (`U`) and right-singular vectors (transpose of `V`) in addition to the singular values. When `compute_uv` is set to `True`, the function computes `U` and `Vh`. If set to `False`, it only computes the singular values. The default value is `True`.
- `hermitian': This parameter indicates whether the input matrix `a` is Hermitian, meaning it is equal to its conjugate transpose. When `hermitian` is set to `True`, the function assumes that a is Hermitian and uses a more efficient algorithm tailored for such matrices. If set to `False`, it uses a general algorithm. The default value is `False`.


## Example
The following example demonstrates various scenarios of Singular Value Decomposition (SVD) and uses `.svd()` using NumPy:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The following example demonstrates various scenarios of Singular Value Decomposition (SVD) and uses `.svd()` using NumPy:
The following example demonstrates various scenarios of Singular Value Decomposition (SVD) and uses `.svd()` using `NumPy`:



```py
# The following example demonstrates various scenarios of Singular Value Decomposition (SVD) using NumPy:

a = np.random.randn(9, 6) + 1j*np.random.randn(9, 6)
b = np.random.randn(2, 7, 8, 3) + 1j*np.random.randn(2, 7, 8, 3)

## Reconstruction based on full SVD, 2D case:

U, S, Vh = np.linalg.svd(a, full_matrices=True)
U.shape, S.shape, Vh.shape
((9, 9), (6,), (6, 6))
np.allclose(a, np.dot(U[:, :6] * S, Vh))
True
smat = np.zeros((9, 6), dtype=complex)
smat[:6, :6] = np.diag(S)
np.allclose(a, np.dot(U, np.dot(smat, Vh)))
True

## Reconstruction based on reduced SVD, 2D case:

U, S, Vh = np.linalg.svd(a, full_matrices=False)
U.shape, S.shape, Vh.shape
((9, 6), (6,), (6, 6))
np.allclose(a, np.dot(U * S, Vh))
True
smat = np.diag(S)
np.allclose(a, np.dot(U, np.dot(smat, Vh)))
True

## Reconstruction based on full SVD, 4D case:

U, S, Vh = np.linalg.svd(b, full_matrices=True)
U.shape, S.shape, Vh.shape
((2, 7, 8, 8), (2, 7, 3), (2, 7, 3, 3))
np.allclose(b, np.matmul(U[..., :3] * S[..., None, :], Vh))
True
np.allclose(b, np.matmul(U[..., :3], S[..., None] * Vh))
True

## Reconstruction based on reduced SVD, 4D case:

U, S, Vh = np.linalg.svd(b, full_matrices=False)
U.shape, S.shape, Vh.shape
((2, 7, 8, 3), (2, 7, 3), (2, 7, 3, 3))
np.allclose(b, np.matmul(U * S[..., None, :], Vh))
True
np.allclose(b, np.matmul(U, S[..., None] * Vh))
True
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please modify or replace this example with a new one, this is plagiarized.
Plagiarized from: https://numpy.org/doc/stable/reference/generated/numpy.linalg.svd.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment. To clarify, I specifically mentioned the source of the examples in the commit comment to avoid plagiarism. However, I wasn't aware that it could still be considered plagiarism even when the resource is provided. Thanks for correcting me!

```
mamtawardhani marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a codebyte example below. For more information on how to write it, visit here.