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 相关文章推荐
linux 下实现python多版本安装实践
Nov 18 Python
python中map()函数的使用方法示例
Sep 29 Python
python多维数组切片方法
Apr 13 Python
pycharm 主题theme设置调整仿sublime的方法
May 23 Python
详解用python自制微信机器人,定时发送天气预报
Mar 25 Python
详解用python计算阶乘的几种方法
Aug 14 Python
python 子类调用父类的构造函数实例
Mar 12 Python
Python ArgumentParse的subparser用法说明
Apr 20 Python
python Gabor滤波器讲解
Oct 26 Python
python中_del_还原数据的方法
Dec 09 Python
python中@contextmanager实例用法
Feb 07 Python
一篇文章弄懂Python关键字、标识符和变量
Jul 15 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 随机生成10位字符代码
2009/03/26 PHP
一步一步学习PHP(4) php 函数 补充2
2010/02/15 PHP
PHP 无限分类三种方式 非函数的递归调用!
2011/08/26 PHP
php的POSIX 函数以及进程测试的深入分析
2013/06/03 PHP
PHP中加密解密函数与DES加密解密实例
2014/10/17 PHP
thinkphp5 加载静态资源路径与常量的方法
2017/12/24 PHP
JS,Jquery获取select,dropdownlist,checkbox下拉列表框的值(示例代码)
2014/01/11 Javascript
javascript对象的使用和属性操作示例详解
2014/03/02 Javascript
详谈javascript中的cookie
2015/06/03 Javascript
jQuery地图map悬停显示省市代码分享
2015/08/20 Javascript
Javascript基础_简单比较undefined和null 值
2016/06/14 Javascript
jQuery设置Easyui校验规则(推荐)
2016/11/21 Javascript
浅析Visual Studio Code断点调试Vue
2018/02/27 Javascript
VUE 全局变量的几种实现方式
2018/08/22 Javascript
jQuery实现的网站banner图片无缝轮播效果完整实例
2019/01/28 jQuery
你可能不知道的CORS跨域资源共享
2019/03/13 Javascript
[01:33:59]真人秀《加油 DOTA》 第六期
2014/09/09 DOTA
[00:43]TI7不朽珍藏III——幽鬼不朽展示
2017/07/15 DOTA
gearman的安装启动及python API使用实例
2014/07/08 Python
Python实现的异步代理爬虫及代理池
2017/03/17 Python
详解Django中CBV(Class Base Views)模型源码分析
2019/02/25 Python
Python学习笔记之抓取某只基金历史净值数据实战案例
2019/06/03 Python
Pytorch中Tensor与各种图像格式的相互转化详解
2019/12/26 Python
python操作gitlab API过程解析
2019/12/27 Python
Python正则表达式学习小例子
2020/03/03 Python
python pandas利用fillna方法实现部分自动填充功能
2020/03/16 Python
python合并多个excel文件的示例
2020/09/23 Python
python+excel接口自动化获取token并作为请求参数进行传参操作
2020/11/10 Python
pycharm 使用tab跳出正在编辑的括号(){}{}等问题
2021/02/26 Python
HTML5拖拽功能实现的拼图游戏
2018/07/31 HTML / CSS
美国第二大连锁药店:Rite Aid
2019/04/03 全球购物
秋季婚礼证婚词
2014/01/11 职场文书
全神贯注教学反思
2014/02/03 职场文书
关于青春的演讲稿
2014/05/05 职场文书
酒店管理失职检讨书
2014/09/16 职场文书
Ubuntu安装Mysql+启用远程连接的完整过程
2022/06/21 Servers