-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Added dot.md #7837
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
Merged
Merged
Added dot.md #7837
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da33ea2
added dot.md
Ananya44444 54ff511
Merge branch 'main' into doc
Ananya44444 dae584a
Enhance documentation for ndarray.dot() method
mamtawardhani 1f3c44f
Update dot.md with return value information
mamtawardhani fcff146
Apply suggestion from @avdhoottt
avdhoottt ae00462
Apply suggestion from @avdhoottt
avdhoottt ed87bc1
Merge branch 'main' into doc
avdhoottt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| --- | ||
| Title: '.dot()' | ||
| Description: 'Computes the dot product of two arrays.' | ||
| Subjects: | ||
| - 'Code Foundations' | ||
| - 'Computer Science' | ||
| Tags: | ||
| - 'Arrays' | ||
| - 'Linear Algebra' | ||
| - 'Methods' | ||
| - 'NumPy' | ||
| CatalogContent: | ||
| - 'learn-python-3' | ||
| - 'paths/computer-science' | ||
| --- | ||
|
|
||
| The **`.dot()`** method computes the dot product of an array with another array or scalar. For one-dimensional arrays, it calculates the standard inner product of vectors. When applied to two-dimensional arrays, it performs matrix multiplication. For arrays with higher dimensions, it executes a sum-product over the last axis of the first array and the second-to-last axis of the second array. | ||
|
|
||
| ## Syntax | ||
|
|
||
| ```pseudo | ||
| ndarray.dot(b, out=None) | ||
| ``` | ||
|
|
||
| **Parameters:** | ||
|
|
||
| - `ndarray`: The first array (A) in the dot product operation (A $\cdot$ B). | ||
| - `b`: The second array (B) or scalar in the dot product operation. | ||
| - `out` (optional): An alternative output array to place the result in. It must have the same shape and buffer length as the expected output, but the type will be cast if necessary. | ||
|
|
||
| ## Example | ||
|
|
||
| This example shows how to use the `.dot()` method for matrix multiplication between two 2D NumPy arrays, `matrix_a` and `matrix_b`: | ||
|
|
||
| ```py | ||
| # Import NumPy | ||
| import numpy as np | ||
|
|
||
| # Create the first 2x3 matrix | ||
| matrix_a = np.array([[1, 2, 3], | ||
| [4, 5, 6]]) | ||
|
|
||
| # Create the second 3x2 matrix | ||
| matrix_b = np.array([[7, 8], | ||
| [9, 10], | ||
| [11, 12]]) | ||
|
|
||
| # Use the '.dot()' method for matrix multiplication (2x3 @ 3x2 = 2x2) | ||
| result_matrix = matrix_a.dot(matrix_b) | ||
|
|
||
| print("Matrix A:") | ||
| print(matrix_a) | ||
| print("\nMatrix B:") | ||
| print(matrix_b) | ||
| print("\nResult (A.dot(B)):") | ||
| print(result_matrix) | ||
| ``` | ||
|
|
||
| We will get the following result: | ||
|
|
||
| ```shell | ||
| Matrix A: | ||
| [[1 2 3] | ||
| [4 5 6]] | ||
|
|
||
| Matrix B: | ||
| [[ 7 8] | ||
| [ 9 10] | ||
| [11 12]] | ||
|
|
||
| Result (A.dot(B)): | ||
| [[ 58 64] | ||
| [139 154]] | ||
| ``` | ||
|
|
||
| ## Codebyte Example | ||
|
|
||
| In the following codebyte example, the `.dot()` method is used to calculate the inner product (dot product) of two one-dimensional vectors, `vector_x` and `vector_y`: | ||
|
|
||
| ```codebyte/python | ||
| import numpy as np | ||
|
|
||
| # Create two 1-D arrays (vectors) | ||
| vector_x = np.array([1, 2, 3]) | ||
| vector_y = np.array([5, 6, 7]) | ||
|
|
||
| # Calculate the inner product (dot product) | ||
| dot_product = vector_x.dot(vector_y) | ||
|
|
||
| print(f"Vector x: {vector_x}") | ||
| print(f"Vector y: {vector_y}") | ||
| print(f"Dot product (x.dot(y)): {dot_product}") | ||
| ``` | ||
|
|
||
| Here's the explanation: | ||
avdhoottt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| $$\vec{x} \cdot \vec{y} = (1 \times 5) + (2 \times 6) + (3 \times 7) = 5 + 12 + 21 = 38$$ | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.