How do I transpose an empty numpy array? - python

I have an 'empty' 2D array in numpy as
arr = np.array([[[], [], []], [[], [], []]]).
When I do np.transpose(arr), I get the result: [], instead of the expected:
[[[],[]],[[],[]],[[],[]]].

You get an empty array [] with the right shape. Mind that also arr is an empty array [].
arr = np.array([[[], [], []], [[], [], []]])
print(arr, arr.shape)
t = arr.T
print(t, t.shape)
[] (2, 3, 0)
[] (0, 3, 2)

Look at what your expression produces:
In [41]: arr = np.array([[[], [], []], [[], [], []]])
In [42]: arr
Out[42]: array([], shape=(2, 3, 0), dtype=float64)
In [43]: print(arr)
[]
In [44]: print(repr(arr))
array([], shape=(2, 3, 0), dtype=float64)
The print shows the str display, while repr is a fuller one that tells us shape and dtype. np.array has followed the [] all the way down, making a 3d array that has float elements. But since the lowest level is created from [], it has size 0 dimension, and overall the array has 0 elements.
What you want, based on the comment, is a (2,3) array with object dtype. This can hold objects such as lists. But making that with np.array is tricky. A more general tool is to make one with the right shape and dtype.
I like to use empty for this, since is fills the object array with None elements. (In a numeric dtype np.empty has other problems, but for object it's nice.)
In [45]: arr = np.empty((2,3), dtype=object)
In [46]: arr
Out[46]:
array([[None, None, None],
[None, None, None]], dtype=object)
But trying to assign a list to elements of such an array can be tricky:
In [47]: arr[:]=[]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-47-b5ed8d639464> in <module>
----> 1 arr[:]=[]
ValueError: could not broadcast input array from shape (0) into shape (2,3)
In [48]: np.full((2,3),[])
...
ValueError: could not broadcast input array from shape (0) into shape (2,3)
full has the same problem. In addition assignment like this, if it worked, would put the same list in each slot, the equivalent of making a list with [[]]*3. We want a new [] in each slot.
We can do this in the 2d arr, but the iteration is simpler with a 1d (which can be reshaped later):
In [49]: arr = np.empty(6, object)
In [50]: arr
Out[50]: array([None, None, None, None, None, None], dtype=object)
In [51]: for i in range(6): arr[i]=[]
In [52]: arr
Out[52]:
array([list([]), list([]), list([]), list([]), list([]), list([])],
dtype=object)
In [53]: arr = np.reshape(arr, (2,3))
In [54]: arr
Out[54]:
array([[list([]), list([]), list([])],
[list([]), list([]), list([])]], dtype=object)
Obvious we could transpose that, but could just as well use (3,2) in the reshape.
Note that this display of arr clearly shows that it contains list objects.
But do you really need such an array? Is it worth the extra work?

Related

How to reorder dstack

I have 6 files with shape (6042,) or 1 column. I used dstack to stack the 6 files in hopes of getting a shape (6042, 1, 6). But after I stack it I get shape (1, 6042, 6). Then I tried to change the order using
new_train = np.reshape(train_x,(train_x[1],1,train_x[2]))
error appears:
IndexError: index 1 is out of bounds for axis 0 with size 1
This is my dstack code:
train_x = dstack([train_data['gx'],train_data['gy'], train_data['gz'], train_data['ax'],train_data['ay'], train_data['az']])
error is because
train_x[1]
tries looking 2nd row of train_x but it has only 1 row as you said shape 1, 6042, 6). So you need to look shape and index it
new_train = np.reshape(train_x, (train_x.shape[1], 1, train_x.shape[2]))
but this can be also doable with transpose
new_train = train_x.transpose(1, 0, 2)
so this changes axes 0 and 1's positions.
Other solution is fixing dstack's way. It gives "wrong" shape because your datas shape not (6042, 1) but (6042,) as you say. So if you reshape the datas before dstack it should also work:
datas = [train_data['gx'],train_data['gy'], train_data['gz'],
train_data['ax'],train_data['ay'], train_data['az']]
#this list comprehension makes all shape (6042, 1) now
new_datas = [td[:, np.newaxis] for td in datas]
new_train = dstack(new_datas)
You can use np.moveaxis(X, 0, -2), where X is your (1,6042,6) array.
This function swaps the axis. 0 for your source axis and -2 is your destination axis.
np.dstack uses:
arrs = atleast_3d(*tup)
to convert the list of arrays to a list of 3d arrays.
In [51]: alist = [np.ones(3,int),np.zeros(3,int)]
In [52]: alist
Out[52]: [array([1, 1, 1]), array([0, 0, 0])]
In [53]: np.atleast_3d(*alist)
Out[53]:
[array([[[1],
[1],
[1]]]),
array([[[0],
[0],
[0]]])]
In [54]: _[0].shape
Out[54]: (1, 3, 1)
Concatenating those on the last dimension produces the (1,n,6) kind of result.
With expand_dims we can adjust the shape of all arrays to (n,1,1), and then do the concatenate:
In [62]: np.expand_dims(alist[0],[1,2]).shape
Out[62]: (3, 1, 1)
In [63]: np.concatenate([np.expand_dims(a,[1,2]) for a in alist], axis=2)
Out[63]:
array([[[1, 0]],
[[1, 0]],
[[1, 0]]])
In [64]: _.shape
Out[64]: (3, 1, 2)
direct reshape or newaxis would work just as well:
In [65]: np.concatenate([a[:,None,None] for a in alist], axis=2).shape
Out[65]: (3, 1, 2)
stack is another cover that adjusts shapes before concatenate:
In [67]: np.stack(alist,1).shape
Out[67]: (3, 2)
In [68]: np.stack(alist,1)[:,None].shape
Out[68]: (3, 1, 2)
So there are lots of ways to get what you want, whether it means adjusting shapes before the concatenate, or after.

