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?
-
1Very similar to stackoverflow.com/questions/1550130/…balezz– balezz2021-01-19 16:48:21 +00:00Commented Jan 19, 2021 at 16:48
-
Do you really need to create such an array? How are you going to use it?hpaulj– hpaulj2021-01-19 17:40:52 +00:00Commented Jan 19, 2021 at 17:40
4 Answers
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))
You can use .full() Else you can first use empty() to make an empty array and then use fill() to fill it with a certain value
np.full((no_of_rows,no_of_cols),the_no_u_want_evry_cell_to_have)
Example for 3x4 array with value of 69
np.full((3,4),69)
it gives an array of :
[[69,69,69,69]]
[[69,69,69,69]]
[[69,69,69,69]]
[[69,69,69,69]]

