Let’s take a look at all the data types of array with n dimension.
import numpy as np
print((set(np.typeDict.values())))
'''{<class 'numpy.complex64'>, <class 'numpy.uint16'>, <class 'numpy.bytes_'>, <class 'numpy.float16'>, <class 'numpy.clongdouble'>, <class 'numpy.int32'>, <class 'numpy.timedelta64'>, <class 'numpy.complex128'>, <class 'numpy.uintc'>, <class 'numpy.int64'>, <class 'numpy.void'>, <class 'numpy.longdouble'>, <class 'numpy.uint8'>, <class 'numpy.uint32'>, <class 'numpy.int16'>, <class 'numpy.uint64'>, <class 'numpy.intc'>, <class 'numpy.int8'>, <class 'numpy.object_'>, <class 'numpy.float64'>, <class 'numpy.bool_'>, <class 'numpy.float32'>, <class 'numpy.datetime64'>, <class 'numpy.str_'>}'''
类型 | 简写 | 解释 |
---|---|---|
int8、uint8 | i1、u1 | 有符号和无符号的8位(1个字节)整型 |
int16、uint16 | i2、u2 | 有符号和无符号的16位(2个字节)整型 |
int32、uint32 | i4、u4 | 有符号和无符号的32位(4个字节)整型 |
int64、uint64 | i8、u8 | 有符号和无符号的64位(8个字节)整型 |
float16 | f2 | 半精度浮点数 |
float32 | f4 或 f | 标准的单精度浮点数。与C语言中的 float 兼容 |
float64 | f8 或 d | 标准的双精度浮点数。与C语言中的 double 和 python 中的 float 对象兼容 |
float128 | f16 或 g | 扩展精度浮点数 |
complex64、complex128、complex 256 | c8、c16、c32 | 分别用 两个 32位、64位或128位浮点数表示复数 |
bool | ? | 存储 True 和 False 值的布尔类型 |
object | O | python 对象 类型 |
string_ | S | 固定长度的字符串类型(每个字符1个字节): 如果要创建一个长度为 10 的字符串,应对应 S10 |
unicode_ | U | 固定长度的unincode类型(字节数由平台决定): 如果要创建一个长度为 10 的字符串,并且每个字符是1个字节的话,应对应 U10 |
1. dtype的两个作用
- 指定数据类型
–arr = np.array(range(10, 20), dtype = 'i2')
- 查看数据类型
arr.dtype
2. 转换数据类型
arr = arr.astype(dtype)
:注意需要接收返回值
import numpy as np
dtypeArr = np.array(range(10, 15), dtype = 'i8')
print(dtypeArr, dtypeArr.dtype)
# [10 11 12 13 14] int64
dtypeArr = dtypeArr.astype('f')
print(dtypeArr, dtypeArr.dtype)
# [10. 11. 12. 13. 14.] float32