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 正则表达式(转义问题)
Dec 15 Python
Python Tkinter GUI编程入门介绍
Mar 10 Python
Python中字典的基础知识归纳小结
Aug 19 Python
Python连接PostgreSQL数据库的方法
Nov 28 Python
Python实现Mysql数据库连接池实例详解
Apr 11 Python
Python+Socket实现基于UDP协议的局域网广播功能示例
Aug 31 Python
Python with语句上下文管理器两种实现方法分析
Feb 09 Python
django用户注册、登录、注销和用户扩展的示例
Mar 19 Python
pygame游戏之旅 如何制作游戏障碍
Nov 20 Python
Python3 串口接收与发送16进制数据包的实例
Jun 12 Python
Pycharm新手教程(只需要看这篇就够了)
Jun 18 Python
python全栈要学什么 python全栈学习路线
Jun 28 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下将XML转换为数组
2010/01/01 PHP
PHP在线生成二维码代码(google api)
2013/06/03 PHP
PHP实现的mongoDB数据库操作类完整实例
2018/04/10 PHP
jQuery 相关控件的事件操作分解
2009/08/03 Javascript
JQuery之拖拽插件实现代码
2011/04/14 Javascript
Extjs4.0设置Ext.data.Store传参的请求方式(默认为GET)
2013/04/02 Javascript
使用GruntJS构建Web程序之合并压缩篇
2014/06/06 Javascript
angularJS 中input示例分享
2015/02/09 Javascript
辨析JavaScript中的Undefined类型与null类型
2016/05/26 Javascript
基于JavaScript实现前端文件的断点续传
2016/10/17 Javascript
jquery插入兄弟节点的操作方法
2016/12/07 Javascript
微信小程序 下拉列表的实现实例代码
2017/03/08 Javascript
Vue 项目代理设置的优化
2018/04/17 Javascript
通过npm或yarn自动生成vue组件的方法示例
2019/02/12 Javascript
vue vant Area组件使用详解
2019/12/09 Javascript
15个简单的JS编码标准让你的代码更整洁(小结)
2020/07/16 Javascript
[01:04:05]VG vs Newbee 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
[49:17]DOTA2-DPC中国联赛 正赛 Phoenix vs Dynasty BO3 第三场 1月26日
2021/03/11 DOTA
Python greenlet实现原理和使用示例
2014/09/24 Python
Python实现将n个点均匀地分布在球面上的方法
2015/03/12 Python
Python中struct模块对字节流/二进制流的操作教程
2017/01/21 Python
Python爬虫包BeautifulSoup简介与安装(一)
2018/06/17 Python
简单了解Python生成器是什么
2019/07/02 Python
python科学计算之scipy——optimize用法
2019/11/25 Python
python去除删除数据中\u0000\u0001等unicode字符串的代码
2020/03/06 Python
如何在windows下安装Pycham2020软件(方法步骤详解)
2020/05/03 Python
使用Python实现微信拍一拍功能的思路代码
2020/07/09 Python
Python+Selenium实现自动化的环境搭建的步骤(图文)
2020/09/01 Python
pandas将list数据拆分成行或列的实现
2020/12/13 Python
纽约手袋品牌:KARA
2018/03/18 全球购物
美国领先的个性化礼品商城:Personalization Mall
2019/07/27 全球购物
node中使用shell脚本的方法步骤
2021/03/23 Javascript
会计专业应届生自荐信
2014/02/07 职场文书
火车来了教学反思
2014/02/11 职场文书
酒店爱岗敬业演讲稿
2014/09/02 职场文书
中学生自我评价范文
2015/03/03 职场文书