一、NumPy Ndarry对象

array(object, dtype, copy, order, subok, ndmin)
  • object:数组或嵌套的数列
  • dtype:数组元素的数据类型,可选
  • copy:对象是否需要复制,可选
  • order:创建数组的样式,C为行方向,F为列方向,A为任意方向(默认)
  • subok:默认返回一个与基类类型一致的数组
  • ndmin: 指定生成数组的最小维度
a = np.array([1, 2, 3, 4, 5], ndmin=3)  # 多个维度,nadmin=维度数
print(a)

b = np.array([1, 2, 3], dtype=complex) #complex:复数

 

二、NumPy 数组属性和数据类型

(1)numpy.dtype(object,align,copy)
  •   object:要转换为的数据类型对象
  •   align:如果为 true,填充字段使其类似 C 的结构体。
  •   copy:复制 dtype 对象 ,如果为 false,则是对内置数据类型对象的引用
# 使用标量类型
dt = np.dtype(np.int32)
print(dt)  # int32

# int8, int16, int32, int64 四种数据类型可以使用字符串 'i1', 'i2','i4','i8' 代替

dt = np.dtype('i4')
print(dt)  # int32

# 字节顺序标注
dt = np.dtype('<i4')
print(dt)  # int32

# 首先创建结构化数据类型
dt = np.dtype([('age', np.int8)])
print(dt)  # [('age', 'i1')]

# 将数据类型应用于 ndarray 对象
dt = np.dtype([('age', np.int8)])
a = np.array([(10,), (20,), (30,)], dtype=dt)
print(a)  # [(10,) (20,) (30,)]

# 类型字段名可以用于存取实际的 age 列
dt = np.dtype([('age', np.int8)])
a = np.array([(10,), (20,), (30,)], dtype=dt)
print(a['age'])  # [10 20 30]

student = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')])
print(student)  # [('name', 'S20'), ('age', 'i1'), ('marks', 'f4')]

student = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')])  # S20:长度为20的字符串,i1:字节,int8,f2:2位浮点型(偶数)
a = np.array([('abc', 21, 50), ('xyz', 18, 75)], dtype=student)
print(a)  # [('abc', 21, 50.0), ('xyz', 18, 75.0)]
  • b 布尔型
  • i (有符号) 整型
  • u 无符号整型 integer
  • f 浮点型
  • c 复数浮点型
  • m timedelta(时间间隔)
  • M datetime(日期时间)
  • O (Python) 对象
  • S, a (byte-)字符串
  • U Unicode
  • V 原始数据 (void)
(2)ndarry.shape
  ndarray.shape 表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。比如,一个二维数组,其维度表示"行数"和"列数"。
  ndarray.shape 也可以用于调整数组大小。
a = np.array([[1, 2, 3], [4, 5, 6]])
print(a.shape)  # (2, 3) 2行3列

a = np.array([[1, 2, 3], [4, 5, 6]])
a.shape = (3, 2)  # [[1 2] [3 4] [5 6]]
print(a)

# NumPy 也提供了 reshape 函数来调整数组大小。返回的是非拷贝副本,改变返回后数字的元素,原数组对应元素的值也会改变

a = np.array([[1, 2, 3], [4, 5, 6]])
b = a.reshape(3, 2)
print(b)  # [[1 2] [3 4] [5 6]]
(3)ndarray.itemsize
ndarray.itemsize 以字节的形式返回数组中每一个元素的大小。
例如,一个元素类型为 float64 的数组 itemsize 属性值为 8(float64 占用 64 个 bits,每个字节长度为 8,所以 64/8,占用 8 个字节),又如,一个元素类型为 complex32 的数组 item 属性为 4(32/8)。
x = np.array([1, 2, 3, 4, 5], dtype=np.int8)
print(x.itemsize)  # 1

y = np.array([1, 2, 3, 4, 5], dtype=np.float64)
print(y.itemsize)  # 8
(4)ndarray.flags 返回 ndarray 对象的内存信息,包含以下属性:
x = np.array([1, 2, 3, 4, 5])
print(x.flags)
  • C_CONTIGUOUS (C) 数据是在一个单一的C风格的连续段中
  • F_CONTIGUOUS (F) 数据是在一个单一的Fortran风格的连续段中
  • OWNDATA (O) 数组拥有它所使用的内存或从另一个对象中借用它
  • WRITEABLE (W) 数据区域可以被写入,将该值设置为 False,则数据为只读
  • ALIGNED (A) 数据和所有元素都适当地对齐到硬件上
  • UPDATEIFCOPY (U) 这个数组是其它数组的一个副本,当这个数组被释放时,原数组的内容将被更新
三、Numpy 创建数组
(1)numpy.empty
numpy.empty 方法用来创建一个指定形状(shape)、数据类型(dtype)且未初始化的数组:
  numpy.empty(shape, dtype = float, order = 'C')
  •   shape 数组形状
  •   dtype 数据类型,可选
  •   order 有"C"和"F"两个选项,分别代表,行优先和列优先,在计算机内存中的存储元素的顺序。
import numpy as np 
x = np.empty([3,2], dtype = int) 
print (x)
'''
[[ 6917529027641081856  5764616291768666155]
 [ 6917529027641081859 -5764598754299804209]
 [          4497473538      844429428932120]]
'''
(2)numpy.zeros
  创建指定大小的数组,数组元素以 0 来填充:
  •   numpy.zeros(shape, dtype = float, order = 'C')
  •   shape 数组形状
  •   dtype 数据类型,可选
  •   order 'C' 用于 C 的行数组,或者 'F' 用于 FORTRAN 的列数组
import numpy as np
 
# 默认为浮点数
x = np.zeros(5) 
print(x)
 
# 设置类型为整数
y = np.zeros((5,), dtype = np.int) 
print(y)
 
# 自定义类型
z = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')])  
print(z)

(3)numpy.ones

创建指定形状的数组,数组元素以 1 来填充:
  • numpy.ones(shape, dtype = None, order = 'C')
  • shape 数组形状
  • dtype 数据类型,可选
  • order 'C' 用于 C 的行数组,或者 'F' 用于 FORTRAN 的列数组
import numpy as np
 
# 默认为浮点数
x = np.ones(5) 
print(x)
 
# 自定义类型
x = np.ones([2,2], dtype = int)
print(x)
'''
[1. 1. 1. 1. 1.]
[[1 1]
 [1 1]]
 '''