2

I want to generate a an array of ordered numbers and then multiply it into another array :

[ [0,1,2,3,4,5] [0,1,2,3,4,5] [0,1,2,3,4,5] ... [0,1,2,3,4,5] ]

I can generate the first [0,1,2,3,4,5] with nums = np.arange(0, 6) but then if I multiply by a number inside a list it just increases the values = [nums* 3] = [0,3,6,9,12,15]. How can I do this ?

3
  • 1
    Makes no sense. When you write multiply, you want to duplicate it X times and store it into a list ? That's not multiplication. Commented Jun 21, 2018 at 12:52
  • provide input and expected output Commented Jun 21, 2018 at 12:53
  • np.repeat(np.arange(0, 6), (4, 1)) Commented Jun 21, 2018 at 12:58

3 Answers 3

7

Using numpy methods (numpy.repeat and numpy.expand_dims):

np.repeat(np.expand_dims(np.arange(0,6), axis=0), repeats=5, axis=0)

array([[0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5]])

Or, more simply:

np.repeat([np.arange(0,6)],repeats=5, axis=0)

The first method is useful if you were trying to expand a pre-existing one dimensional array. If you are trying to create your array from the start, the second method is more straightforward.

Sign up to request clarification or add additional context in comments.

Comments

3

BTW, why not simply use np.array() as in:

In [147]: nums = np.arange(6)

In [148]: nums
Out[148]: array([0, 1, 2, 3, 4, 5])

In [149]: [nums] * 5
Out[149]: 
[array([0, 1, 2, 3, 4, 5]),
 array([0, 1, 2, 3, 4, 5]),
 array([0, 1, 2, 3, 4, 5]),
 array([0, 1, 2, 3, 4, 5]),
 array([0, 1, 2, 3, 4, 5])]

In [150]: np.array([nums] * 5)
Out[150]: 
array([[0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5]])

In one-line:

In [151]: np.array([np.arange(6)] * 5)
Out[151]: 
array([[0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5]])

Comments

1

you can't multiply a numpy array with a scalar and expect the same behaviour as multiplying a python list (or string) with a scalar.

for numpy, the multiplication operator will broadcast the multiplication over all the array elements:

i.e.

np.array([1,2,3]) * 2 == np.array([1*2, 2*2, 3*2) == np.array([2,4,6])

instead, you can use a list comprehension

np.array([np.arange(6) for _ in range(4)])
array([[0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5]])

or generate a list of lists through multiplication and then convert to numpy array & reshape:

np.array([list(range(6))*4]).reshape(4,6)
array([[0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5],
       [0, 1, 2, 3, 4, 5]])

or, generate an array of shape (1,6) and repeat along the first axix:

np.repeat(np.arange(6).reshape(1,6), repeats=4, axis=0)
# produces the same output as the example outputs above.

1 Comment

The next answer makes the simple change of including the multiplication inside the np.array() invocation. From the wording of your answer it would appear you were not aware of it.

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.