4

I'm trying to set an element of a Numpy array to be another Numpy array. I'm not sure on how to do this since every time I try I get ValueError: setting an array element with a sequence.

I know this is possible with Python's list since I can append the new array to list and it will work.

This is an example of what I'm trying to do:

import numpy as np

finalArray = np.zeros(3)
finalList = []

a = np.arange(128).reshape(32,4)
b = np.arange(124).reshape(31,4)
c = np.arange(120).reshape(30,4)

# This works
finalList.append(a)
finalList.append(b)
finalList.append(c)

# This doesn't work
finalArray[0] = a
finalArray[1] = b
finalArray[2] = c

Any ideas on how to do this?

3
  • 2
    "I'm trying to set an element of a Numpy array to be another Numpy array" - bad move. NumPy might not be a good fit for your program's needs. Commented Jun 2, 2016 at 23:10
  • What do you want the shape/dtypes of finalList to be? Commented Jun 2, 2016 at 23:25
  • I was attempting to have variable shapes. For instance in the description above, a, b, and c have different shapes and I was hoping to have a numpy array filled with those. Commented Jun 3, 2016 at 3:22

3 Answers 3

1

It would work if

finalArray = np.zeros(3, dtype=object)
finalArray[0] = a

Then finalArray takes object points like a list.

But I hesitate suggesting this because this feature is being abused or misused by beginners.

https://stackoverflow.com/a/37597524/901925 - my answer to another question about a deep copy of an array of arrays.

The concatenate answer is the only alternative that makes sense, given the dimensions of your arrays.

A variation on the concatenate is

finalArray = np.zeros((93,4),a.dtype)
finalArray[:32,:] = a
etc

In other words, make finalArray big enough to receive the elements of the arrays, and copy values.

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

Comments

0

I think you're looking for numpy.concatenate:

In [11]: np.concatenate((a, b, c))
Out[11]:
array([[  0,   1,   2,   3],
       [  4,   5,   6,   7],
       [  8,   9,  10,  11],
       [ 12,  13,  14,  15],
       [ 16,  17,  18,  19],
       ...

This creates a single 93 x 4 numpy array:

In [12]: np.concatenate((a, b, c)).shape
Out[12]: (93, 4)

Comments

0

Numpy arrays are closer to statically typed. They can only contain the types specified on creation. They are more like the arrays of C or Java. While it may be possible to create a numpy array of numpy arrays (where each sub-array is treated as an generic python object), you most likely want to make a proper 2D numpy array.

To create a two dimensional array, feed a two dimensional list into the np.array function. For example,

finalArray = np.array([[0,0,0],[0,0,0],[0,0,0]])

creates a 3x3 two dimensional array filled with zeros.

1 Comment

You can also do this with np.zeros((3, 3), np.int64). That said, I'm not sure how this solves the question (what is x?) Edit: to be fair nor does mine, the question is a little ill-defined!

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.