数据格式汇总及type, astype, dtype区别

2023-05-16

标签(空格分隔): python


文章目录

        • uint8:在此输入正文8位的无符号整形数据,取值范围从0到255
    • 一、singed与unsigned的区别
    • 二、float,
        • 1.改变类型, $64 \to 32$ shape翻倍
        • 2.改变类型, $32 \to 16$ shape翻倍
        • 3.改变类型, $32 \to float$ shape还原, float默认是float64
        • 4.改变类型, $float64 \to int$
        • 5.改变类型, $int \to int32 \to int16 \to int8$
        • 6.改变类型, $int8 \to int$ 默认是int32
    • 三、看看数据转换 astype等知识
      • 1、向下取整
      • 2、四舍五入
      • 3、向上取整
      • 4、分别取整数部分和小数部分
        • - 接下来是重点,astype
  • 转载和疑问声明
  • 我祝各位帅哥,和美女,你们永远十八岁,嗨嘿嘿~~~

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的存储方式用表格来表示

0123456789101112131415
0000000000000001

最左边的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.或者扫那个二维码,后台发送 “我要找朕”,联系我也行啦!

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

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

数据格式汇总及type, astype, dtype区别 的相关文章

随机推荐