数据格式汇总及type, astype, dtype区别「建议收藏」

数据格式汇总及type, astype, dtype区别「建议收藏」标签(空格分隔):pythonuint8在此输入正文8位的无符号整形数据取值范围从0到255一singed与unsigned的区别二float改变类型643264to32shape翻倍改变类型321632to16shape翻倍改变类型32float32tofloatshape还原float默认是float64改变类型float64intfloat64

大家好,又见面了,我是你们的朋友全栈君。

标签(空格分隔): python


uint8:在此输入正文8位的无符号整形数据,取值范围从0到255

一、singed与unsigned的区别

1).计算机最小的存储单位是“位” 也就是bit或binary digits,用来存放一个二进制数,即 0或1, 8个二进制位为一个字节Byte。

2).对于16-bit(16位)的计算机,int是以两个字节来储存的,而32-bit的计算机,则是以4个字节,即32个bit来储存的。

如果想要明白singed与unsigned的区别,除了这两个基本知识,还需要了解整数在计算机中的存储方式,以16-bit 计算机为例,定义 int a = 1, 那么a的存储方式用表格来表示

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1

最左边的0是用来标记正负的,故signed 类型就是 − 2 15 − 1 → + 2 15 − 1 -2^{15-1} \to +2^{15-1} 2151+2151, unsigned 则是 0 → + 2 16 − 1 0 \to +2^{16}-1 0+2161.

二、float,

>>> import numpy as np
>>>
>>> a = np.array([0.213132, 1.032123, 2.000212])
>>> a
array([ 0.213132,  1.032123,  2.000212])
>>> a.dtype
dtype('float64')
>>> a.shape
(3,)

1.改变类型, 64 → 32 64 \to 32 6432 shape翻倍

>>> a.dtype = 'float32'
>>> a
array([ -1.16170272e+08,   1.58813190e+00,   3.15813966e+24,
         1.87901533e+00,   5.84719814e-16,   2.00002646e+00], dtype=float32)
>>> a.shape
(6,)

2.改变类型, 32 → 16 32 \to 16 3216 shape翻倍

>>> a.dtype = 'float16'
>>> a
array([ -9.47952271e-04,  -1.94531250e+01,   7.90625000e+00,
         1.94824219e+00,   1.49169922e-01,   2.12600000e+03,
        -5.45382500e-05,   1.98437500e+00,  -1.43647194e-04,
         2.40478516e-02,   6.61611557e-06,   2.00000000e+00], dtype=float16)
>>> a.shape
(12,)
>>>

3.改变类型, 32 → f l o a t 32 \to float 32float shape还原, float默认是float64

>>> a.shape
(12,)
>>> a.dtype = 'float'
>>> a
array([ 0.213132,  1.032123,  2.000212])
>>> a.shape
(3,)
>>>

4.改变类型, f l o a t 64 → i n t float64 \to int float64int

>>> a.dtype = 'int'
>>> a
array([-857893948, 1070286824, 1747398854, 1072726931,  640190645,
       1073741935])
>>> a.shape
(6,)
>>>

5.改变类型, i n t → i n t 32 → i n t 16 → i n t 8 int \to int32 \to int16 \to int8 intint32int16int8

>>> a.dtype = 'int32'
>>> a
array([-857893948, 1070286824, 1747398854, 1072726931,  640190645,
       1073741935])
>>> a.shape
(6,)
>>>
>>> a.dtype = 'int16'
>>> a
array([-27708, -13091,  18408,  16331,  12486,  26663, -31853,  16368,
       -30539,   9768,    111,  16384], dtype=int16)
>>> a.shape
(12,)
>>>
>>>
>>> a.dtype = 'int8'
>>> a
array([ -60, -109,  -35,  -52,  -24,   71,  -53,   63,  -58,   48,   39,
        104, -109, -125,  -16,   63,  -75, -120,   40,   38,  111,    0,
          0,   64], dtype=int8)
>>> a.shape
(24,)
>>>

6.改变类型, i n t 8 → i n t int8 \to int int8int 默认是int32

>>> a.dtype = 'int'
>>> a.shape
(6,)
>>> a
array([-857893948, 1070286824, 1747398854, 1072726931,  640190645,
       1073741935])
>>>

三、看看数据转换 astype等知识

1、向下取整

向下取整直接用内建的 int() 函数即可:

a = 3.75
int(a)
3

2、四舍五入

对数字进行四舍五入用 round() 函数:


>>>round(3.25); round(4.85)
3.0
5.0

3、向上取整

向上取整需要用到 math 模块中的 ceil() 方法:


>>>import math
>>>math.ceil(3.25)
4.0
>>>math.ceil(3.75)
4.0
>>>math.ceil(4.85)
5.0

4、分别取整数部分和小数部分

有时候我们可能需要分别获取整数部分和小数部分,这时可以用 math 模块中的 modf()
方法,该方法返回一个包含小数部分和整数部分的元组:

