Python ndim在numpy的工作

Python ndim在numpy的工作,python,numpy,Python,Numpy,我已经给出了一个由3个变量组成的方程,这意味着它是一个3维,但它把维表示为1。谁能告诉我ndim的逻辑吗?正如上面所说,numpy.ndim(a)返回: a中的维度数。标量是零维的 e、 g: 请注意,最后一个数组d是对象dtype的数组,因此它的维度仍然是1 您想要使用的可以是a.shape(或者a.size用于一维数组): 方法.shape返回一个元组,您应该使用[0]获取维度: print a.size, b.size print c.size # == 4, which is the t

我已经给出了一个由3个变量组成的方程,这意味着它是一个3维,但它把维表示为1。谁能告诉我ndim的逻辑吗?

正如上面所说,
numpy.ndim(a)
返回:

a
中的维度数。标量是零维的

e、 g:

请注意,最后一个数组
d
是对象
dtype
的数组,因此它的维度仍然是
1

您想要使用的可以是
a.shape
(或者
a.size
用于一维数组):

方法
.shape
返回一个
元组
,您应该使用
[0]
获取维度:

print a.size, b.size
print c.size # == 4, which is the total number of elements in the array
#outputs:
1 2
4

要让您了解2维的概念,请尝试
np.array([[1,2],[3,4]]).ndim
,也许您正在寻找
a.shape
a = np.array(111)
b = np.array([1,2])
c = np.array([[1,2], [4,5]])
d = np.array([[1,2,3,], [4,5]])
print a.ndim, b.ndim, c.ndim, d.ndim
#outputs: 0 1 2 1
print a.size, b.size
print c.size # == 4, which is the total number of elements in the array
#outputs:
1 2
4
print a.shape, b.shape, b.shape[0]
() (2L,) 2