Check If Numpy Array Has A Normal Shape
How do I check if a numpy array has a regular shape. In the example below x is a *2 by 3* matrix. However y is not regular in the sense that it can't be represented as a proper mat
Solution 1:
Both are arrays and those are valid shapes. But, with normal, think you meant that each element has the same shape and length across it. For that, a better way would be to check for the datatype. For the variable length case, it would be object. So, we can check for that condition and call out accordingly. Hence, simply do -
defis_normal_arr(a): # a is input array to be testedreturn a.dtype isnot np.dtype('object')
Solution 2:
I think the .shape method is capable of checking it.
If you input an array which can form a matrix it returns it's actual shape, (2, 3) in your case. If you input an incorrect matrix it returns something like (2,), which says something's wrong with the second dimension, so it can't form a matrix.
Post a Comment for "Check If Numpy Array Has A Normal Shape"