0
点赞
收藏
分享

微信扫一扫

NumPy数据对象1 —— numpy.ndarray类

三千筱夜 2022-03-20 阅读 36

1 N维数组

NumPy使用ndarray作为N维数组类型,来描述相同类型的元素的“集合”。元素的类型是相同的,占用相同的内存大小。元素可以通过索引提取对应元素的Python对象。

2 ndarray类

class numpy.ndarray(shape, 
					dtype=float, 
					buffer=None, 
					offset=0, 
					strides=None, 
					order=None)

2.1 参数(Parameters)

  • shape: tuple of ints,创造的数组的形状大小
  • dtype: data-type, optional,元素的数据类型
  • buffer: object exposing buffer interface, optional,缓冲区数据接口,用于填充数组
  • offset: int, optional,缓冲区中数组数据向后偏移的字节数
  • strides: tuple of ints, optional,内存中数据的移动
  • order: {‘C’, ‘F’}, optional,C 行优先,F 列优先
>>>np.arange(24).reshape(3,8)
>array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23]])

# shape参数, buffer参数
>>>x = np.ndarray((3,2), 
				buffer=np.arange(24).reshape(3,8), 
)
>>>x
>array([[0, 1],
       [2, 3],
       [4, 5]])

# dtype参数
>>>x = np.ndarray((3,2), 
				buffer=np.arange(24).reshape(3,8), 
				dtype=float
)
>>>x.dtype
>dtype('float64')

# order参数
>>>x = np.ndarray((3,2), 
				buffer=np.arange(24).reshape(3,8), 
				order="C"
)
>>>x
>array([[0, 1],
       [2, 3],
       [4, 5]])
>>>x = np.ndarray((3,2), 
				buffer=np.arange(24).reshape(3,8), 
				order="F"
)
>>>x
>array([[0, 3],
       [1, 4],
       [2, 5]])

# offset参数
>>>x = np.ndarray((3,2), 
				buffer=np.arange(24).reshape(3,8), 
				offset=8,
				order="C"
)
>>>x
>array([[2, 3],
       [4, 5],
       [6, 7]])

# strides参数
>>>x = np.ndarray((3,2), 
				buffer=np.arange(24).reshape(3,8), 
				strides=(16,16),
				order="C"
)
>>>x
>array([[ 0,  4],
       [ 4,  8],
       [ 8, 12]])

2.2 属性(Attributes)

  1. obj.T: ndarray类型,返回转置数组
  2. obj.data: buffer,返回数组开始地址
  3. obj.dtype: 数据类型对象,数组元素的数据类型
  4. obj.flags: dict类型,数组内存布局相关信息
  5. obj.flat: 迭代器,返回数组的一维迭代器
  6. obj.imag: ndarray类型,返回数组的虚部
  7. obj.real: ndarray类型,返回数组的实部
  8. obj.size: int类型,返回元素个数
  9. obj.itemsize: int类型,返回一个元素的字节长度
  10. obj.nbytes: int类型,全部元素的字节长度
  11. obj.ndim: int类型,返回数组维度
  12. obj.shaoe: tuple of ints,返回维度的元组
  13. obj.strides: 遍历数组时每个维度中步进的字节数元组
  14. obj.ctypes: 一个简化数组与ctypes模块交互的对象。
  15. obj.base: ndarray类型,如果数组来源于其它对象,则返回原数组,即buffer指向的数组

2.3 方法(Methods)

  1. obj.all(axis=None, out=None, keepdims=False, *, where=True)
    全部元素为True,返回True
    axis为默认值None,全部元素执行and操作,axis为int or tuple,按照对应轴执行and操作;
    out备用输出数组,与预期输出数组形状相同,并保持自身的数据类型不变。
    keepdims为True时保持数组的维度,缩小是轴保留尺寸为1。
  2. obj.any(axis=None, out=None, keepdims=False, *, where=True)
    任一元素为True,返回True
  3. obj.argmax(axis=None, out=None, keepdims=False)
    返回最大值的下标
  4. obj.argmax(axis=None, out=None, keepdims=False)
    返回最小值的下标
  5. obj.argsort(axis)
    返回排序后的下标数组
  6. obj.astype(dtype)
    复制并返回一个指定数据类型的数组
  7. obj.clip(min, max,out)
    返回数组,将元素值限定在一定范围内
  8. obj.compress(condition, axis, out
    返回给定轴组成的数组
  9. obj.cumprod(axis)
    返回指定轴累乘的数组
  10. obj.cumsum(axis)
    返回指定轴累加的数组
  11. obj.copy()
    返回复制的数组
  12. obj.fill(value)
    使用指定的value值填充数组
  13. obj.flatten()
    将数组展开为一维数组并返回
  14. obj.getfield(dtype)
    以特定类型返回给定数组的字段
  15. obj.item(*args)
    返回数组指定位置的元素值
  16. obj.itemset(position, value)
    将value插入到数组指定位置
  17. obj.max(axis=None, out=None, keepdims=False, *, where=True)
    返回指定维度的最大值
  18. obj.mean(axis=None, out=None, keepdims=False, *, where=True)
    返回指定维度的平均值
  19. obj.min(axis=None, out=None, keepdims=False, *, where=True)
    返回指定维度的最小值
  20. obj.nonzero()
    返回数组中非零元素的索引
  21. obj.prod(axis)
    返回指定轴元素的乘积
  22. obj.ptp(axis)
    返回指定轴元素最大值减最小值的结果
  23. obj.put(indices,value)
    给数组指定位置设置指定值
  24. obj.ravel()
    返回展开为一维的数组
  25. obj.repeat(repeats, axis)
    重复数组元素
  26. obj.reshape(shape)
    返回一个新的数组,重新设定形状
  27. obj.resize(new_shape)
    返回一个新尺寸的数组
  28. obj.round()
    返回四舍五入的结果
  29. obj.searchsorted(v)
    返回元素在有序数组中应该插入的位置
  30. obj.sort(axis)
    数组原地排序
  31. obj.std(axis, dtype)
    返回标准差
  32. obj.sum(axis, dtype)
    返回和
  33. obj.trace()
    返回数组对角元素和
  34. obj.transpose()
    返回数组的转置
  35. obj.std(axis, dtype)
    返回数组方差
举报

相关推荐

0 条评论