Numpy: np_array[index_order_array] changes the order of elements in an array?

I'm new to Python and Numpy
Why array = [1,2,3,4] and new_array = array[[3,2,0,1]] results in changing the order of elements as mentioned in the inner array?
import numpy as np
array = np.array([10,20,30,40,50])
array_link = np.array(['A','B','C','D','E'])
new_array = np.ndarray(5, dtype=np.int32)
new_array_link = np.ndarray(5, dtype=np.int32)
perm = np.random.permutation(array.shape[0])
new_array = array[perm]
new_array_link = array_link[perm]
print(new_array)
print(new_array_link)
# Output:
# [30 40 10 50 20]
# ['C' 'D' 'A' 'E' 'B']
Here is the Playground
Is this how it is supposed to work? Shouldn't it be initializing a new (perhaps 2D) array with the elements of inner array (as the first row)?
The first of these 2 lines is useless. python does not require that you initialize or 'pre-define' a variable. The first creates an array; the second also creates one, and reassigns the variable. The original value of new_array is discarded.
new_array = np.ndarray(5, dtype=np.int32)
...
new_array = array[perm]
And as a general rule, np.ndarray is only used for advanced purposes. np.array, np.zeros etc are used to create new arrays.
array is a poor choice of variable name. array looks too much like np.array, and actually confused me when I first copied the above lines.
array = np.array([10,20,30,40,50])
In sum your code does:
In [28]: arr = np.array([10,20,30,40,50])
In [29]: perm = np.random.permutation(arr.shape[0])
In [30]: perm
Out[30]: array([2, 0, 1, 4, 3])
In [31]: arr1 = arr[perm]
In [32]: arr1
Out[32]: array([30, 10, 20, 50, 40])
arr1 is a new array with values selected from arr. arr itself is unchanged.
You could assign values to predefined array this way:
In [35]: arr2 = np.zeros(5, int)
In [36]: arr2
Out[36]: array([0, 0, 0, 0, 0])
In [37]: arr2[:] = arr[perm]
In [38]: arr2
Out[38]: array([30, 10, 20, 50, 40])
In arr[perm], the result is the same shape as perm, in this case a 5 element 1d array. If I turn perm into a (5,1) column array, the result is also a (5,1) array:
In [40]: arr[perm[:,None]]
Out[40]:
array([[30],
[10],
[20],
[50],
[40]])
In [41]: _.shape
Out[41]: (5, 1)
Another example of array indexing - with a (2,2) array:
In [43]: arr[np.array([[0,1],[2,3]])]
Out[43]:
array([[10, 20],
[30, 40]])

trouble with basic numpy multiplication

