- ndarray属性
| 属性 | 说明 |
|---|---|
| ndarray.nidm | 秩,即轴的数量或维度的数量 |
| ndarray.shape | 数组的维度,对于矩阵,n行m列 |
| ndarray.size | 数组的元素的总个数,相当于.shape中n*m的值 |
| ndarray.dtype | ndarray对象的元素类型 |
| ndarray.itemsize | ndarray对象中每个元素的大小,以字节为单位 |
| ndarray.flags | ndarray对象的内存信息 |
| ndarray.raal | ndarray元素的实部 |
| ndarray.imag | ndarray元素的虚部 |
| ndarray.data | 包括实际数组元素的缓冲区,由于一般通过数组的索引获取元素,通常不用这个属性 |
- ndarray属性测试
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.random.randint(4, 10, size=(2, 3))
c = np.random.randn(2, 3, 4)
# admin属性
print('ndim:', a.ndim, b.ndim, c.ndim) # ndim: 1 2 3
# shape属性
print('shape:', a.shape, b.shape, c.shape) # shape: (4,) (2, 3) (2, 3, 4)
# dtype属性
print('dtype:', a.dtype, b.dtype, c.dtype) # dtype: int32 int32 float64
# size属性 元素总个数
print('size:', a.size, b.size, c.size) # size: 4 6 24
# itemsize属性 每个元素所占的字节
print('itemsize:', a.itemsize, b.itemsize, c.itemsize) # itemsize: 4 4 8
- 其他方式创建数组
- linespace
- logspace
# linespace左右都是包括的 # endpoint默认是True x = np.linspace(1, 10, 10) x = np.linspace(5, 50, 5, endpoint=False) # logspace等比数列 # def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0): x = np.logspace(0, 9, 10, base=2) print(x)



