[关闭]
@BruceWang 2018-01-08T14:05:41.000000Z 字数 3638 阅读 11333

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

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

最左边的0是用来标记正负的,故signed类型就是 , unsigned 则是.

二、float,

  1. >>> import numpy as np
  2. >>>
  3. >>> a = np.array([0.213132, 1.032123, 2.000212])
  4. >>> a
  5. array([ 0.213132, 1.032123, 2.000212])
  6. >>> a.dtype
  7. dtype('float64')
  8. >>> a.shape
  9. (3,)

1.改变类型, shape翻倍

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

2.改变类型, shape翻倍

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

3.改变类型, shape还原, float默认是float64

  1. >>> a.shape
  2. (12,)
  3. >>> a.dtype = 'float'
  4. >>> a
  5. array([ 0.213132, 1.032123, 2.000212])
  6. >>> a.shape
  7. (3,)
  8. >>>

4.改变类型,

  1. >>> a.dtype = 'int'
  2. >>> a
  3. array([-857893948, 1070286824, 1747398854, 1072726931, 640190645,
  4. 1073741935])
  5. >>> a.shape
  6. (6,)
  7. >>>

5.改变类型,

  1. >>> a.dtype = 'int32'
  2. >>> a
  3. array([-857893948, 1070286824, 1747398854, 1072726931, 640190645,
  4. 1073741935])
  5. >>> a.shape
  6. (6,)
  7. >>>
  8. >>> a.dtype = 'int16'
  9. >>> a
  10. array([-27708, -13091, 18408, 16331, 12486, 26663, -31853, 16368,
  11. -30539, 9768, 111, 16384], dtype=int16)
  12. >>> a.shape
  13. (12,)
  14. >>>
  15. >>>
  16. >>> a.dtype = 'int8'
  17. >>> a
  18. array([ -60, -109, -35, -52, -24, 71, -53, 63, -58, 48, 39,
  19. 104, -109, -125, -16, 63, -75, -120, 40, 38, 111, 0,
  20. 0, 64], dtype=int8)
  21. >>> a.shape
  22. (24,)
  23. >>>

6.改变类型, 默认是int32

  1. >>> a.dtype = 'int'
  2. >>> a.shape
  3. (6,)
  4. >>> a
  5. array([-857893948, 1070286824, 1747398854, 1072726931, 640190645,
  6. 1073741935])
  7. >>>

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

1、向下取整

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

  1. a = 3.75
  2. int(a)
  3. 3

2、四舍五入

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

  1. >>>round(3.25); round(4.85)
  2. 3.0
  3. 5.0

3、向上取整

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

  1. >>>import math
  2. >>>math.ceil(3.25)
  3. 4.0
  4. >>>math.ceil(3.75)
  5. 4.0
  6. >>>math.ceil(4.85)
  7. 5.0

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

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

  1. >>>import math
  2. >>>math.modf(3.25)
  3. (0.25, 3.0)
  4. >>>math.modf(3.75)
  5. (0.75, 3.0)
  6. >>>math.modf(4.2)
  7. (0.20000000000000018, 4.0)

- 接下来是重点,astype

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

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

怎么办? 用astype!

  1. >>> b = np.array([1.23,12.201,123.1])
  2. >>>
  3. >>> b
  4. array([ 1.23 , 12.201, 123.1 ])
  5. >>> b.dtype
  6. dtype('float64')
  7. >>> c = b.astype(int)
  8. >>> c
  9. array([ 1, 12, 123])
  10. >>> c.dtype
  11. dtype('int32')
  12. >>>

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

  1. >>>
  2. >>> b = np.array([1.23,12.201,123.1])
  3. >>>
  4. >>> b.astype('uint8')
  5. array([ 1, 12, 123], dtype=uint8)
  6. >>> b.astype('uint16')
  7. array([ 1, 12, 123], dtype=uint16)
  8. >>> b.astype('uint32')
  9. array([ 1, 12, 123], dtype=uint32)
  10. >>> b.astype('uint64')
  11. array([ 1, 12, 123], dtype=uint64)
  12. >>>
  13. >>>
  14. >>>
  15. >>>
  16. >>> b = np.array([-1.23,12.201,123.1])
  17. >>>
  18. >>> b.astype('uint8')
  19. array([255, 12, 123], dtype=uint8)
  20. >>> b.astype('uint16')
  21. array([65535, 12, 123], dtype=uint16)
  22. >>> b.astype('uint32')
  23. array([4294967295, 12, 123], dtype=uint32)
  24. >>> b.astype('uint64')
  25. array([18446744073709551615, 12, 123], dtype=uint64)
  26. >>>

如果你有什么疑问,欢迎联系我哈,我会给大家慢慢解答啦~~~怎么联系我? 笨啊~ ~~ 你留言也行
你关注微信公众号1.听朕给你说:2.tzgns666,3.或者扫那个二维码,后台联系我也行啦!
tzgns666

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

终于写完了,赞赏一下下嘛!

(爱心.gif) 么么哒~么么哒~么么哒
爱心从我做起,贫困山区捐衣服,为开源社区做贡献!

码字不易啊,如果你觉得本文有帮助,三毛也是爱!真的就三毛,呜呜。。。

weiChat
Alibaba

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注