1. numpy 的基本数据类型
类型名 | 类型表示符 |
---|---|
布尔型 | bool |
有符号整数型 | int8 / int16 / int32 / int64 |
无符号整数型 | uint8 / uint16 / uint32 / uint64 |
浮点型 | float16 / float32 / float64 |
复数型 | complex64 / complex128 |
字符型 | str,每个字符用 32 位 Unicode 编码表示 |
import numpy as np
arr = np.array([1, 2, 3])
print(arr, arr.dtype)
arr = arr.astype('int64')
print(arr, arr.dtype)
arr = arr.astype('float32')
print(arr, arr.dtype)
arr = arr.astype('bool')
print(arr, arr.dtype)
arr = arr.astype('str')
print(arr, arr.dtype)
2. numpy 自定义复合数据类型
如果希望 ndarray 中存储对象类型,numpy 建议使用元组存储对象的属性字段值,然后把元组添加到 ndarray 中,ndarray 提供了语法方便处理这些数据。
import numpy as np
data = [
('zs', [99, 98, 90], 17),
('ls', [95, 95, 92], 16),
('ww', [97, 92,