General broadcasting rule
Two dimensions are compatible when
1. they are equal, or
2. one of them is 1.
No. | Example Code | |
1 | import numpy as np a = np.array( [1.0, 2.0, 3.0] ) b = 2.0 a * b |
an array and a scalar value are combined in an operation: |
2 |
import numpy as np a = np.array( [ [ 0.0, 0.0, 0.0], [10.0, 10.0, 10.0], [20.0, 20.0, 20.0], [30.0, 30.0, 30.0] ] ) b = np.array([1.0, 2.0, 3.0]) a + b b = np.array([1.0, 2.0, 3.0, 4.0]) a + b |
|
3 | import numpy as np a = np.array( [ [ 0.0, 0.0, 0.0], [10.0, 10.0, 10.0], [20.0, 20.0, 20.0], [30.0, 30.0, 30.0] ] ) b = np.array( [1.0, 2.0, 3.0, 4.0] ) a + b |
|
4 | import numpy as np a = np.array( [0.0, 10.0, 20.0, 30.0] ) b = np.array( [1.0, 2.0, 3.0] ) a[ :, np.newaxis ] + b |
|
Source: https://numpy.org/doc/stable/user/basics.broadcasting.html
'Python' 카테고리의 다른 글
Python_EX-003. how to extract a condition dataset (0) | 2025.01.21 |
---|---|
Python_N-006. Array Slicing (0) | 2025.01.20 |
Python_EX-002. Average Score Comparison (NumPy Practice) (0) | 2025.01.20 |
Python_N-005. 10 basic attributes in array (2) (0) | 2025.01.19 |
Python_N-004. 10 basic attributes in array (0) | 2025.01.19 |