[파이썬][numpy] numpy `dot` 함수

In this blog post, we will discuss the dot function of the NumPy library in Python. The dot function is one of the most commonly used functions in NumPy, and it is used for matrix multiplication.

The dot function

The dot function in NumPy is used to perform matrix multiplication between two arrays. It takes two arrays as input and returns their matrix product. The shape of the output array depends on the shape of the input arrays.

The syntax for the dot function is as follows:

numpy.dot(a, b, out=None)

where a and b are the input arrays, and out is an optional parameter to specify the output array. If out is not provided, a new array is created to store the result.

Example usage

Let’s see some examples of how the dot function can be used.

import numpy as np

# Example 1
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
result = np.dot(a, b)
print(result)
# Output: [[19 22]
#          [43 50]]

# Example 2
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[7, 8], [9, 10], [11, 12]])
result = np.dot(c, d)
print(result)
# Output: [[ 58  64]
#          [139 154]]

# Example 3
e = np.array([1, 2, 3])
f = np.array([4, 5, 6])
result = np.dot(e, f)
print(result)
# Output: 32

As you can see from the examples, the dot function can be used with both 2D arrays and 1D arrays. It calculates the matrix product of the input arrays according to the matrix multiplication rules.

Conclusion

The dot function in NumPy is a useful tool for performing matrix multiplication. It allows you to efficiently calculate the dot product of arrays, whether they are 1D or 2D. Understanding how to use the dot function is essential for working with arrays and performing mathematical operations in NumPy.

I hope this blog post was helpful in understanding the dot function in NumPy. Stay tuned for more articles on Python and NumPy. Happy coding!