0

I have a numpy array D of dimensions 4x4

I want a new numpy array based on an user defined value v

If v=2, the new numpy array should be [D D]. If v=3, the new numpy array should be [D D D]

How do i initialise such a numpy array as numpy.zeros(v) dont allow me to place arrays as elements?

1
  • 1
    Do you want the new array to be 2D or 3D? It's not quite clear what [D D] is supposed to mean. Commented May 11, 2014 at 1:43

3 Answers 3

4

If I understand correctly, you want to take a 2D array and tile it v times in the first dimension? You can use np.repeat:

# a 2D array
D = np.arange(4).reshape(2, 2)

print D
# [[0 1]
#  [2 3]]

# tile it 3 times in the first dimension
x = np.repeat(D[None, :], 3, axis=0)

print x.shape
# (3, 2, 2)

print x
# [[[0 1]
#   [2 3]]

#  [[0 1]
#   [2 3]]

#  [[0 1]
#   [2 3]]]

If you wanted the output to be kept two-dimensional, i.e. (6, 2), you could omit the [None, :] indexing (see this page for more info on numpy's broadcasting rules).

print np.repeat(D, 3, axis=0)
# [[0 1]
#  [0 1]
#  [0 1]
#  [2 3]
#  [2 3]
#  [2 3]]

Another alternative is np.tile, which behaves slightly differently in that it will always tile over the last dimension:

print np.tile(D, 3)
# [[0, 1, 0, 1, 0, 1],
#  [2, 3, 2, 3, 2, 3]])
Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what i wanted
0

You can do that as follows:

import numpy as np
v = 3
x = np.array([np.zeros((4,4)) for _ in range(v)])

>>> print x
[[[ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]]

 [[ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]]

 [[ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]
  [ 0.  0.  0.  0.]]]

Comments

0

Here you go, see if this works for you.

import numpy as np
v = raw_input('Enter: ')

To intialize the numpy array of arrays from user input (obviously can be whatever shape you're wanting here):

b = np.zeros(shape=(int(v),int(v)))

I know this isn't initializing a numpy array but since you mentioned wanting an array of [D D] if v was 2 for example, just thought I'd throw this in as another option as well.

new_array = []
for x in range(0, int(v)):
    new_array.append(D)

3 Comments

appending to numpy arrays is quite slow and something you should generally try to avoid doing
Which is why it wasn't my first answer, just an alternative.
Sorry, I should have read that more carefully - I just realised you weren't actually creating a numpy array at all, but rather a list of numpy arrays. You'd have to then convert the list to a numpy array, using np.concatenate, np.vstack, np.hstack etc. Either way, it's more efficient to use np.tile or np.repeat to get there in one call.

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.