0

(Disclaimer: Yes, I know, in a perfect world I should avoid working with jagged arrays in numpy altogether... The following are some questions regarding toy example that are all related to jagged arrays and that I don't understand.)

1] Consider

my_array_1 = np.array([
                    [np.array([[0,0],[0, 0],[0,0]]),np.array([0,0,0])],
                    [np.array([0,0,0]).reshape(0,0),np.array([0])]
                    ])

For some reason, this np.equal(NN_z,NN_z) throws an error

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().

The only way to get it to work is to loop through the array and compared each subarray individually. Is there a more elegant solution?

2] np.array([[[1,2,3],[2,2,2]], np.array([5,2])]) evaluates finde, but making a very small change and giving the np.array constructor ndarrays instead of lists (but with the same elements), somehow doesn't work anymore: np.array([np.array([[1,2,3],[2,2,2]]), np.array([5,2])]), giving the error

ValueError: could not broadcast input array from shape (2,3) into shape (2)
  

What is going on here?

3] In this answer a sequence of examples of jagged arrays was provided and how np.array fails on them. I'm not asking you to go through all of them, but rather to give me some insight: Is it even possible to discern some general rule how np.array works for jagged arrays?

billyboy
  • 49
  • 1
  • 5
  • what happenss in the `doesn't work` case? – hpaulj Dec 02 '20 at 13:15
  • It is well known that for certain shape combinations (same first dimension) np.array raises this kind of error. Depending np.array to make a ragged array in all cases is not a good idea. You don't get a ragged array when the argument arrays are all the same size either. – hpaulj Dec 02 '20 at 16:58
  • Object dtypes arrays are something of a step-child in `numpy`. The fast compiled methods work on numeric multidimensional arrays. The ragged arrays are more like lists, using the same Python object referencing. They are slower, and math on them is hit-or-miss. That includes things like the `np.equal` test. – hpaulj Dec 02 '20 at 17:04

0 Answers0