zoukankan      html  css  js  c++  java
  • ndarray数据类型

    dtype(数据类型)是一个特殊的对象,它含有ndarray将一块内存解释为特定数据类型所需的信息

    1 In [18]: sim1 = np.array([1,2,3],dtype=np.float64)
    2 
    3 In [19]: sim2 = np.array([1,2,3],dtype=np.float32)
    4 
    5 In [20]: sim1.dtype
    6 Out[20]: dtype('float64')
    7 
    8 In [21]: sim2.dtype
    9 Out[21]: dtype('float32')

    dtype是Numpy强大和灵活的原因之一。数值型dtype的命名方式相同:一个类型名(如float或int),后面跟一个用于表示各元素位长的数字。标准的双精度浮点值(既Python中的float)需要占用8字节(既64位)。因此,该类型在Numpy中就记作float64。

    Numpy的数据类型

    类型 类型代码 说明
    int8、uint8 i1、u1 有符号和无符号的8位(1个字节)整数
    int16、uint16 i2、u2 有符号和无符号的16位(2个字节)整数
    int32、uint32 i4、u4 有符号和无符号的32位(4个字节)整数
    int64、unint64 i8、u8 有符号和无符号的64位(8个字节)整数
    float16   f2 半精度浮点数
    float32 f4或f 标准的单精度浮点数
    float64 f8或d 标准的双精度浮点数
    float128 f16或g 扩展精度浮点数
    complex64、complex128、complex256 c8、c16、c32 分别用两个32位、64位或128位浮点数表示的复数
    bool   ? 存储True和False值的布尔类型
    object O Python对象类型
    string_ S 固定长度的字符串长度(每个字符1个字节)
    unicode_ U 固定长度的unicode长度(每个字符1个字节)

    可以通过ndarray的astype方法显式地转换其dtype。整数转换浮点数。

    注意:调用astype无论如何都会创建出一个新的数组(原始数据的一份拷贝),即使新dtype跟老dtype相同也是如此。

    1 In [22]: sim = np.array([1,2,3,4,5])
    2 
    3 In [23]: sim.dtype
    4 Out[23]: dtype('int64')
    5 
    6 In [24]: float_sim = sim.astype(np.float64)
    7 
    8 In [25]: float_sim.dtype
    9 Out[25]: dtype('float64')

    浮点数转换成整数,小数点部分将会被截断。

    1 In [26]: sim = np.array([3.7,-1.6,4.7,-2.3,9.0])
    2 
    3 In [27]: sim
    4 Out[27]: array([ 3.7, -1.6,  4.7, -2.3,  9. ])
    5 
    6 In [28]: sim.astype(np.int32)
    7 Out[28]: array([ 3, -1,  4, -2,  9], dtype=int32)

    字符串全是数字,可以用astype将其转换为数值形式。

    1 In [31]: number_strings = np.array(['1.26','-8','-4.6'],dtype=np.string_)
    2 
    3 In [32]: number_strings.astype(np.float64)
    4 Out[32]: array([ 1.26, -8.  , -4.6 ])

    还可以用简洁的代码来表示dtype。

    1 In [33]: empty_uint32 = np.empty(8,dtype='u4')
    2 
    3 In [34]: empty_uint32
    4 Out[34]: 
    5 array([         0, 1072693248, 1717986918, 1073899110,          0,
    6        1074790400,          0, 1075052544], dtype=uint32)
  • 相关阅读:
    JavaWeb(一)
    有趣的天平秤假币问题
    栈应用——逆波兰式表达式的值
    栈应用——最长括号匹配
    倾力总结40条常见的移动端Web页面问题解决方案
    Emmet:HTML/CSS代码快速编写神器
    我的 Github 个人博客是怎样炼成的
    解决mac下atom安装插件失败问题
    Github建站全攻略
    OS X快捷键最最齐全版(官方版)
  • 原文地址:https://www.cnblogs.com/yu-1104/p/7867849.html
Copyright © 2011-2022 走看看