I’m working with inhomogeneous higher dimensions matrix, but I could solve all of the inhomogeneous problems by using:
self.matrix = np.array([np.empty((1, 5, 5, 5), dtype=object), np.empty((1, 5, 5, 5), dtype=object)])
This is a first simplification, where I just trying to work with 2 dimensions that “grow” independently, I can and will colapse some dimensions in the future.
The following code is a direct representation of my problem:
a1 = np.arange(1, 3).reshape(1, 2)
a2 = np.arange(1, 5).reshape(2, 2)
a1 = a2
a3 = np.arange(1, 3).reshape(1, 1, 2)
a4 = np.arange(1, 5).reshape(1, 2, 2)
a3[0] = a4[0]
I just don’t get why the first part works fine and the second one doesn’t, they just seem to be the same thing, the only difference is that I couldn’t work with inhomogeneous arrays without using it like the second example.
I’ll try to clarify it in 2D, since there are only 2 dimensions that need to be different, while the others can be treated as a function of either of the first 2.
I have [[1]]
at the beginning, and I need it to be possible to become [[1 2]]
or [[1][2]]
or the most likely scenario [[1 2][1][1 2 3][ 2 3]]
, where the numbers represent different states (for rows, not columns). I need each row to preserve the past states for my calculations, that’s why [[2][1][3][3]]
doesn’t work.
One thing that may be misrepresented is that [0][1]
is not the same as [2][1]
, but [2][1]
is the same as [3][1]
(notice how [3][0]
is empty), this logic applies to [2][2] != [3][2]
.
The amount of workaround I had to do already shows me that numpy doesn’t like to do this, I just couldn’t find any other way of working with this.
user27452820 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4
Your question isn’t real clear, but I think you want a ‘general’ way of constructing ‘inhomogenous’ arrays. Keep in mind that that is something of a step-child in numpy. Numpy is really intended for working with large ‘rectangular’ arrays with numeric values.
Your first example makes:
In [142]: arr = np.array([np.empty((1, 5, 5, 5), dtype=object), np.empty((1, 5, 5, 5), dtype=object)])
In [143]: arr.shape
Out[143]: (2, 1, 5, 5, 5)
In [144]: arr.dtype
Out[144]: dtype('O')
In [146]: arr[0,0,0,0,:]
Out[146]: array([None, None, None, None, None], dtype=object)
It’s a 5d array filled with None
objects. You can reshape it, but can’t add or remove elements, or change slices to other sizes.
Let’s instead make an object dtype array with just 2 elements:
In [147]: arr = np.empty(2, object); arr
Out[147]: array([None, None], dtype=object)
and your 2 arrays with different shapes:
In [148]: a3 = np.arange(1, 3).reshape(1, 1, 2)
...: a4 = np.arange(1, 5).reshape(1, 2, 2)
In [149]: a3
Out[149]: array([[[1, 2]]])
In [150]: a4
Out[150]:
array([[[1, 2],
[3, 4]]])
We can assign those to the elements of arr
:
In [151]: arr[0] = a3; arr[1] = a4; arr
Out[151]:
array([array([[[1, 2]]]), array([[[1, 2],
[3, 4]]])], dtype=object)
We can, at least when arr
is 1d, assign all elements from a list:
In [152]: arr[:] = [a3,a4]
In [153]: arr
Out[153]:
array([array([[[1, 2]]]), array([[[1, 2],
[3, 4]]])], dtype=object)
In many ways such a object array is little different from a list.
Note we cannot make an object array directly from the list:
In [154]: np.array([a3,a4], object)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[154], line 1
----> 1 np.array([a3,a4], object)
ValueError: could not broadcast input array from shape (2,) into shape (1,)
There are cases where np.array(alist, object)
works, but in others it makes a higher dimensional array as in your first example. In others you get this kind of error. The create and fill approach works in all cases.
But keep in mind that computations, and most other processing, of such array are not as fast or convenient as the equivalent on a homegeneous numeric array.
2