In my code, I multiply two matrices:
c = b*a
Where a outputs as
array([array([-0.08358731, 0.07145386, 0.1052811 , -0.05362566]),
array([-0.05335939, -0.03136824, -0.01260714, 0.11532605]),
array([-0.09164538, 0.02280118, -0.00290509, 0.09415849])], dtype=object)
and b outputs as
array([ 0.60660017, 0.54703557, 0.69928535, 0.70157223])
...That should work right (where values of b is multiplied by each value of each row in a)?
Instead, I get
ValueError: operands could not be broadcast together with shapes (3) (4)
But then when I try it in a separate python console, it works great.
(bare in mind I've set array = np.array)
>>> aa = array([array([-0.12799382, 0.07758469, -0.02968546, -0.01811048]),
array([-0.00465869, -0.00483031, -0.00591955, -0.00386022]),
array([-0.02036786, 0.0078658 , 0.09493727, -0.01790333])], dtype=object)
>>> bb = array([ 0.16650179, 0.74140229, 0.60859776, 0.37505098])
>>> aa * bb
array([[-0.021311200138937801, 0.057521466834940096, -0.0180665044605696,
-0.0067923532722703999],
[-0.00077568022405510005, -0.0035812028954099002,
-0.0036026248702079999, -0.0014477792940156],
[-0.0033912851484694004, 0.0058317221326819992, 0.0577786098625152,
-0.0067146614617633995]], dtype=object)
The fact it works here really confuses me...
Your first array has only 1 dimension and 3 "object" elements while your second array has 1 dimension and 4 float elements. numpy uses element-wise arithmetic operations and there is just no way it can do that with one 3-item array and a 4-item array therefore the Exception.
>>> x = np.empty(3, dtype=object)
>>> x[0] = np.array([-0.08358731, 0.07145386, 0.1052811 , -0.05362566])
>>> x[1] = np.array([-0.05335939, -0.03136824, -0.01260714, 0.11532605])
>>> x[2] = np.array([-0.09164538, 0.02280118, -0.00290509, 0.09415849])
>>> x.shape
(3, )
The example above is an awful way of creating a numpy.array and should be avoided!
The difference to your second example is that it doesn't have numpy-arrays inside an array, it creates a multidimensional (3x4) array:
>>> x_new = np.array(list(x))
>>> x_new # no nested arrays!
array([[-0.12799382, 0.07758469, -0.02968546, -0.01811048],
[-0.00465869, -0.00483031, -0.00591955, -0.00386022],
[-0.02036786, 0.0078658, 0.09493727, -0.01790333]], dtype=object)
>>> x_new.shape
(3, 4)
That the multiplication operation works with the new array (x_new or your aa) is because numpy broadcasts the arrays. Here every row will be multiplied by one of your items in the second array.
Your original a and the copy aa have different shapes. Do a a.shape and aa.shape. The problem is with how object arrays are created. np.array tries to create as high a dimensional object as it can.
a is (3,) array, a 1d array containing 3 arrays.
aa is (3,4) array, a 2d array contain numbers as objects (not floats).
To construct a I have to take a convoluted route:
In [659]: a=np.empty((3,), object)
In [660]: a[0]=np.array([-0.08358731, 0.07145386, 0.1052811 , -0.05362566])
...: a[1]=np.array([-0.05335939, -0.03136824, -0.01260714, 0.11532605])
...: a[2]=np.array([-0.09164538, 0.02280118, -0.00290509, 0.09415849])
...:
In [661]: a
Out[661]:
array([array([-0.08358731, 0.07145386, 0.1052811 , -0.05362566]),
array([-0.05335939, -0.03136824, -0.01260714, 0.11532605]),
array([-0.09164538, 0.02280118, -0.00290509, 0.09415849])], dtype=object)
In [662]: a.shape
Out[662]: (3,)
I can multiply those 3 elements with another 3 element array (that doesn't always work with object arrays, but here the elements implement *.)
In [663]: a*np.array([0,1,2])
Out[663]:
array([array([-0., 0., 0., -0.]),
array([-0.05335939, -0.03136824, -0.01260714, 0.11532605]),
array([-0.18329076, 0.04560236, -0.00581018, 0.18831698])], dtype=object)
But if I copy-n-paste as you did I get
In [665]: aa = array([array([-0.12799382, 0.07758469, -0.02968546, -0.01811048]
...: ),
...: array([-0.00465869, -0.00483031, -0.00591955, -0.00386022])
...: ,
...: array([-0.02036786, 0.0078658 , 0.09493727, -0.01790333])
...: ], dtype=object)
In [666]: aa.shape
Out[666]: (3, 4)
Now that (3,4) can multiply a (4,) array.
vstack can convert the (3,) object array into a (3,4) array of floats:
In [667]: a3=np.vstack(a)
In [668]: a3.shape
Out[668]: (3, 4)
In [669]: a3.dtype
Out[669]: dtype('float64')
In [670]: a3
Out[670]:
array([[-0.08358731, 0.07145386, 0.1052811 , -0.05362566],
[-0.05335939, -0.03136824, -0.01260714, 0.11532605],
[-0.09164538, 0.02280118, -0.00290509, 0.09415849]])
==============
You can multiply a by a b that matches it in shape and type:
In [681]: b=np.empty((3,),object)
In [682]: for i in range(3):
...: b[i]=np.arange(i,i+4)
...:
In [683]: b # 3 arrays of length 4 each
Out[683]: array([array([0, 1, 2, 3]), array([1, 2, 3, 4]), array([2, 3, 4, 5])], dtype=object)
In [684]: a*b
Out[684]:
array([array([-0. , 0.07145386, 0.2105622 , -0.16087698]),
array([-0.05335939, -0.06273648, -0.03782142, 0.4613042 ]),
array([-0.18329076, 0.06840354, -0.01162036, 0.47079245])], dtype=object)
Basically it is doing: for i in range(3): res[i]=a[i]*b[i]

Numpy multi-dimensional slicing with multiple boolean arrays

I'm trying to use individual 1-dimensional boolean arrays to slice a multi-dimension array. For some reason, this code doesn't work:
>>> a = np.ones((100, 200, 300, 2))
>>> a.shape
(100, 200, 300, 2)
>>> m1 = np.asarray([True]*200)
>>> m2 = np.asarray([True]*300)
>>> m2[-1] = False
>>> a[:,m1,m2,:]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (200,) (299,)
>>> m2 = np.asarray([True]*300) # try again with all 300 dimensions True
>>> a[:,m1,m2,:]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (200,) (300,)
But this works just fine:
>>> a = np.asarray([[[1, 2], [3, 4], [5, 6]], [[11, 12], [13, 14], [15, 16]]])
>>> a.shape
(2, 3, 2)
>>> m1 = np.asarray([True, False, True])
>>> m2 = np.asarray([True, False])
>>> a[:,m1,m2]
array([[ 1, 5],
[11, 15]])
Any idea of what I might be doing wrong in the first example?
Short answer: The number of True elements in m1 and m2 must match, unless one of them has only one True term.
Also distinguish between 'diagonal' indexing and 'rectangular' indexing. This is about indexing, not slicing. The dimensions with : are just along for the ride.
Initial ideas
I can get your first case working with:
In [137]: a=np.ones((100,200,300,2))
In [138]: m1=np.ones((200,),bool)
In [139]: m2=np.ones((300,),bool)
In [140]: m2[-1]=False
In [141]: I,J=np.ix_(m1,m2)
In [142]: a[:,I,J,:].shape
Out[142]: (100, 200, 299, 2)
np.ix_ turns the 2 boolean arrays into broadcastable index arrays
In [143]: I.shape
Out[143]: (200, 1)
In [144]: J.shape
Out[144]: (1, 299)
Note that this picks 200 'rows' in one dimension, and 299 in the other.
I'm not sure why this kind of reworking of the arrays is needed in this case, but not in the 2nd
In [154]: b=np.arange(2*3*2).reshape((2,3,2))
In [155]: n1=np.array([True,False,True])
In [156]: n2=np.array([True,False])
In [157]: b[:,n1,n2]
Out[157]:
array([[ 0, 4], # shape (2,2)
[ 6, 10]])
Taking the same ix_ strategy produces the same values but a different shape:
In [164]: b[np.ix_(np.arange(b.shape[0]),n1,n2)]
# or I,J=np.ix_(n1,n2);b[:,I,J]
Out[164]:
array([[[ 0],
[ 4]],
[[ 6],
[10]]])
In [165]: _.shape
Out[165]: (2, 2, 1)
Both cases use all rows of the 1st dimension. The ix one picks 2 'rows' of the 2nd dim, and 1 column of the last, resulting the (2,2,1) shape. The other picks b[:,0,0] and b[0,2,0] terms, resulting (2,2) shape.
(see my addenda as to why both are simply broadcasting).
These are all cases of advanced indexing, with boolean and numeric indexes. One can study the docs, or one can play around. Sometimes it's more fun to do the later. :)
(I knew that ix_ was good for adding the necessary np.newaxis to arrays so can be broadcast together, but didn't realize that worked with boolean arrays as well - it uses np.nonzero() to convert boolean to indices.)
Resolution
Underlying this is, I think, a confusion over 2 modes of indexing. which might called 'diagonal' and 'rectangular' (or element-by-element selection versus block selection). To illustrate look at a small 2d array
In [73]: M=np.arange(6).reshape(2,3)
In [74]: M
Out[74]:
array([[0, 1, 2],
[3, 4, 5]])
and 2 simple numeric indexes
In [75]: m1=np.arange(2); m2=np.arange(2)
They can be used 2 ways:
In [76]: M[m1,m2]
Out[76]: array([0, 4])
and
In [77]: M[m1[:,None],m2]
Out[77]:
array([[0, 1],
[3, 4]])
The 1st picks 2 points, the M[0,0] and M[1,1]. This kind of indexing lets us pick out the diagonals of an array.
The 2nd picks 2 rows and from that 2 columns. This is the kind of indexing the np.ix_ produces. The 1st picks 2 points, the M[0,0] and M[1,1]. This a 'rectangular' form of indexing.
Change m2 to 3 values:
In [78]: m2=np.arange(3)
In [79]: M[m1[:,None],m2] # returns a 2x3
Out[79]:
array([[0, 1, 2],
[3, 4, 5]])
In [80]: M[m1,m2] # produces an error
...
ValueError: shape mismatch: objects cannot be broadcast to a single shape
But if m2 has just one element, we don't get the broadcast error - because the size 1 dimension can be expanded during broadcasting:
In [81]: m2=np.arange(1)
In [82]: M[m1,m2]
Out[82]: array([0, 3])
Now change the index arrays to boolean, each matching the length of the respective dimensions, 2 and 3.
In [91]: m1=np.ones(2,bool); m2=np.ones(3,bool)
In [92]: M[m1,m2]
...
ValueError: shape mismatch: objects cannot be broadcast to a single shape
In [93]: m2[2]=False # m1 and m2 each have 2 True elements
In [94]: M[m1,m2]
Out[94]: array([0, 4])
In [95]: m2[0]=False # m2 has 1 True element
In [96]: M[m1,m2]
Out[96]: array([1, 4])
With 2 and 3 True terms we get an error, but with 2 and 2 or 2 and 1 it runs - just as though we'd used the indices of the True elements: np.nonzero(m2).
To apply this to your examples. In the first, m1 and m2 have 200 and 299 True elements. a[:,m1,m2,:] fails because of a mismatch in the number of True terms.
In the 2nd, they have 2 and 1 True terms, with nonzero indices of [0,2] and [0], which can be broadcast to [0,0]. So it runs.
http://docs.scipy.org/doc/numpy-1.10.0/reference/arrays.indexing.html
explains boolean array indexing in terms of nonzero and ix_.
Combining multiple Boolean indexing arrays or a Boolean with an integer indexing array can best be understood with the obj.nonzero() analogy. The function ix_ also supports boolean arrays and will work without any surprises.
Addenda
On further thought the distinction between 'diagonal' and 'block/rectangular' indexing might be more my mental construct that numpys. Underlying both is the concept of broadcasting.
Take the n1 and n2 booleans, and get their nonzero equivalents:
In [107]: n1
Out[107]: array([ True, False, True], dtype=bool)
In [108]: np.nonzero(n1)
Out[108]: (array([0, 2], dtype=int32),)
In [109]: n2
Out[109]: array([ True, False], dtype=bool)
In [110]: np.nonzero(n2)
Out[110]: (array([0], dtype=int32),)
Now try broadcasting in the 'diagonal' and 'rectangular' modes:
In [105]: np.broadcast_arrays(np.array([0,2]),np.array([0]))
Out[105]: [array([0, 2]),
array([0, 0])]
In [106]: np.broadcast_arrays(np.array([0,2])[:,None],np.array([0]))
Out[106]:
[array([[0],
[2]]),
array([[0],
[0]])]
One produces (2,) arrays, the other (2,1).
This might be a simple workaround:
a[:,m1,:,:][:,:,m2,:]

numpy masked ragged array

I want to fill a masked array whose dtype is object (because I need to store masked ragged arrays) with a non scalar fill_value.
Here's an example of a 2D array whose elements are 1D numpy arrays. Of course, I would like the fill_value to be an empty array.
import numpy as np
arr = np.array([
[np.arange(10), np.arange(5), np.arange(3)],
[np.arange(1), np.arange(2), np.array([])],
])
marr = np.ma.array(arr)
marr.mask = [[True, False, False],
[True, False, True]]
marr.fill_value = np.array([])
marr.filled()
Unfortunately, it yields an error on the last line:
ValueError: could not broadcast input array from shape (0) into shape (2,3)
I could manually extract the mask, and apply it on an element-by-element algorithm; but it does not seem to be the right direction to me.
Thank you !
I would not count on MaskedArray to work well with object dtype arrays. filled is trying to copy the fill value, an array, into a subset of the slots in the data. Due to broadcasting that can be tricky, even without the masking layer.
Look at the full error:
In [39]: marr.filled()
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-39-219e016a84cf> in <module>
----> 1 marr.filled()
/usr/local/lib/python3.6/dist-packages/numpy/ma/core.py in filled(self, fill_value)
3718 result = self._data.copy('K')
3719 try:
-> 3720 np.copyto(result, fill_value, where=m)
3721 except (TypeError, AttributeError):
3722 fill_value = narray(fill_value, dtype=object)
ValueError: could not broadcast input array from shape (0) into shape (2,3)
np.copyto tries to broadcast result, fill_value and m (mask) against each other, and then copy the corresponding (mask==true) elements from fill_value to result.
marr.data and marr.mask are both (2,3). But broadcasting a (0,) shape to (2,3) doesn't work, and isn't what you want anyways.
Filling with a scalar works, but not with an array (or list).
In [56]: np.broadcast_to(np.array([]),(2,3))
...
ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (0,) and requested shape (2,3)
A (1,) shape array will broadcast -
In [57]: np.broadcast_to(np.array([1]),(2,3))
Out[57]:
array([[1, 1, 1],
[1, 1, 1]])
But the filled result is not an array; it's a scalar:
In [58]: marr.filled(np.array([1]))
Out[58]:
array([[1, array([0, 1, 2, 3, 4]), array([0, 1, 2])],
[1, array([0, 1]), 1]], dtype=object)
A fill that works
I can make this fill work if I define a (1,) object dtype array, and putting the (0,) array in it (as an object).
In [97]: Ofill = np.array([None], object)
In [98]: Ofill[0] = np.array([])
In [99]: Ofill
Out[99]: array([array([], dtype=float64)], dtype=object)
In [100]: marr.filled(Ofill)
Out[100]:
array([[array([], dtype=float64), array([0, 1, 2, 3, 4]),
array([0, 1, 2])],
[array([], dtype=float64), array([0, 1]),
array([], dtype=float64)]], dtype=object)
This works because Ofill can be broadcasted to (2,3) without messing with the shape of the element
In [101]: np.broadcast_to(Ofill,(2,3))
Out[101]:
array([[array([], dtype=float64), array([], dtype=float64),
array([], dtype=float64)],
[array([], dtype=float64), array([], dtype=float64),
array([], dtype=float64)]], dtype=object)
This works, but I wouldn't say it's pretty (or recommended).
Filling with None is prettier, but even then we have to make it a list:
In [103]: marr.filled([None])
Out[103]:
array([[None, array([0, 1, 2, 3, 4]), array([0, 1, 2])],
[None, array([0, 1]), None]], dtype=object)
The function "filled" has be supplied with value to be filled for masked part.
import numpy as np
arr = np.array([
[np.arange(10), np.arange(5), np.arange(3)],
[np.arange(1), np.arange(2), np.array([])],
])
marr = np.ma.array(arr)
marr.mask = [[True, False, False],
[True, False, True]]
marr.fill_value = np.array([])
marr.filled(2)
This version of code is not giving that error.

Categories

Resources