Given a vector v, I would like to create a 2D numpy array with all rows equal to v. What is the best way to do this? There must be a faster way to do this than to use for loops. There are functions like fill, ones and zeros for filling the entire array with one value, but what about filling each column with the same value, but different values for each column?
Broadcasting may be useful:
v = np.random.normal(size=(4, 1))
v * np.ones((4, 3))
Output:
array([[ 1.29471919, 1.29471919, 1.29471919],
[ 0.26505351, 0.26505351, 0.26505351],
[ 1.04885901, 1.04885901, 1.04885901],
[-0.18587621, -0.18587621, -0.18587621]])
Use np.repeat. For example:
v = np.random.normal(size=(4, 1))
np.repeat(v, 3, axis=1)
Output:
array([[ 1.7676415 , 1.7676415 , 1.7676415 ],
[ 0.77139662, 0.77139662, 0.77139662],
[ 1.34501879, 1.34501879, 1.34501879],
[-1.3641335 , -1.3641335 , -1.3641335 ]])
UPDATE: I recommend you use the answer from @balezz (https://stackoverflow.com/a/65795639/5763165) due to speed improvements. If the number of repeats is large the broadcasting method is better:
import timeit
setup = "import numpy as np; v = np.random.normal(size=(1000, 1))"
repeat, multiply = [], []
for i in range(50):
multiply.append(timeit.timeit(f'v * np.ones((v.shape[0], {i}))', setup=setup, number=10000))
repeat.append(timeit.timeit(f'np.repeat(v, {i}, axis=1)', setup=setup, number=10000))
Gives the following:

The improvement of multiply over repeat persists in most cases when varying the size of the input vector as well:
import timeit
repeat, multiply = [], []
for i in [2, 5, 10, 50, 100, 1000, 10000]:
setup = f"import numpy as np; v = np.random.normal(size=({i}, 1))"
repeat.append(timeit.timeit(f'np.repeat(v, 50, axis=1)', setup=setup, number=10000))
multiply.append(timeit.timeit(f'v * np.ones((v.shape[0], 50))', setup=setup, number=10000))

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With