#numpy
NumPy
Broadcasting
Broadcasting describes how NumPy automatically brings two arrays with different shapes to a compatible shape during arithmetic operations. Generally, the smaller array is “repeated” multiple times until both arrays have the same shape. Broadcasting is memory-efficient as it doesn’t actually copy the smaller array multiple times.
Code:
# [3 6 9]
Share and Support
@Python_Codes
NumPy
Broadcasting
Broadcasting describes how NumPy automatically brings two arrays with different shapes to a compatible shape during arithmetic operations. Generally, the smaller array is “repeated” multiple times until both arrays have the same shape. Broadcasting is memory-efficient as it doesn’t actually copy the smaller array multiple times.
Code:
import numpy as npOutput:
A = np.array([1, 2, 3])
res = A * 3 # scalar is broadcasted to [3 3 3]
print(res)
# [3 6 9]
Share and Support
@Python_Codes
#numpy
NumPy
Smart use of ‘:’ to extract the right shape
Sometimes you encounter a 3-dim array that is of shape (N, T, D), while your function requires a shape of (N, D). At a time like this, reshape() will do more harm than good, so you are left with one simple solution:
Example:
@Python_Codes
NumPy
Smart use of ‘:’ to extract the right shape
Sometimes you encounter a 3-dim array that is of shape (N, T, D), while your function requires a shape of (N, D). At a time like this, reshape() will do more harm than good, so you are left with one simple solution:
Example:
for t in xrange(T):
x[:, t, :] = # ...
Share and Support@Python_Codes