ndarray基础

未匹配的标注

numpy中最重要的对象—ndarray:

Ndarray对象指的是用于存放同类型元素的多维数据,它是一个多维容器,N代表着它的维度

创建ndarray对象

  • 通过array方法创建
    • 参数说明
      • 必选参数
        • object 数组或嵌套的数列
      • 可选参数
        • dtype 数组元素的数据类型
        • copy 对象是否需要复制
        • order 创建数组的样式,C为行方向,F为列方向,A为任意方向(默认)
        • subok 默认返回一个与基类类型一致的数组
        • ndmin 指定生成数组的最小维度
    • demo
      >>> from numpy as np
      >>> x = np.array([[1, 2, 3], [4, 5, 6]],dtype=np.int32,copy=True,order=None,subok=False,ndmin=0)
      >>> type(x) # 查看x类型
      <type 'numpy.ndarray'>
      >>> x.shape # 查看ndarray对象的维度
      (2, 3)
      >>> x.dtype # 查看x里的数据类型
      dtype('int32')
  • 通过zeros/ones方法创建(创建指定大小的数组,数组元素以 0/1 来填充,)
    • 参数说明
      • 必要参数
        • shape 数组形状
      • 可选参数
        • dtype 数据类型
        • order ‘C’ 用于 C 的行数组,或者 ‘F’ 用于 FORTRAN 的列数组
    • demo
      >>> np.zeros(5)
      array([0., 0., 0., 0., 0.])
      >>> np.zeros((3,3))
      array([[0., 0., 0.],
             [0., 0., 0.],
             [0., 0., 0.]])
  • 通过empty方法创建(创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组:)
    • 参数说明
      • 必要参数
        • shape 数组形状
      • 可选参数
        • dtype 数据类型
        • 有”C”和”F”两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序
    • demo
      >>> x = np.empty((3,2),dtype=int)
      >>> x
      array([[0, 0],
            [0, 0],
            [0, 0]])
      >>> y = np.empty([3,2],dtype=int)
      >>> y
      array([[0, 0],
             [0, 0],
             [0, 0]])
  • 其他方法:
    • 通过full方法创建(创建一个填充给定值的n * n数组)
    • demo
      >>> np.full([3,3],3)
      array([[3, 3, 3],
             [3, 3, 3],
             [3, 3, 3]])
    • 通过eye方法创建(创建一个对角线是1,其余是0的多维数组)
    • demo
      >>> np.eye(3)
      array([[1., 0., 0.],
             [0., 1., 0.],
             [0., 0., 1.]])
      >>> np.eye(1)
      array([[1.]])
    • 通过linspace方法创建(创建一个在指定的时间间隔内返回均匀间隔的数字的数组)
    • demo
      >>> np.linspace(0,8.8,num=5)
      array([0. , 2.2, 4.4, 6.6, 8.8])
    • 通过random方法创建(创建一个填充0到1之间随机值的数组)
    • demo
      >>> np.random.random([3,3])
      array([[0.17647511, 0.79086009, 0.26275058],
             [0.83484953, 0.6386956 , 0.53928901],
             [0.26020885, 0.58836421, 0.39308341]])

NumPy支持的数据类型(ndarray对象支持的数据类型)

名称 描述
bool_ 布尔型数据类型(True 或者 False)
int_ 默认的整数类型(类似于 C 语言中的 long,int32 或 int64)
intc 与 C 的 int 类型一样,一般是 int32 或 int 64
intp 用于索引的整数类型(类似于 C 的 ssize_t,一般情况下仍然是 int32 或 int64)
int8 字节(-128 to 127)
int16 整数(-32768 to 32767)
int32 整数(-2147483648 to 2147483647)
int64 整数(-9223372036854775808 to 9223372036854775807)
uint8 无符号整数(0 to 255)
uint16 无符号整数(0 to 65535)
uint32 无符号整数(0 to 4294967295)
uint64 无符号整数(0 to 18446744073709551615)
float_ float64 类型的简写
float16 半精度浮点数,包括:1 个符号位,5 个指数位,10 个尾数位
float32 单精度浮点数,包括:1 个符号位,8 个指数位,23 个尾数位
float64 双精度浮点数,包括:1 个符号位,11 个指数位,52 个尾数位
complex_ complex128 类型的简写,即 128 位复数
complex64 复数,表示双 32 位浮点数(实数部分和虚数部分)
complex128 复数,表示双 64 位浮点数(实数部分和虚数部分)

ndarray对象的属性

属性 说明
ndarray.ndim 数组的轴(维度)的个数。在Python世界中,维度的数量被称为rank。
ndarray.shape 数组的维度。这是一个整数的元组,表示每个维度中数组的大小。对于有n行和m列的矩阵,shape将是(n,m)。因此,shape元组的长度就是rank或维度的个数 ndim。
ndarray.size 数组元素的总数。这等于shape的元素的乘积。
ndarray.dtype 一个描述数组中元素类型的对象。可以使用标准的Python类型创建或指定dtype。另外NumPy提供它自己的类型。例如numpy.int32、numpy.int16和numpy.float64。
ndarray.itemsize 数组中每个元素的字节大小。例如,元素为 float64 类型的数组的 itemsize 为8(=64/8),而 complex32 类型的数组的 itemsize 为4(=32/8)。它等于 ndarray.dtype.itemsize 。
ndarray.flags ndarray 对象的内存信息
ndarray.real ndarray元素的实部
ndarray.imag ndarray 元素的虚部
ndarray.data 该缓冲区包含数组的实际元素。通常,我们不需要使用此属性,因为我们将使用索引访问数组中的元素。

demo

  >>> a = np.full((3,3),3)
  >>> a
  array([[3, 3, 3],
         [3, 3, 3],
         [3, 3, 3]])
  >>> a.ndim
  2
  >>> a.shape
  (3, 3)
  >>> a.size
  9
  >>> a.dtype
  dtype('int32')
  >>> a.itemsize
  4
  >>> a.flags
    C_CONTIGUOUS : True
    F_CONTIGUOUS : False
    OWNDATA : True
    WRITEABLE : True
    ALIGNED : True
    WRITEBACKIFCOPY : False
    UPDATEIFCOPY : False
  >>> a.real
  array([[3, 3, 3],
         [3, 3, 3],
         [3, 3, 3]])
  >>> a.imag
  array([[0, 0, 0],
         [0, 0, 0],
         [0, 0, 0]])
  >>> a.data
  <memory at 0x0FE0E990>

本文章首发在 LearnKu.com 网站上。

上一篇 下一篇
讨论数量: 0
发起讨论 查看所有版本


暂无话题~