Skip to content Skip to sidebar Skip to footer

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.

Solution 3:

Here y is a one-dimensional array and the size of y is 2. y contains 2 list values. AND x is our actual matrix in a proper format.

check the dimensions by y.ndim AND x.ndim.

Post a Comment for "Check If Numpy Array Has A Normal Shape"