How do I connect different shapes of ndarrays with new axes added?
For example, a two-dimensional array of two different shapes (columns equal)
a=array([1,1,1,1],
[1,1,1,1],
[1,1,1,1]])
b = array([2,2,2,2],
[2,2,2,2]])
Connect vertically and
array([[1,1,1,1],
[1,1,1,1],
[1,1,1,1]],
[[2,2,2,2],
[2,2,2,2]]])
I would like to create a three-dimensional ndarray array like this.
When connected by np.vstack, no new axes are added, so the connected ones become a two-dimensional array.
Specifically, when reading image data in ndarray format as a data set for classification, I want to combine the 256-dimensional (this is different for each class) *? (but each class has a three-dimensional array for easy index access).
I look forward to your kind cooperation.
python numpy
I heard it's called a "jag arrangement.
Jug Array/Jug Array-Array-Wikipedia
Python is briefly described in list and so on to convert it into a numpy array.
You can put it in a list or something like a table and convert it into a numpy array.It works just as you normally imagine.
For example, if you want to treat the list of images as a numpy array, you can use this method.
If the ndim is not aligned, an error will occur.
If the ndim is the same but the shape is not aligned, it is treated as an object-type array that does not cause an error and cannot be converted successfully.
Therefore, based on the array of questions, c=np.array(a,b))
or c=np.array([a,b])
can create the following objects:
array([1,1,1,1,1],
[1, 1, 1, 1],
[1, 1, 1, 1]]),
array([2,2,2,2],
[2,2,2,2]), dtype=object)
If they are both the same shape:
array([[1,1,1,1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[2, 2, 2, 2],
[2, 2, 2, 2],
[2, 2, 2, 2]]])
It's a good idea to create a program with the assumption that something can be done and the processing can continue.
is a similar question, but
It's optimized for homogenous arrays of numbers with fixed dimensions. If you really need arrays of arrays, better use a nested list
As stated,
NumPy guarantees fast operations and flexible operation by optimizing a fixed-size, same-dimensional array of numbers. Simply put, NumPy is meaningless to use if that assumption is broken.
© 2025 OneMinuteCode. All rights reserved.