Lesson4——NumPy 数组属性和方法

NumPy 教程目录

  NumPy 数组的维数称为秩(rank),秩就是轴的数量,即数组的维度,一维数组的秩为 1,二维数组的秩为 2,以此类推。

  在 NumPy中,每一个线性的数组称为是一个轴(axis),也就是维度(dimensions)。比如说,二维数组相当于是两个一维数组,其中第一个一维数组中每个元素又是一个一维数组。所以一维数组就是 NumPy 中的轴(axis),第一个轴相当于是底层数组,第二个轴是底层数组里的数组。而轴的数量——秩,就是数组的维数。

  很多时候可以声明 axis。

    • axis=0,表示沿着第 0 轴进行操作,即对每一列进行操作;
    • axis=1,表示沿着第1轴进行操作,即对每一行进行操作。

  NumPy 的数组中比较重要 ndarray 对象属性有:

属性说明
ndarray.ndim 秩,即轴的数量或维度的数量
ndarray.shape 数组的维度,对于矩阵,n 行 m 列
ndarray.size 数组元素的总个数,相当于 .shape 中 n*m 的值
ndarray.dtype ndarray 对象的元素类型
ndarray.itemsize ndarray 对象中每个元素的大小,以字节为单位
ndarray.flags ndarray 对象的内存信息
ndarray.real ndarray元素的实部
ndarray.imag ndarray 元素的虚部
ndarray.data 包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要使用这个属性。
ndarray.flat 数组上的一维迭代器。
ndarray.base Base object if memory is from some other object.

属性

numpy.ndarray.ndim

  Examples:返回轴数。

>>> x = np.array([1, 2, 3])
>>> x.ndim
1
>>> y = np.zeros((2, 3, 4))
>>> y.ndim
3

ndarray.shape

  ndarray.shape 表示数组的维度,返回一个元组,这个元组的长度就是维度的数目,即 ndim 属性(秩)。比如,一个二维数组,其维度表示"行数"和"列数"。

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

(2, 3) #元组

  类似的  numpy.shape : Return the shape of an array.

np.shape(np.eye(3))
(3, 3)
np.shape([[1, 3]])
(1, 2)
np.shape([0])
(1,)
np.shape(0)
()

a = np.array([(1, 2), (3, 4), (5, 6)],
             dtype=[('x', 'i4'), ('y', 'i4')])
np.shape(a)
(3,)
a.shape
(3,)

  使用 ndarray.shape 调整数据大小:

a = np.array([[1,2,3],[4,5,6]]) 
a.shape =  (3,2)  
print (a)
#输出结果为:
[[1 2]
 [3 4]
 [5 6]]

  类似的:NumPy 也提供了 reshape 函数来调整数组大小。

a = np.array([[1,2,3],[4,5,6]]) 
b = a.reshape(3,2)  
print (b)
#输出结果为:
[[1, 2] 
 [3, 4] 
 [5, 6]]

ndarray.itemsize

  ndarray.itemsize 以字节的形式返回数组中每一个元素的大小。

  例如,一个元素类型为 float64 的数组 itemsize 属性值为 8  (float64 占用 64 个 bits,每个字节长度为 8,所以 64/8,占用 8 个字节),又如,一个元素类型为 complex32 的数组 item 属性为 4(32/8)。

# 数组的 dtype 为 int8(一个字节)  
x = np.array([1,2,3,4,5], dtype = np.int8)  
print (x.itemsize)
 
# 数组的 dtype 现在为 float64(八个字节) 
y = np.array([1,2,3,4,5], dtype = np.float64)  
print (y.itemsize)
#输出结果
1
8

ndarray.flags

  ndarray.flags 返回 ndarray 对象的内存信息,包含以下属性:

属性描述
C_CONTIGUOUS (C) 数据是在一个单一的C风格的连续段中
F_CONTIGUOUS (F) 数据是在一个单一的Fortran风格的连续段中
OWNDATA (O) 数组拥有它所使用的内存或从另一个对象中借用它
WRITEABLE (W) 数据区域可以被写入,将该值设置为 False,则数据为只读
ALIGNED (A) 数据和所有元素都适当地对齐到硬件上
UPDATEIFCOPY (U) 这个数组是其它数组的一个副本,当这个数组被释放时,原数组的内容将被更新

numpy.ndarray.base

  返回 Base Object.

  Examples:

x = np.array([1,2])
print(x.base)
y=x[1:]
print(y.base) #返回的是 x 中的数据,有点像 copy
print(y.base is x) 
#输出结果
None
[1 2]
True

numpy.ndarray.flat

  返回 ndarray 的一维迭代器。

  Examples:

x= np.arange(6).reshape((2,3))
print(x)
for i in x.flat:
    print(i,end=' ')
print()
print(type(x.flat))
#输出结果
[[0 1 2]
 [3 4 5]]
0 1 2 3 4 5 
<class 'numpy.flatiter'>

numpy.ndarray.T

  返回转置数组。

  Examples:

x = np.array([[1.,2.],[3.,4.]])
print('原始数组:\n',x)
print('转置数组:\n',x.T)
x = np.array([1.,2.,3.,4.])
print('原始数组:\n',x)
print('转置数组:\n',x.T)
#输出结果
原始数组:
 [[1. 2.]
 [3. 4.]]
转置数组:
 [[1. 3.]
 [2. 4.]]
原始数组:
 [1. 2. 3. 4.]
转置数组:
 [1. 2. 3. 4.]

numpy.ndarray.real & numpy.ndarray.imag

  • numpy.ndarray.real 返回 ndarray 的实部
  • numpy.ndarray.imag 返回 ndarray 的虚部

  Example1:

x = np.array([1.,2.,3.,4.])
print(x)
print(x.real)
print(x.imag)
#输出结果
[1. 2. 3. 4.]
[1. 2. 3. 4.]
[0. 0. 0. 0.]

  Example2:

x = np.array([1+1j,2,3,4])
print(x)
print(x.real)
print(x.imag)
#输出结果
[1.+1.j 2.+0.j 3.+0.j 4.+0.j]
[1. 2. 3. 4.]
[1. 0. 0. 0.]

  同样可以通过 .real  和 .imag 修改元素的实部或者虚部。

  Examples3:

x = np.array([1+1j,2,3,4])
print(x)
print(x.real)
print(x.imag)
#通过x.real修改所有元素的实部
x.real = 9
print(x)
#通过x.imag修改所有元素的实部
x = np.array([1+1j,2,3,4])
x.imag = 7
print(x)
#输出结果
[1.+1.j 2.+0.j 3.+0.j 4.+0.j]
[1. 2. 3. 4.]
[1. 0. 0. 0.]
[9.+1.j 9.+0.j 9.+0.j 9.+0.j]
[1.+7.j 2.+7.j 3.+7.j 4.+7.j]

  对于实部和虚部,存在:

  • np.real(x):返回实部
  • np.imag(x):返回虚部

  Examples:

print(np.real(x))
print(np.imag(x))
#输出结果
[1. 2. 3. 4.]
[7. 7. 7. 7.]
posted @ 2022-02-07 18:56  多发Paper哈  阅读(226)  评论(0编辑  收藏  举报
Live2D