Numpy的数据类型
| 数据类型 | 描述 | 唯一标识符 |
|---|---|---|
| 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 |
import numpy as np
class Person:
def __init__(self,name,age):
self.name = name
self.age = age
#创建数组指定数据类型
a1 = np.array([1,2,3,4,5],dtype='i1')
a2 = np.array([1,2,3,4,5],dtype=np.int32)
d = np.array([Person('test1',18),Person('test2',20)])
#查询数据类型
print(a1)
print(a1.dtype)
print(a2)
print(a2.dtype)
print(d)
print(d.dtype)
#修改数据类型
a2 = a2.astype('f2')
print(a2)
print(a2.dtype)
[1 2 3 4 5]
int8
[1 2 3 4 5]
int32
[<__main__.Person object at 0x0000028CACDF5590>
<__main__.Person object at 0x0000028CACB284D0>]
object
[1. 2. 3. 4. 5.]
float16
小结: