检测 numpy 数组的维数(不是形状)

Detect number of dimensions of numpy array (not shape)

获取两个不同 numpy 数组的形状 returns 元组

a.shape
Out[131]: (3,)

A.shape
Out[132]: (3, 3)

基于元组,一个是一维数组(维数=1),一个是2d。我如何检测类似于 type(A) 告诉我其中一个是 numpy.ndarray 的维数?我应该只使用 len(a.shape) 吗?

你应该使用 numpy.ndarray.ndim。所以

a.ndim # gives 1

A.ndim # gives 2