一、NumPy数组属性

文章目录

      • ndarray属性表
      • ndarray.ndim
      • ndarray.shape
      • ndarray.reshape(shape)
      • ndarray.size
      • ndarray.dtype
      • ndarray.astype(dtype)
      • ndarray.itemsize
      • ndarray.flags
      • ndarray.real
      • ndarray.imag

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

在 NumPy中,轴 (axis) 一般是在超过一维数组时作用更大的属性:

  • 一维数组时,axis=0
  • 二维数组时,axis=0, 1
  • N 维数组时,axis=0, 1, …, N

当对某一轴进行操作时,我们可以这样看待:

  • 一维数组,axis=0 表示沿着第 0 轴进行操作,即跨列操作;
  • 二维数组,axis=0 表示沿着第 0 轴进行操作,即跨行操作;axis=1 表示沿着第 1 轴进行操作,即跨列操作
  • 三维数组,axis=0 表示沿着第 0 轴进行操作,即跨表操作;axis=1 表示沿着第 1 轴进行操作,即跨行操作;axis=2 表示沿着第 2 轴进行操作,即跨列操作

ndarray属性表

属性 说明
ndarray.ndim 秩 (rank),即数组的轴(维度)的个数
ndarray.shape 数组形状,返回一个元组表示各个维度中元素的个数
ndarray.size 数组元素的总数,相当于np.prod(ndarray.shape)
ndarray.dtype 数组中每个元素的数据类型和数据 (bit) 大小
ndarray.itemsize 数组中每个元素的字节 (byte) 大小,1 byte=8 bit
ndarray.flags 数组的内存信息
ndarray.real 数组元素的实部
ndarray.imag 数组元素的虚部

ndarray.ndim

ndarray.ndim 返回秩 (rank),即数组的轴(维度)的个数

import numpy as np 
a = np.arange(24)  
print (a.ndim)			# a 现只有一个维度
# 现在调整其大小
b = a.reshape(2,4,3)	# b 现在拥有三个维度
print (b.ndim)

# 输出
1
3

ndarray.shape

ndarray.shape 数组形状,返回一个元组表示各个维度中元素的个数。比如,一个二维数组,其数组形状为 (行数, 列数)

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

# 输出
(2, 3)

ndarray.shape 也可以用于调整数组结构,但要保证元素的总个数 (size) 不能变

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

# 输出
[[1 2]
 [3 4]
 [5 6]]

ndarray.reshape(shape)

也可以使用 ndarray.reshape() 函数来调整数组结构,但要保证元素的总个数 (size) 不能变,会创建一个新对象,不会改变原对象

import numpy as np 
a=np.array([[1,2,3],[4,5,6]]) 
b=a.reshape(3,2)  
print (b)

# 输出
[[1, 2] 
 [3, 4] 
 [5, 6]]

ndarray.size

ndarray.size 返回数组元素的总数,相当于np.prod(ndarray.shape)

import numpy as np 
x=np.zeros((3,5,2),dtype=np.complex128)
print(x.size)
print(np.prod(x.shape))

# 输出
30
30

ndarray.dtype

ndarray.dtype 返回数组中每个元素的数据类型和数据 (bit) 大小

# 查询dtype
arr1=np.array([1,2,3],dtype=np.float64)
print(arr1.dtype)

# 输出
float64

ndarray.astype(dtype)

ndarray.astype(dtype) 转换数组的数据类型,会创建一个新对象,不会改变原对象

# 浮点数数组转换整数数组,小数点后的部分将被省略
arr1=np.array([3.7,-1.2,-2.6,0.5,12.9,10.1])
print(arr1)
print(arr1.dtype)
int_arr1=arr1.astype(np.int32)
print(int_arr1)
print(int_arr1.dtype)
# 直接使用另一个数组的类型
arr2=np.array([1,2,3,4,5])
astype_arr=arr1.astype(arr2.dtype)
print(astype_arr)

# 输出
[ 3.7 -1.2 -2.6  0.5 12.9 10.1]
float64
[ 3 -1 -2  0 12 10]
int32
[ 3 -1 -2  0 12 10]
# 把浮点数含义的Unicode字符串转换为浮点数(注:不能直接转换为整型)
num_strings=np.array([1.25,-9.6,42],dtype=np.str)
print(num_strings)
print(num_strings.dtype)
num_floats=num_strings.astype(np.float)
print(num_floats)
print(num_floats.dtype)

# 输出
['1.25' '-9.6' '42']
<U4
[ 1.25 -9.6  42.  ]
float64

ndarray.itemsize

ndarray.itemsize 返回数组中每个元素的字节 (byte) 大小

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

import numpy as np
# x数组的dtype为int8(一个字节)  
x = np.array([1,2,3,4,5], dtype = np.int8)  
print (x.itemsize)
# y数组的dtype为float64(八个字节) 
y = np.array([1,2,3,4,5], dtype = np.float64)  
print (y.itemsize)

# 输出
1
8

ndarray.flags

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

属性 描述
C_CONTIGUOUS © 数据是在一个单一的C风格的连续段中
F_CONTIGUOUS (F) 数据是在一个单一的Fortran风格的连续段中
OWNDATA (O) 数组拥有它所使用的内存或从另一个对象中借用它
WRITEABLE (W) 数据区域可以被写入,将该值设置为 False,则数据为只读
ALIGNED (A) 数据和所有元素都适当地对齐到硬件上
WRITEBACKIFCOPY(X) 该数组是其他数组的副本。
必须先调用C-API函数 PyArray_ResolveWritebackIfCopy,然后再使用该数组的内容更新分配给基本数组的内容。
UPDATEIFCOPY (U) 这个数组是其它数组的一个副本,当这个数组被释放时,原数组的内容将被更新
import numpy as np 
x = np.array([1,2,3,4,5])  
print (x.flags)

# 输出
  C_CONTIGUOUS : True
  F_CONTIGUOUS : True
  OWNDATA : True
  WRITEABLE : True
  ALIGNED : True
  WRITEBACKIFCOPY : False
  UPDATEIFCOPY : False

ndarray.real

ndarray.real 返回数组元素的实部

arr2=[1+2j,3+4j,5+6j]
complex_arr=np.array(arr2)
print(complex_arr.dtype)
print(complex_arr.real)

# 输出
complex128
[1. 3. 5.]

ndarray.imag

ndarray.imag 返回数组元素的虚部

arr2=[1+2j,3+4j,5+6j]
complex_arr=np.array(arr2)
print(complex_arr.dtype)
print(complex_arr.imag)

# 输出
complex128
[2. 4. 6.]

你可能感兴趣的:(NumPy,numpy,数据分析)