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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

doroodianMostafa
Copy link

@doroodianMostafa doroodianMostafa commented Apr 19, 2024

new term entry on the built in function for Numpy named SVD(Singular Value Decomposition) under svd.md

Description

This is a new term entry on the built-in-functions term for [Numpy]: Singular Value Decomposition. The is in a new file under docs/content/numpy/concepts/built-in-functions/terms/svd/svd.md

I'd like to acknowledge that the following link served as my source of motivation or inspiration for the provided example: numpy.linalg.svd() documentation.

Issue Solved

Closes #4479

Type of Change

  • Adding a new entry

Checklist

  • All writings are my own.
  • My entry follows the Codecademy Docs style guide.
  • My changes generate no new warnings.
  • I have performed a self-review of my own writing and code.
  • I have checked my entry and corrected any misspellings.
  • I have made corresponding changes to the documentation if needed.
  • I have confirmed my changes are not being pushed from my forked main branch.
  • I have confirmed that I'm pushing from a new branch named after the changes I'm making.
  • I have linked any issues that are relevant to this PR in the Issues Solved section.

@CLAassistant
Copy link

CLAassistant commented Apr 19, 2024

CLA assistant check
All committers have signed the CLA.

@doroodianMostafa
Copy link
Author

Thank you for your comment, I tried to redo it through the provided link, I hope this time it's addressed. Otherwise I do appreciate further instructions.

Besides I do not understand why it's mentioned that there are two committers, since my account is doroodianMostafa.

@mamtawardhani
Copy link
Collaborator

@doroodianMostafa it appears that you've submitted this PR using two accounts (The other one is @doroodian ). Therefore, please sign this CLA with both accounts or close this and resubmit a new PR using only one account.

@doroodianMostafa
Copy link
Author

doroodianMostafa commented Apr 20, 2024 via email

@mamtawardhani mamtawardhani self-assigned this Apr 20, 2024
@mamtawardhani mamtawardhani added new entry New entry or entries numpy NumPy entries status: under review Issue or PR is currently being reviewed labels Apr 20, 2024
Copy link
Collaborator

@mamtawardhani mamtawardhani left a comment

Choose a reason for hiding this comment

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

Hey @doroodianMostafa
Thank you for contributing at Codecademy!

I've suggested some changes in the entry, please review and modify them at your earliest convenience. Also, please change or replace the example given with a new one, since this one is plagiarized.

Thanks!

@@ -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.'

Comment on lines 19 to 21
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.

Comment on lines 22 to 23


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.

Comment on lines 32 to 39
- `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`.

Comment on lines 46 to 92
# 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
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!

Copy link
Collaborator

@mamtawardhani mamtawardhani left a comment

Choose a reason for hiding this comment

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

Hey @doroodianMostafa! Please review the changes suggested and modify them. Thanks!

- `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`.
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
- `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`.
- `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`.

- `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`:

Comment on lines 38 to 81
import numpy as np

# Example 1: Full SVD, 2D Case
A = np.random.randn(5, 3)
U, S, Vt = np.linalg.svd(A, full_matrices=True)
U.shape, S.shape, Vt.shape
((5, 5), (3,), (3, 3))
np.allclose(A, np.dot(U[:, :3] * S, Vt))
True
Sigma = np.zeros((5, 3))
Sigma[:3, :3] = np.diag(S)
np.allclose(A, np.dot(U, np.dot(Sigma, Vt)))
True

# Example 2: Reduced SVD, 2D Case
B = np.random.randn(4, 6)
U, S, Vt = np.linalg.svd(B, full_matrices=False)
U.shape, S.shape, Vt.shape
((4, 4), (4,), (4, 6))
np.allclose(B, np.dot(U * S, Vt))
True
Sigma = np.diag(S)
np.allclose(B, np.dot(U, np.dot(Sigma, Vt)))
True

# Example 3: Full SVD, 3D Case
C = np.random.randn(3, 4, 5)
U, S, Vt = np.linalg.svd(C, full_matrices=True)
U.shape, S.shape, Vt.shape
((3, 3, 3), (3,), (3, 4, 5))
np.allclose(C, np.matmul(U[..., :3] * S[..., None, :], Vt))
True
np.allclose(C, np.matmul(U, np.matmul(np.diag(S), Vt)))
True

# Example 4: Reduced SVD, 3D Case
D = np.random.randn(2, 2, 3)
U, S, Vt = np.linalg.svd(D, full_matrices=False)
U.shape, S.shape, Vt.shape
((2, 2, 2), (2,), (2, 2, 3))
np.allclose(D, np.matmul(U * S[..., None, :], Vt))
True
np.allclose(D, np.matmul(U, np.matmul(np.diag(S), Vt)))
True
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hey @doroodianMostafa, based on the entry you made, the .svd() computes the singular value decomposition and produces the U', Vh, and s` matrices, the example should be something similar to that. Please refer to the following links for reference:

https://www.geeksforgeeks.org/compute-the-factor-of-a-given-array-by-singular-value-decomposition-using-numpy/

https://python.quantecon.org/svd_intro.html

Please modify the example so that it fits your description.
Thanks!

Copy link
Author

Choose a reason for hiding this comment

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

Hi @mamtawardhani,

Thanks for the feedback! I took a look at the links and tried to create a similar, but simpler example. To keep things clear, I skipped the 3D matrix example and left the other input parameters at their defaults.

Copy link
Collaborator

@mamtawardhani mamtawardhani left a comment

Choose a reason for hiding this comment

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

Hey @doroodianMostafa! Thank you for changing the example!

Just a few more suggestions are added (like leaving a line after every heading, and, adding the output block, etc); please review them and merge those suggestions at your earliest convenience.
Thank you!

@@ -0,0 +1,51 @@
---
Title: '.svd()'
Description: 'The `.svd()` built-in function performs the Singular Value Decomposition (SVD) on a matrix, breaking it down into singular vectors and values.'
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: 'The `.svd()` built-in function performs the Singular Value Decomposition (SVD) on a matrix, breaking it down into singular vectors and values.'
Description: 'Performs the Singular Value Decomposition (SVD) on a matrix, breaking it down into singular vectors and values.'

Copy link
Collaborator

@mamtawardhani mamtawardhani left a comment

Choose a reason for hiding this comment

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

Good for the next review

@doroodianMostafa
Copy link
Author

Hi @letsbuildanything, Thanks for taking responsibility for the second review.
Looking forward to hear your feedbacks.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new entry New entry or entries numpy NumPy entries status: review 1️⃣ completed status: under review Issue or PR is currently being reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Term Entry] Python:NumPy built-in-functions Singular Value Decomposition
5 participants