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
base: main
Are you sure you want to change the base?
Conversation
… Value Decomposition) under svd.md
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. |
@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. |
Thanks for clarification, I found it easier to sign the CLA with other account as well, since I have no idea how I submitted with two accounts. I hope now the matter is addressed.
…________________________________
From: mamtawardhani ***@***.***>
Sent: Saturday, April 20, 2024 8:44
To: Codecademy/docs ***@***.***>
Cc: Doroodian, M. (Mostafa) ***@***.***>; Mention ***@***.***>
Subject: Re: [Codecademy/docs] New term entry on the built in function for Numpy named 'svd' (PR #4547)
@doroodianMostafa<https://github.com/doroodianMostafa> it appears that you've submitted this PR using two accounts (The other one is @doroodian<https://github.com/doroodian> ). Therefore, please sign this CLA with both accounts or close this and resubmit a new PR using only one account.
—
Reply to this email directly, view it on GitHub<#4547 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AQ3XQOCWXP76KFJ7NVWRM6LY6IFERAVCNFSM6AAAAABGOY52GWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANRXGU3TQOBUGA>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
There was a problem hiding this 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' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.' |
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. |
|
||
|
There was a problem hiding this comment.
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.
- `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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- `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`. | |
# 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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!
There was a problem hiding this 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`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- `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: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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`: |
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 |
There was a problem hiding this comment.
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://python.quantecon.org/svd_intro.html
Please modify the example so that it fits your description.
Thanks!
There was a problem hiding this comment.
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.
There was a problem hiding this 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.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.' |
There was a problem hiding this 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
Hi @letsbuildanything, Thanks for taking responsibility for the second review. |
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
Checklist
main
branch.Issues Solved
section.