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?

2
  • 1
    Very similar to stackoverflow.com/questions/1550130/… Commented Jan 19, 2021 at 16:48
  • Do you really need to create such an array? How are you going to use it? Commented Jan 19, 2021 at 17:40

4 Answers 4

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]])
Sign up to request clarification or add additional context in comments.

2 Comments

This involves unnecessary multiplication, though, right?
Yep, but broadcasting may be faster than np.repeat
5

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

2 Comments

Asked identical rows, not columns ))
Right. Sorry (I read that the rows should be identical e.g. row0 == row1). Updated.
1

You could try:

np.full(shape, fill_value)

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

Comments

-1

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

1 Comment

The question was about replicating a vector, not a single value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.