7.2 NumPy数组
7.2.1 NumPy 数值类型
NumPy支持的数值类型比Python多得多。本节展示哪些可用,以及如何修改数组的数据类型。
所支持的基本类型与C语言中的基本类型紧密相连:
NumPy类型
C语言类型
描述
np.bool
bool
Boolean (True or False) stored as a byte
np.byte
signed char
Platform-defined
np.ubyte
unsigned char
Platform-defined
np.short
short
Platform-defined
np.ushort
unsigned short
Platform-defined
np.intc
int
Platform-defined
np.uintc
unsigned int
Platform-defined
np.int_
long
Platform-defined
np.uint
unsigned long
Platform-defined
np.longlong
long long
Platform-defined
np.ulonglong
unsigned long long
Platform-defined
np.half / np.float16
Half precision float: sign bit, 5 bits exponent, 10 bits mantissa
np.single
float
Platform-defined single precision float: typically sign bit, 8 bits exponent, 23 bits mantissa
np.double
double
Platform-defined double precision float: typically sign bit, 11 bits exponent, 52 bits mantissa.
np.longdouble
long double
Platform-defined extended-precision float
np.csingle
float complex
Complex number, represented by two single-precision floats (real and imaginary components)
np.cdouble
double complex
Complex number, represented by two double-precision floats (real and imaginary components).
np.clongdouble
long double complex
Complex number, represented by two extended-precision floats (real and imaginary components).
由于其中许多具有平台相关的定义,因此提供了一组固定大小的别名:
NumPy类型
C type
Description
np.int8
int8_t
Byte (-128 to 127)
np.int16
int16_t
Integer (-32768 to 32767)
np.int32
int32_t
Integer (-2147483648 to 2147483647)
np.int64
int64_t
Integer (-9223372036854775808 to 9223372036854775807)
np.uint8
uint8_t
Unsigned integer (0 to 255)
np.uint16
uint16_t
Unsigned integer (0 to 65535)
np.uint32
uint32_t
Unsigned integer (0 to 4294967295)
np.uint64
uint64_t
Unsigned integer (0 to 18446744073709551615)
np.intp
intptr_t
Integer used for indexing, typically the same as ssize_t
np.uintp
uintptr_t
Integer large enough to hold a pointer
np.float32
float
np.float64 / np.float_
double
Note that this matches the precision of the builtin python float.
np.complex64
float complex
Complex number, represented by two 32-bit floats (real and imaginary components)
np.complex128 / np.complex_
double complex
Note that this matches the precision of the builtin python complex.
有5种基本的数值类型表示布尔值(bool)、整数(int)、无符号整数(uint)浮点数(float)和复数。这些数值对象类型中,有5种最基本的数值类型,分别是布尔(bool)、整数(int)、无符号整数(uint)、浮点数(float)和复数。
NumPy中的数据类型名称也是数据类型的构造函数的名称:
[]:x_1=np.int(1)
x_2=np.float32([1.0, 2.17, 3.14])
x_1, x_2
[]:(1, array([1. , 2.17, 3.14], dtype=float32))
可以使用arange函数来创建整数列:
[]:x_3=np.arange(4, dtype=np.uint8)
x_3
[]:array([0, 1, 2, 3], dtype=uint8)
dtype为创建的数值类型。
可以使用. .astype()方法或类型本身作为函数来对数组类型进行转换:
[]:x_3.astype(float) #将整型数组x_3转换为浮点数组x3
[]:array([0., 1., 2., 3.])
要确定数组的类型,可以查看dtype属性:
>>> z.dtype
dtype('uint8')
NumPy包含几个常量:
numpy.Inf
浮点数(正)无穷大的表示
numpy.NINF
浮点数负无穷大的表示
numpy.NAN
非数字表示
numpy.NZERO
负0的浮点数表示
numpy.e
欧拉常数,e2.71828182
numpy.pi
派,pi = 3.1415926535897932384626433...
NumPy使用IEEE二进制浮点运算标准(IEEE 754)。这意味着没有一个数不等于无穷大。正无穷不等于负无穷。但是无穷大等于正无穷大。
电气电子工程师学会(IEEE)的英文全称是the Institute of Electrical and Electronics Engineers,其前身是成立于1884年的美国电气工程师协会(AIEE)和成立于1912年的无线电工程师协会(IRE)。前者主要致力于有线通讯、光学以及动力系统的研究,而后者则是国际无线电领域不断扩大的产物。20世纪30年代,“电子学”这个词开始进入工程学词典。虽然许多工程师都同时是AIEE和IRE两个协会的会员,但是新入行的电子工程师们还是更倾向于加入无线电工程师协会。两个协会之间激烈的竞争的结果,造就了双方的合作与合并。1963年,AIEE和IRE宣布合并,电气电子工程师学会(IEEE)正式成立了。
作为全球最大的专业技术组织,在电气及电子工程、计算机、通信等领域中,IEEE 发表的技术文献占到了全球同类文献的百分之三十。同时IEEE每年还结集出版电气工程、通讯、计算机理论及方法领域的专业技术期刊,数量达140余册。配合各专业技术领域的学术交流活动,IEEE还提供学报、技术通讯、会议论文集和会刊等约700余种出版物。
如今,随着电子信息和其他相关技术的不断发展和演进,以及会员职业范围的扩大和技术兴趣的不断增长,IEEE也将其关注点不断扩充至其他新兴领域。IEEE将努力帮助其会员跟上并引领新技术发展步伐,制定国际和行业标准,并激励新一代专业技术人员架构并融入全球技术协作团队。更重要的是,随着会员所在地域范围的扩大,IEEE已经日趋全球化,并通过其会员的全球化,在航空航天、生物科技、计算机工程、新能源、地理信息系统、神经网络、无线通信等高科技领域的技术发展上走在了前沿。
7.2.2 ndarray类型
NumPy中核心数据对象类型是ndarray类型,也就是N-维数组对象。可以使用array()函数来创建ndarray对象:
[]: x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
x
[]:array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
使用ndarray的shape属性查看数组的形状
[]:x.shape
(3, 3)
若想创建特定数据类型的ndarray对象,可以将对象类型传入构造函数:
[]:x=np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], np.float)
x
[]:array([[1., 2., 3.],
[4., 5., 6.],
[7., 8., 9.]])
使用type()函数产看x的类型:
[]:type(x)
[]:numpy.ndarray
除了使用array函数来创建,也可以使用更底层的ndarray()构造函数来创建由固定大小的项组成的多维齐次数组。
[]:np.ndarray(shape=(4, 3), dtype=float)
[]:array([[1.047e-321, 0.000e+000, 0.000e+000],
[0.000e+000, 0.000e+000, 0.000e+000],
[0.000e+000, 0.000e+000, 0.000e+000],
[0.000e+000, 0.000e+000, 0.000e+000]])
Last updated
Was this helpful?