脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - numpy数据类型dtype转换实现

numpy数据类型dtype转换实现

2021-10-19 08:56罗兵 Python

这篇文章主要介绍了numpy数据类型dtype转换实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

这篇文章我们玩玩numpy的数值数据类型转换

导入numpy

?
1
>>> import numpy as np

一、随便玩玩

生成一个浮点数组

?
1
>>> a = np.random.random(4)

看看信息

?
1
2
3
4
5
6
>>> a
array([ 0.0945377 0.521999160.624906460.21260126])
>>> a.dtype
dtype('float64')
>>> a.shape
(4,)

改变dtype,发现数组长度翻倍!

?
1
2
3
4
5
6
7
>>> a.dtype = 'float32'
>>> a
array([  3.65532693e+20,   1.43907535e+00-3.31994873e-25,
         1.75549972e+00-2.75686653e+14,   1.78122652e+00,
        -1.03207532e-19,   1.58760118e+00], dtype=float32)
>>> a.shape
(8,)

改变dtype,数组长度再次翻倍!

?
1
2
3
4
5
6
7
8
9
10
>>> a.dtype = 'float16'
>>> a
array([ -9.58442688e-05,   7.19000000e+02,   2.38159180e-01,
         1.92968750e+00,              nan,  -1.66034698e-03,
        -2.63427734e-01,   1.96875000e+00-1.07519531e+00,
        -1.19625000e+02,              nan,   1.97167969e+00,
        -1.60156250e-01-7.76290894e-03,   4.07226562e-01,
         1.94824219e+00], dtype=float16)
>>> a.shape
(16,)

改变dtype='float',发现默认就是float64,长度也变回最初的4

?
1
2
3
4
5
6
7
>>> a.dtype = 'float'
>>> a
array([ 0.0945377 0.521999160.624906460.21260126])
>>> a.shape
(4,)
>>> a.dtype
dtype('float64')

把a变为整数,观察其信息

?
1
2
3
4
5
6
>>> a.dtype = 'int64'
>>> a
array([4591476579734816328, 4602876970018897584, 4603803876586077261,
       4596827787908854048], dtype=int64)
>>> a.shape
(4,)

改变dtype,发现数组长度翻倍!

?
1
2
3
4
5
6
>>> a.dtype = 'int32'
>>> a
array([ 16377790161069036447, -17649175841071690807-679822259,
        1071906619, -16114193601070282372])
>>> a.shape
(8,)

改变dtype,发现数组长度再次翻倍!

?
1
2
3
4
5
6
>>> a.dtype = 'int16'
>>> a
array([-3116024990132151631232432, -26931, -1940116352,
       -17331, -10374,   -19716355, -20192, -245891395616331], dtype=int16)
>>> a.shape
(16,)

改变dtype,发现数组长度再次翻倍!

?
1
2
3
4
5
6
7
>>> a.dtype = 'int8'
>>> a
array([  72, -122-98,   97-97,   51-72,   63-80126-51,
       -106,   55-76-32,   63,   77-68122-41,   59,   -1,
        -29,   63,   32-79-13-97, -124,   54-53,   63], dtype=int8)
>>> a.shape
(32,)

改变dtype,发现整数默认int32!

?
1
2
3
4
5
6
7
8
>>> a.dtype = 'int'
>>> a.dtype
dtype('int32')
>>> a
array([ 16377790161069036447, -17649175841071690807-679822259,
        1071906619, -16114193601070282372])
>>> a.shape
(8,)

二、换一种玩法

很多时候我们用numpy从文本文件读取数据作为numpy的数组,默认的dtype是float64。
但是有些场合我们希望有些数据列作为整数。如果直接改dtype='int'的话,就会出错!原因如上,数组长度翻倍了!!!

下面的场景假设我们得到了导入的数据。我们的本意是希望它们是整数,但实际上是却是浮点数(float64)

?
1
2
3
>>> b = np.array([1., 2., 3., 4.])
>>> b.dtype
dtype('float64')

用 astype(int) 得到整数,并且不改变数组长度

?
1
2
3
4
5
6
7
>>> c = b.astype(int)
>>> c
array([1, 2, 3, 4])
>>> c.shape
(8,)
>>> c.dtype
dtype('int32')

如果直接改变b的dtype的话,b的长度翻倍了,这不是我们想要的(当然如果你想的话)

?
1
2
3
4
5
6
7
8
9
10
11
>>> b
array([ 1.2.3.4.])
 
>>> b.dtype = 'int'
>>> b.dtype
dtype('int32')
>>> b
array([         0, 1072693248,          0, 1073741824,          0,
       1074266112,          0, 1074790400])
>>> b.shape
(8,)

三、结论

numpy中的数据类型转换,不能直接改原数据的dtype!  只能用函数astype()。

到此这篇关于numpy数据类型dtype转换实现的文章就介绍到这了,更多相关numpy dtype转换内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/hhh5460/p/5129032.html

延伸 · 阅读

精彩推荐