|
| 1 | +--- |
| 2 | +Title: '.dot()' |
| 3 | +Description: 'Computes the dot product of two arrays.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'Linear Algebra' |
| 10 | + - 'Methods' |
| 11 | + - 'NumPy' |
| 12 | +CatalogContent: |
| 13 | + - 'learn-python-3' |
| 14 | + - 'paths/computer-science' |
| 15 | +--- |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | +## Syntax |
| 20 | + |
| 21 | +```pseudo |
| 22 | +ndarray.dot(b, out=None) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | + |
| 27 | +- `ndarray`: The first array (A) in the dot product operation (A $\cdot$ B). |
| 28 | +- `b`: The second array (B) or scalar in the dot product operation. |
| 29 | +- `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. |
| 30 | + |
| 31 | +**Return value:** |
| 32 | + |
| 33 | +Returns the dot product as a scalar, 2-D array, or ndarray, depending on the input dimensions. |
| 34 | + |
| 35 | +## Example |
| 36 | + |
| 37 | +This example shows how to use the `.dot()` method for matrix multiplication between two 2D NumPy arrays, `matrix_a` and `matrix_b`: |
| 38 | + |
| 39 | +```py |
| 40 | +# Import NumPy |
| 41 | +import numpy as np |
| 42 | + |
| 43 | +# Create the first 2x3 matrix |
| 44 | +matrix_a = np.array([[1, 2, 3], |
| 45 | + [4, 5, 6]]) |
| 46 | + |
| 47 | +# Create the second 3x2 matrix |
| 48 | +matrix_b = np.array([[7, 8], |
| 49 | + [9, 10], |
| 50 | + [11, 12]]) |
| 51 | + |
| 52 | +# Use the '.dot()' method for matrix multiplication (2x3 @ 3x2 = 2x2) |
| 53 | +result_matrix = matrix_a.dot(matrix_b) |
| 54 | + |
| 55 | +print("Matrix A:") |
| 56 | +print(matrix_a) |
| 57 | +print("\nMatrix B:") |
| 58 | +print(matrix_b) |
| 59 | +print("\nResult (A.dot(B)):") |
| 60 | +print(result_matrix) |
| 61 | +``` |
| 62 | + |
| 63 | +The output of the above code will be: |
| 64 | + |
| 65 | +```shell |
| 66 | +Matrix A: |
| 67 | +[[1 2 3] |
| 68 | + [4 5 6]] |
| 69 | + |
| 70 | +Matrix B: |
| 71 | +[[ 7 8] |
| 72 | + [ 9 10] |
| 73 | + [11 12]] |
| 74 | + |
| 75 | +Result (A.dot(B)): |
| 76 | +[[ 58 64] |
| 77 | + [139 154]] |
| 78 | +``` |
| 79 | + |
| 80 | +## Codebyte Example |
| 81 | + |
| 82 | +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`: |
| 83 | + |
| 84 | +```codebyte/python |
| 85 | +import numpy as np |
| 86 | +
|
| 87 | +# Create two 1-D arrays (vectors) |
| 88 | +vector_x = np.array([1, 2, 3]) |
| 89 | +vector_y = np.array([5, 6, 7]) |
| 90 | +
|
| 91 | +# Calculate the inner product (dot product) |
| 92 | +dot_product = vector_x.dot(vector_y) |
| 93 | +
|
| 94 | +print(f"Vector x: {vector_x}") |
| 95 | +print(f"Vector y: {vector_y}") |
| 96 | +print(f"Dot product (x.dot(y)): {dot_product}") |
| 97 | +``` |
| 98 | + |
| 99 | +Calculation breakdown: |
| 100 | + |
| 101 | +$$\vec{x} \cdot \vec{y} = (1 \times 5) + (2 \times 6) + (3 \times 7) = 5 + 12 + 21 = 38$$ |
0 commit comments