numpy数据类型dtype转换实现


Posted in Python onApril 24, 2021

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

导入numpy

>>> import numpy as np

一、随便玩玩

生成一个浮点数组

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

看看信息

>>> a
array([ 0.0945377 ,  0.52199916,  0.62490646,  0.21260126])
>>> a.dtype
dtype('float64')
>>> a.shape
(4,)

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

>>> 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,数组长度再次翻倍!

>>> 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

>>> a.dtype = 'float'
>>> a
array([ 0.0945377 ,  0.52199916,  0.62490646,  0.21260126])
>>> a.shape
(4,)
>>> a.dtype
dtype('float64')

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

>>> a.dtype = 'int64'
>>> a
array([4591476579734816328, 4602876970018897584, 4603803876586077261,
       4596827787908854048], dtype=int64)
>>> a.shape
(4,)

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

>>> a.dtype = 'int32'
>>> a
array([ 1637779016,  1069036447, -1764917584,  1071690807,  -679822259,
        1071906619, -1611419360,  1070282372])
>>> a.shape
(8,)

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

>>> a.dtype = 'int16'
>>> a
array([-31160,  24990,  13215,  16312,  32432, -26931, -19401,  16352,
       -17331, -10374,   -197,  16355, -20192, -24589,  13956,  16331], dtype=int16)
>>> a.shape
(16,)

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

>>> a.dtype = 'int8'
>>> a
array([  72, -122,  -98,   97,  -97,   51,  -72,   63,  -80,  126,  -51,
       -106,   55,  -76,  -32,   63,   77,  -68,  122,  -41,   59,   -1,
        -29,   63,   32,  -79,  -13,  -97, -124,   54,  -53,   63], dtype=int8)
>>> a.shape
(32,)

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

>>> a.dtype = 'int'
>>> a.dtype
dtype('int32')
>>> a
array([ 1637779016,  1069036447, -1764917584,  1071690807,  -679822259,
        1071906619, -1611419360,  1070282372])
>>> a.shape
(8,)

二、换一种玩法

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

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

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

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

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

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

>>> 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转换内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python 时间操作例子和时间格式化参数小结
Apr 24 Python
Python随机生成一个6位的验证码代码分享
Mar 24 Python
简单学习Python time模块
Apr 29 Python
一个Python最简单的接口自动化框架
Jan 02 Python
使用apidocJs快速生成在线文档的实例讲解
Feb 07 Python
python简单商城购物车实例代码
Mar 15 Python
Python 25行代码实现的RSA算法详解
Apr 10 Python
Pycharm 设置自定义背景颜色的图文教程
May 23 Python
python实现AES加密与解密
Mar 28 Python
使用python模拟高斯分布例子
Dec 09 Python
20行Python代码实现一款永久免费PDF编辑工具的实现
Aug 27 Python
python如何调用百度识图api
Sep 29 Python
解决python存数据库速度太慢的问题
Apr 23 #Python
python实战之90行代码写个猜数字游戏
Apr 22 #Python
python实战之一步一步教你绘制小猪佩奇
Apr 22 #Python
python 破解加密zip文件的密码
python入门之算法学习
Apr 22 #Python
python使用XPath解析数据爬取起点小说网数据
Apr 22 #Python
python 实现德洛内三角剖分的操作
You might like
PHP 身份证号验证函数
2009/05/07 PHP
php基于websocket搭建简易聊天室实践
2016/10/24 PHP
实例:尽可能写友好的Javascript代码
2006/10/09 Javascript
JS获取整个页面文档的实现代码
2011/12/15 Javascript
jquery图片延迟加载 前端开发技能必备系列
2012/06/18 Javascript
IE的fireEvent方法概述及应用
2013/02/22 Javascript
javascript获取元素CSS样式代码示例
2013/11/28 Javascript
JavaScript的strict模式与with关键字介绍
2014/02/08 Javascript
Jquery中国地图热点效果-鼠标经过弹出提示层信息的简单实例
2014/02/12 Javascript
使用CSS3的scale实现网页整体缩放
2014/03/18 Javascript
jQuery淡入淡出元素让其效果更为生动
2014/09/01 Javascript
jQuery中position()方法用法实例
2015/01/16 Javascript
Angular中ng-bind和ng-model的区别实例详解
2017/04/10 Javascript
jQuery实现按比例缩放图片的方法
2017/04/29 jQuery
js学习总结之DOM2兼容处理顺序问题的解决方法
2017/07/27 Javascript
highcharts.js数据绑定方式代码实例
2019/11/13 Javascript
[36:19]2018DOTA2亚洲邀请赛 小组赛 A组加赛 Newbee vs LGD
2018/04/03 DOTA
Python中一些自然语言工具的使用的入门教程
2015/04/13 Python
关于Python中浮点数精度处理的技巧总结
2017/08/10 Python
python自定义函数实现一个数的三次方计算方法
2019/01/20 Python
python3操作注册表的方法(Url protocol)
2020/02/05 Python
python yield和Generator函数用法详解
2020/02/10 Python
Python网页解析器使用实例详解
2020/05/30 Python
Python中的全局变量如何理解
2020/06/04 Python
python3读取autocad图形文件.py实例
2020/06/05 Python
澳大利亚领先的亚麻品牌:Bed Threads
2019/12/16 全球购物
新员工入职感言
2014/02/01 职场文书
公务员转正鉴定材料
2014/02/11 职场文书
销售活动策划方案
2014/08/26 职场文书
2014年营销工作总结
2014/11/22 职场文书
迟到检讨书范文
2015/01/27 职场文书
六一儿童节开幕词
2015/01/29 职场文书
经费申请报告范文
2015/05/18 职场文书
同乡会致辞
2015/07/30 职场文书
Axios取消重复请求的方法实例详解
2021/06/15 Javascript
数据设计之权限的实现
2022/08/05 MySQL