numpy学习(3)
?? ????? —数组属性
<1>-----------------数组的大小--------------------------- 代码:
ndarray.shape
例子: 例1-------------------------------------------- 输入:
import numpy as np
a = np.array([[1,2],[4,6],[4,6]])
print(a.shape)
输出:
(3, 2)
例2-------------------------------------------- 输入:
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a)
a.shape = (1, 6)
print(a)
输出:
[[1 2 3]
[4 5 6]]
[[1 2 3 4 5 6]]
<2>------------------------数组的维数----------------------------- 代码:
ndarray.ndim
例子: 例1-------------------------------------------- 输入:
import numpy as np
a = np.arange(24)
print(a)
print(a.ndim)
a.shape = (2,12)
print(a)
print(a.ndim)
输出:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23]
1
[[ 0 1 2 3 4 5 6 7 8 9 10 11]
[12 13 14 15 16 17 18 19 20 21 22 23]]
2
<3>-----------每个元素的字节单位长度----------------------------- 代码:
numpy.itemsize
例子: 例1-------------------------------------------- 输入:
import numpy as np
a = np.array([1, 2, 3, 4, 5], dtype=np.int8)
print(a.itemsize)
a = np.array(a, dtype=np.float64)
print(a.itemsize)
输出:
1
8
|