>>>import math
>>>math.modf(3.25)
(0.25, 3.0)
>>>math.modf(3.75)
(0.75, 3.0)
>>>math.modf(4.2)
(0.20000000000000018, 4.0)

– 接下来是重点,astype

很多时候我们用numpy从文本文件读取数据作为numpy的数组,默认的dtype是float64

但是有些场合我们希望有些数据列作为整数, 如果直接改dtype='int'的话,就会出错!原因如上,数组长度翻倍了!!!

怎么办? 用astype!

>>> b = np.array([1.23,12.201,123.1])
>>>
>>> b
array([   1.23 ,   12.201,  123.1  ])
>>> b.dtype
dtype('float64')
>>> c = b.astype(int)
>>> c
array([  1,  12, 123])
>>> c.dtype
dtype('int32')
>>>

好像还没讲uint类型,其实就是unsigned int嘛,看‘一’部分

>>>
>>> b = np.array([1.23,12.201,123.1])
>>>
>>> b.astype('uint8')
array([  1,  12, 123], dtype=uint8)
>>> b.astype('uint16')
array([  1,  12, 123], dtype=uint16)
>>> b.astype('uint32')
array([  1,  12, 123], dtype=uint32)
>>> b.astype('uint64')
array([  1,  12, 123], dtype=uint64)
>>>
>>>
>>>
>>>
>>> b = np.array([-1.23,12.201,123.1])
>>>
>>> b.astype('uint8')
array([255,  12, 123], dtype=uint8)
>>> b.astype('uint16')
array([65535,    12,   123], dtype=uint16)
>>> b.astype('uint32')
array([4294967295,         12,        123], dtype=uint32)
>>> b.astype('uint64')
array([18446744073709551615,                   12,                  123], dtype=uint64)
>>>

转载和疑问声明

如果你有什么疑问或者想要转载,没有允许是不能转载的哈
赞赏一下能不能转?哈哈,联系我啊,我告诉你呢 ~~
欢迎联系我哈,我会给大家慢慢解答啦~~~怎么联系我? 笨啊~ ~~ 你留言也行

你关注微信公众号1.机器学习算法工程师:2.或者扫那个二维码,后台发送 “我要找朕”,联系我也行啦!


数据格式汇总及type, astype, dtype区别「建议收藏」

(爱心.gif) 么么哒 ~么么哒 ~么么哒
码字不易啊啊啊,如果你觉得本文有帮助,三毛也是爱!

我祝各位帅哥,和美女,你们永远十八岁,嗨嘿嘿~~~


数据格式汇总及type, astype, dtype区别「建议收藏」
数据格式汇总及type, astype, dtype区别「建议收藏」

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/131938.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java实现excel导入导出功能_java导出excel合并列

    java实现excel导入导出功能_java导出excel合并列一、在后台实现,利用java的poi1、导入jar包,需要导入lib文件夹下如下包:poi-3.11-20141221.jarpoi-ooxml.jarpoi-ooxml-schemas.jar2、在util下写一个公共类,该类主要利用JakartaPOIHSSFAPI组件(用于操作Excel的组件),主要部分包括Excel对象,样式和格式,还有辅助操作。常用组件:……………

    2022年8月10日
    2
  • python开两个守护线程_hdfs守护线程

    python开两个守护线程_hdfs守护线程**守护线程**是区别于用户线程哈,**用户线程**即我们手动创建的线程,而守护线程是程序运行的时候在后台提供一种**通用服务的线程**。垃圾回收线程就是典型的守护线程。

    2022年10月15日
    0
  • 如何开启MySQL慢查询日志

    如何开启MySQL慢查询日志摘要:前言数据库日志记录了用户对数据库的各种操作及数据库发生的各种事件。能帮助数据库管理员追踪、分析问题。MySQL提供了错误日志、二进制日志、查询日志、慢查询日志。MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值(long_query_time,单位:秒)的SQL语句。前言数据库日志记录了用户对数据库的各种操作及数据库发生的各种事件。能帮助数据…

    2022年10月11日
    0
  • pycharm安装配置教程_pycharm2020.2安装教程

    pycharm安装配置教程_pycharm2020.2安装教程Pycharm下载地址:http://www.jetbrains.com/pycharm/  (选择professional版)2.下载好之后直接默认安装

    2022年8月28日
    1
  • html字体下划线取消,取消下划线与显示下划线设置

    html字体下划线取消,取消下划线与显示下划线设置a标签下划线和勾销下划线样式text-decoration配置篇以下介绍DIVCSS组织时刻,默许情况下A超链接锚文本下划线几种情况兼容各阅读器设置装备摆设。1、取消A默认下划线在CSS代码中最前面设置CSS以下:a{text-decoration:none}多么就可设置默认状况下超链接标签A字体无论是默许情况下照常鼠标悬停超链接字体均不闪现下划线。2、兼容各大涉猎器默许A超链接全显示下划线岂论…

    2022年5月26日
    40
  • 在触屏设备上面利用html5裁剪图片[通俗易懂]

    在触屏设备上面利用html5裁剪图片

    2022年2月7日
    59

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号