How is this not allowed in Numpy?
Posted by Delicious-Turnip8373@reddit | learnprogramming | View on Reddit | 6 comments
arr = np.array([
[[1, 2, 3, 3], [1, 0, 0]],
[[1, 2, 3, 3], [1, 2, 0]],
[[1, 3, 4, 3], [1, 3, 4]]
])
This gives error:
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 2 dimensions. The detected shape was (3, 2) + inhomogeneous part.
How is this not allowed? It is an array where each element is an array with 2 elements, the first being an array of length 4 and the second being an arra of length 3.
This is ridiculuous! How am I supposed to do anything with tensorflow like this.
Using Numpy version 1.26.4
dmazzoni@reddit
Because np.array isn't just a bunch of nested arrays, it's specifically a single multidimensional array, and intuitively what the error message is telling you is that it has to be "rectangular".
Let's take a simpler example of a matrix.
This is a matrix:
But this is not a matrix:
But that's what you're trying to do (just in 3-dimensions).
A matrix must be rectangular.
A 3-d tensor, or a 3-d numpy array, must also be "rectangular" in a 3-d sense. Not sure the more precise technical word, but the length of every dimension must be consistent. Without that property, you couldn't do any tensor math.
I think you're misunderstanding how to use tensorflow or math or both. Can you explain more about what you're trying to do?
Educational_Map_1369@reddit
Rectangular in 3d = cubical?
dmazzoni@reddit
Not quite right, because a cube is the 3d version of a square, where all sides are equal. I think you can call it a cuboid or rectangular prism, but those aren't common enough words if you don't work in that field.
Morsie@reddit
Take a look at the documentation https://numpy.org/doc/stable/reference/arrays.ndarray.html
What should the shape of your array be (3, 2, (4 or 3?))? At each level your array needs to have the same size.
Making a simpler example:
You can see cleary that shape of the array is not clearly defined. The general definition of the arrys in numpy is here https://numpy.org/doc/stable/reference/arrays.html
CatSwolo@reddit
Can you be specific with what you exactly want? As of now, you are intending to make a out of shape 3 dimensional array. But seems like you are intending to make 3 matrices, right?
If that is the case then you gotta do this:
Either way, the problem with your code is that 2 dimensional matrices have to be a MxN shape. You cant make a row with 4 elements and the following row has only 3 rows. Simply said a 2 dimensional matrix should always be in a form of a rectangle, not a trapezoid.
BadBoyJH@reddit
It isn't. It's a 3D array of integers, and the arrays aren't the same size, which they need to be in numpy.