Numpy的学习笔记(二)- Numpy的数据类型

       当知道如何创建 ndarray(Numpy的数组)之后,来看看它里面到底可以放些什么样的数据。这里引入了在Python数组里非常重要的一个概念:dType。Numpy通过dType来显示的指定数组中的数据到底是什么类型的。例如

import Numpy as np

# 用dtype来指定

darrary = np.array(np.arange(9), dtype='S1')

print(darrary)

print(darrary.dtype)

'''

Output:

['0' '1' '2' '3' '4' '5' '6' '7' '8']

|S1

'''

这段代码构建了一个一维数组。正常来讲,np.arange所产生的序列是数值类型的。但是这里经过指定dtype=’S’,将数字类型的数据转化成了字符串类型S1。那dtype到底可以指定多少种数据类型呢?

 

基本数据类型

Numpy支持24个基本类型的数据,具体情况可查阅https://numpy.org/devdocs/reference/c-api/dtype.html 。它包括布尔型(bool), 整形(int32),浮点型等等(float32)。可以通过三种方式来指定:

import Numpy as np

# 通过类似int32这种方式来指定

darrary = np.array(np.arange(4), dtype='int32')

print(darrary)

print(darrary.dtype)

'''

Output:

[0 1 2 3]

int32

'''



darrary = np.array(np.arange(4), dtype='bool')

print(darrary)

print(darrary.dtype)

'''

Output:

[False  True  True  True]

bool

'''



darrary = np.array(np.arange(4), dtype='float32')

print(darrary)

print(darrary.dtype)

'''

Output:

[0. 1. 2. 3.]

float32

'''



# 通过np.int32这种方式来指定

darrary = np.array(np.arange(4), dtype=np.int32)

print(darrary)

print(darrary.dtype)

'''

Output:

[0 1 2 3]

int32

'''



darrary = np.array(np.arange(4), dtype=np.bool)

print(darrary)

print(darrary.dtype)

'''

Output:

[False  True  True  True]

bool

'''



darrary = np.array(np.arange(4), dtype=np.float32)

print(darrary)

print(darrary.dtype)

'''

Output:

[0. 1. 2. 3.]

float32

'''

# 通过类型的简化版,来指定。这种情况,只有整数和浮点数支持。

darrary = np.array(np.arange(4), dtype='i4')

print(darrary)

print(darrary.dtype)

'''

Output:

[0 1 2 3]

int32

'''



darrary = np.array(np.arange(4), dtype='f4')

print(darrary)

print(darrary.dtype)

'''

Output:

[0. 1. 2. 3.]

float32

'''

其中比较令人疑惑的是i4, f4这样的写法。 其实ix是整数类型的一个缩写,其中i表示int, x表示数字所占的字节数。fx就相应的代表浮点数类型的缩写。缩写和实际类型的对应关系如下:

字符表示实际类型字符表示实际类型
i1int8f2float16
i2int16f4float32
i4int32f8float64
i8int64f16float128

 

 

 

 

 

 

字符串类型

除了上面的基本类型之外,dtype还可以接受字符串类型的指定。 比如文章一开始提到的S1。 这里S表示字符串,1表示长度(如果不是,请更正我)。

import Numpy as np

# 指定dtype 为S1

darray = np.array(['a', 'b'], dtype='S1')

print(darray.dtype)

'''

Output:

|S1

'''

# 从下面这个例子可以看出,S后面的数字,与数组中最长的字符串的长度是一致的

darray = np.array(['Hello', 'Numpy!'])

print(darray.dtype)

'''

Output:

|S6

'''

# 这里有一个有趣的现象,即便数组中的字符串超过了我们指定的S的大小,系统依然不会报错。但是系统也不会按照数组中最长字符串的长度来改变dtype的类型

darray = np.array(['Hello', 'Numpy!'], dtype='S1')

print(darray.dtype)

'''

Output:

|S1

'''

指定object类型

在Python中的object与json的结构惊人的相似,也是有键值对(Key-Value)的方式来构建的,例如:

import Numpy as np

person = {'name' : "三儿", 'age' : 37, 'gendar' : '男', 'married' : False}

print(type(person))

'''

Output:

<type 'dict'>

'''

如果想把类似于person这样的object当成元素放到Numpy的数组中去,就需要按照下面的方式指定dtype:

import Numpy as np

custom_dtype = np.dtype([('name' , 'S20'), ('age' , 'i1'), ('gendar' , 'S1'), ('married' , 'bool')])

parray = [("三儿", 37, '男', False), ("五儿", 22, '女', False)]

darray = np.array(parray, dtype = custom_dtype)

print(darray[:]['age'])

'''

Output:

[37, 22]

'''

可见在dtype指定一个复杂的object类型时,是通过元组的方式来实现的。想要知道数组元素的数据类型,也可以使用np.dtype来查看。 更多dtype的相关内容,可以参考https://numpy.org/devdocs/reference/arrays.dtypes.html?highlight=dtype

<<< Numpy的学习笔记(一)  Numpy的学习笔记(三) >>>

 

 

 

 

 

 

 

 

 

 

 

 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值