As others have already pointed out, NumPy's broadcasting is your friend here.
Note that because of this broadcasting rules, in NumPy it is actually a lot less frequent to use the transpose operation compared to other matrix-oriented tech stacks (read: MATLAB/Octave).
EDITED (reorganized)
The key is to get a correctly shaped array.
The best method is to use a slicing with an extra np.newaxis
/None
value. But you could also use ndarray.reshape()
:
import numpy as np
x = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
y = np.array([3, 6 ,9]).reshape(-1, 1) # same as: y = np.array([3, 6 ,9])[:, None]
y - x
Most importantly, a correctly shaped array would allow to use numexpr
, which can be more efficient than NumPy for large arrays (and it can be a good fit for your algorithm, if the bottleneck is that operation):
import numpy as np
import numexpr as ne
x = np.random.randint(1, 100, (3, 4))
y = np.random.randint(1, 100, (3, 1))
%timeit y - x
# The slowest run took 43.14 times longer than the fastest. This could mean that an intermediate result is being cached.
# 1000000 loops, best of 3: 879 ns per loop
%timeit ne.evaluate('y - x')
# The slowest run took 20.86 times longer than the fastest. This could mean that an intermediate result is being cached.
# 100000 loops, best of 3: 10.8 µs per loop
# not so exciting for small arrays, but for somewhat larger numbers...
x = np.random.randint(1, 100, (3000, 4000))
y = np.random.randint(1, 100, (3000, 1))
%timeit y - x
# 10 loops, best of 3: 33.1 ms per loop
%timeit ne.evaluate('y - x')
# 100 loops, best of 3: 10.7 ms per loop
# which is roughly a factor 3 faster on my machine
In this case, there is no much difference on how you get to a correctly shaped account -- either slicing or reshape -- but slicing seems to be twice as fast.
To put some numbers to it (edited as per comments):
import numpy as np
# creating the array does not depend too much as long as its size is the same
%timeit y = np.zeros((3000000))
# 838 µs ± 10.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit y = np.zeros((3000000, 1))
# 825 µs ± 12.7 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
%timeit y = np.zeros((3000, 1000))
# 827 µs ± 14.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
# ...and reshaping / slicing is independent of the array size
x = np.zeros(3000000)
%timeit x[:, None]
# 147 ns ± 4.02 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
%timeit x.reshape(-1, 1)
# 214 ns ± 9.55 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
x = np.zeros(300)
%timeit x[:, None]
# 146 ns ± 0.659 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
%timeit x.reshape(-1, 1)
# 212 ns ± 1.56 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
Needless to say that %timeit
benchmarks should be taken with a grain of salt.