3

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?

BGreen
  • 370
  • 3
  • 17

4 Answers4

5

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]])
balezz
  • 788
  • 5
  • 15
4

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:

enter image description here

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))

enter image description here

nick
  • 1,310
  • 8
  • 15
1

You could try:

np.full(shape, fill_value)

Specify the shape of the 2D array and pass your vector as the fill value.

stadepalli
  • 29
  • 6
0

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]]
richinrix
  • 434
  • 1
  • 3
  • 13