代码:
import numpy as np
array = np.array([[1, 2, 3],
[2, 3, 4]]) # 数组
print(array)
print('number of dim:', array.ndim) # 数组维数
print('shape:', array.shape) # 形状
print('size', array.size) # 总共多少元素
运行结果:
[[1 2 3]
[2 3 4]]
number of dim: 2
shape: (2, 3)
size 6
代码:
x = np.array([1, 2, 3])
print(x)
print(x.ndim)
y = np.zeros((2, 3, 4))
print(y)
print(y.shape)
print(y.size)
print(y.ndim)
运行结果:
[1 2 3]
1
[[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]]
(2, 3, 4)
24
3
代码:
# # 三维
a = np.array([[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]],
[[4, 4, 4],
[5, 5, 5],
[6, 6, 6]]])
print(a.shape)
print(a.ndim)
运行结果:
(2, 3, 3)
3