数组性质之一是只能存储同一种数据类型,因此可以通过dtype属性获取数组中的元素的数据类型,也可以在创建数组时通过dtype指定数组的数据类型。
下表是ndarray.dtype的常用的数据类型:
| 数据类型 | 描述 | 唯一标识符 |
|---|---|---|
| bool | 用一个字节存储的布尔类型(True或False) | ‘b’ |
| int8 | 一个字节大小,-128 至 127 | ‘i1’ |
| int16 | 整数,16 位整数(-32768 ~ 32767) | ‘i2’ |
| int32 | 整数,32 位整数(-2147483648 ~ 2147483647) | ‘i4’ |
| int64 | 整数,64 位整数(-9223372036854775808 ~ 9223372036854775807) | ‘i8’ |
| uint8 | 无符号整数,0 至 255 | ‘u1’ |
| uint16 | 无符号整数,0 至 65535 | ‘u2’ |
| uint32 | 无符号整数,0 至 2 ** 32 - 1 | ‘u4’ |
| uint64 | 无符号整数,0 至 2 ** 64 - 1 | ‘u8’ |
| float16 | 半精度浮点数:16位,正负号1位,指数5位,精度10位 | ‘f2’ |
| float32 | 单精度浮点数:32位,正负号1位,指数8位,精度23位 | ‘f4’ |
| float64 | 双精度浮点数:64位,正负号1位,指数11位,精度52位 | ‘f8’ |
| complex64 | 复数,分别用两个32位浮点数表示实部和虚部 | ‘c8’ |
| complex128 | 复数,分别用两个64位浮点数表示实部和虚部 | ‘c16’ |
| object_ | python对象 | ‘O’ |
| string_ | 字符串 | ‘S’ |
| unicode_ | unicode类型 | ‘U’ |
Numpy中关于数值的类型比Python内置的多得多,这是因为Numpy为了能高效处理处理海量数据而设计的。举个例子,比如想要存储上百亿的数字,并且这些数字都不超过254(一个字节内),我们就可以将dtype设置为int8,这样就比默认使用int64更能节省内存空间了。
获取数组属性示例:
object类型
#object类型(python对象)
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
d = np.array([Person('阿福',13),Person('阿宇',56)])
print(d)
print(d.dtype) #数据类型为object(python对象)
[<__main__.Person object at 0x000001761EC4A9A0> <__main__.Person object at 0x000001761EC4AA60>] object
int32
import numpy as np a1 = np.arange(10) print(a1) print(a1.dtype)
[0 1 2 3 4 5 6 7 8 9] int32
import numpy as np a2 = np.array([1,2,3,4]) print(a2) print(a2.dtype)
[1 2 3 4] int32
np.arange()生成的数组和通过python列表生成的数组的元素默认是int32类型,可以通过dtype改变其数据的类型。
指定数据类型的方法有两种,一是numpy.数据类型(例如np.int32),这里object类和unicode类有点特殊,np.object和np.unicode都是弃用别名,使用会报警,可分别用object和np.compat.unicode替代。二是使用唯一标识符(例如int32类型可以表示为dtype='i4'),
注意: 纯数字类型的数组可以随意指定为其他类型的数组,但是string_、unicode、object则不能指定为数字类型的数组。
通过dtype指定数组元素数据类型:
int32转换为int8
#指定数据类型 import numpy as np a2 = np.array([1,2,3,4],dtype=np.int8) #a2 = np.array([1,2,3,4],dtype='i1') # 通过唯一标识符指定,效果一样 print(a2) print(a2.dtype)
[1 2 3 4] int8
int32转换为object类型
import numpy as np a3 = np.array([1,2,3,4],dtype='O') a3 = np.array([1,2,3,4],dtype=object) # np.object是弃用别名,使用会出现警告,直接使用自身名字即可 print(a3) print(a3.dtype)
[1 2 3 4] object
int32转化为string_类型
import numpy as np a4 = np.array([1,2,3,4],dtype=np.string_) #a4 = np.array([1,2,3,4],dtype='S') # 唯一标识符 print(a4) print(a4.dtype)
[b'1' b'2' b'3' b'4'] |S1
int32转换为unicode类型
import numpy as np a2 = np.array([1,2,3,4],dtype='U') # a2 = np.array([1,2,3,4],dtype=np.compat.unicode) # np.unicode也是弃用别名,使用会有警告,可使用np.compat.unicode替代 print(a2) print(a2.dtype)
['1' '2' '3' '4']如果数组元素都是字符类型,默认是unicode类型
#unicode类型 import numpy as np r = np.array(['w','rr','qs']) print(r) print(r.dtype)['w' 'rr' 'qs'] #unicode类型 import numpy as np rs = np.array(['w','rr','qs'],dtype='U') print(rs) print(rs.dtype)['w' 'rr' 'qs']string_(字符串类型)
#string_(字符串类型) import numpy as np t = np.array(['w','rr','q'],dtype='S') print(t) print(t.dtype)[b'w' b'rr' b'q'] |S2unicode转换为int32,报ValueError错误
import numpy as np rs = np.array(['w','rr','q'],dtype=np.int32) print(rs) print(rs.dtype)--------------------------------------------------------------------------- ValueError Traceback (most recent call last)in 1 #unicode类型 ----> 2 rs = np.array(['w','rr','q'],dtype=np.int32) 3 print(rs) 4 print(rs.dtype) ValueError: invalid literal for int() with base 10: 'w' 通过astype 修改dtype类型
注意:不会改变原数组的数据类型,而是将修改后的结果返回
import numpy as np #修改dtype a3 = np.array([4,2,4,1,2,3,6]) print(a3.dtype) #默认为int32 #修改dtype类型 a4 = a3.astype(np.int8) # astype不会修改数组本身,即a3还是int32类型,而是会将修改后的结果返回,a4返回修改后的int8类型 print(a4.dtype) print(a3.dtype) # 原数组元素数据类型不变int32 int8 int322. ndarray.ndim查看数组维数
判断维数方法:从第一个" [ “开始到第一个元素,有多少个” [ "就是多少维。
import numpy as np b1 = np.array([1,2,3]) print(b1.ndim) b2 = np.array([[1,2,3], [2,3,4]]) print(b2.ndim) b3 = np.array([ [ [1,2,3], [3,4,5], [6,7,8] ] ]) print(b3.ndim) b4 = np.array([ [ [1,2,3], [4,5,6], [3,7,6] ], [ [7,8,9], [10,11,12], [12,35,6] ] ]) print(b4.ndim)1 2 3 33. shape()、reshape()和flatten()shape查看数组是几行几列(形状)
import numpy as np b1 = np.array([1,2,3]) b2 = np.array([[1,2,3], [2,3,4]]) b3 = np.array([ [ [1,2,3], [3,4,5], [6,7,8] ] ]) b4 = np.array([ [ [1,2,3], [4,5,6], [3,7,6] ], [ [7,8,9], [10,11,12], [12,35,6] ] ]) print(b1.shape) # 输出(3,),意思是一维数组,有3个数据 print(b2.shape) # 输出(2,3),意思是二维数组,2行3列 print(b3.shape) #输出(1,3,3),意思是三维数组,总共有1个元素,每个元素是3行3列 print(b4.shape) #输出(2,3,3),意思是三维数组,总共有2个元素,每个元素是3行3列(当不同元素的行列数不相同是会报错)(3,) (2, 3) (1, 3, 3) (2, 3, 3)reshape:重新修改数组的维数。
flatten():将n维数组变为一维数组
注意:
1.reshape并不会修改原来数组本身,而是会将修改后的结果返回。如果想要直接修改数组本身,那么可以使用resize来替代reshape。
2.reshape()改变原数组形状后,新数组的元素个数要和原数组一致。
import numpy as np c1 = np.arange(12) #生成一个有12个数据的一维数组 print(c1) print("="*20) c2 = c1.reshape((3,4)) #变成一个2维数组,是3行4列的 print(c2) print("="*20) c3 = c1.reshape((2,3,2)) ##变成一个3维数组,总共有2块(第一个参数),每一块是2行2列的(三维数组中数的数量要和一维数组中数的数量要一致) print(c3) print("="*20) c4 = c2.reshape((12,)) #将ac的二维数组重新变成一个12列的1维数组 print(c4) print("="*20) c5 = c2.flatten() # 不管c2是几维数组,都将他变成一个一维数组 print(c5) print("="*20) c6 = c3.flatten() # 不管c3是几维数组,都将他变成一个一维数组 print(c6)[ 0 1 2 3 4 5 6 7 8 9 10 11] ==================== [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] ==================== [[[ 0 1] [ 2 3] [ 4 5]] [[ 6 7] [ 8 9] [10 11]]] ==================== [ 0 1 2 3 4 5 6 7 8 9 10 11] ==================== [ 0 1 2 3 4 5 6 7 8 9 10 11] ==================== [ 0 1 2 3 4 5 6 7 8 9 10 11]4. ndarray.size获取数组中总的元素的个数
import numpy as np d1 = np.array([[1,2,3],[3,4,5]]) print(d1.size) #打印的是6,因为该二维数组总共有6个元素 d2 = np.array([1,2,3]) print(d2.size) #打印的是3,因为该一维数组总共有3个元素 d3 = np.array([ [ [1,2,3], [4,5,6], [3,7,6] ], [ [7,8,9], [10,11,12], [12,35,6] ] ]) print(d3.size) #打印的是18,因为该三维数组总共有18个元素6 3 185.ndarray.itemsize数组中每个元素占内存的大小,单位是字节(byte)
import numpy as np e1 = np.array([1,2,3],dtype=np.int32) print(e1.dtype) print(e1.itemsize) # 打印4,因为每个字节是8位,32位/8=4个字节 print("="*20) e2 = np.array([1,2,3,4],dtype=np.int16) print(e2.dtype) print(e2.itemsize) # 打印2,因为每个字节是8位,16位/8=2个字节 print("="*20) e3 = np.array([1,2,3,4,],dtype=np.int8) print(e3.dtype) print(e3.itemsize) # 打印1,因为每个字节是8位,8位/8=1个字节int32 4 ==================== int16 2 ==================== int8 1



