zoukankan      html  css  js  c++  java
  • numpy 学习:数据类型和空值

    NumPy是Python中用于科学计算的基础软件包,专门用于处理矩阵,数据类型是数值型的,用于对数值数据进行快速的计算。因此,numpy支持的数据类型非常精细,但是numpy不支持精确小数。

    在导入numpy模块时,通常把numpy模块重命名为np:

    import numpy as np

    一,numpy的数据类型

    Numpy支持的标量数据类型非常繁杂,我比较喜欢固定size的数据类型,因为固定size的数据类型跟平台无关,且命名格式比较规范,格式是:[u]type[size],u代表无符号,size代表占用的内存空间。

    1,布尔类型

    • numpy.bool8

    2,有符号的整数类型:

    • numpy.int8
    • numpy.int16
    • numpy.int32
    • numpy.int64

    3,无符号的整数类型:

    • numpy.uint8
    • numpy.uint16
    • numpy.uint32
    • numpy.uint64

    4,浮点数类型:

    • numpy.float16
    • numpy.float32
    • numpy.float64
    • numpy.float96
    • numpy.float128

    5,复数类型:

    • numpy.complex64
    • numpy.complex128
    • numpy.complex192
    • numpy.complex256

    6,汇总

    numpy支持的数据类型如下表所示:

    类型比特位说明
    bool_ = bool8 8位 布尔类型
    int8 = byte 8位 整型
    int16 = short 16位 整型
    int32 = intc 32位 整型
    int_ = int64 = long 64位 整型
    uint8 = ubyte 8位 无符号整型
    uint16 = ushort  16位 无符号整型
    uint32 = uintc  32位 无符号整型
    uint64 = uintp 64位 无符号整型
    float16 16位 浮点型
    float32 = single 32位 浮点型
    float_ = float64 = double 64位 浮点型
    str_ = unicode_ = unicode Unicode 字符串  
    datetime64 日期时间类型  
    timedelta64 表示两个时间之间的间隔  

    二,浮点数类型

    数据类型的信息,可以通过finfo来查看浮点数的类型信息,通过iinfo函数来查看整数的类型信息,

    finfo.eps表示1.0和下一个大于1.0的最小的浮点数之间的差异值。

    ff16 = np.finfo(np.float16)
    print(ff16.bits)  # 16
    print(ff16.min)  # -65500.0
    print(ff16.max)  # 65500.0
    print(ff16.eps)  # 0.000977

    三,日期和时间类型

    datetime64是带单位的,分为日期单位和时间单位,日期单位的级别都大于时间单位。

    日期单位是:年(Y),月(M),周(W),天(D),单位级别依次减小,

    时间单位是:时(h),分(m),秒(s),毫秒(ms),微妙(us),纳秒(ns),单位级别依次减小,

    • 1秒 = 1000 毫秒(milliseconds)
    • 1毫秒 = 1000 微秒(microseconds

    通过字符串来创建日期时间类型,默认情况下,numpy会根据字符串来自动选择日期和时间单位:

    a = np.datetime64('2020-03-08 20:00:05')
    b = np.datetime64('2020-03-08')

    datetime64的单位是级别最小的单位,例如,a 的单位是s,而b的单位是D。

    1,时间增量

    timedelta64 表示两个 datetime64 之间的差值,timedelta64是带单位的,和相减运算中的两个 datetime64 中的较小的单位保持一致。

    t1 = np.datetime64('2020-03-08') - np.datetime64('2020-03-07')

    t1 表示两个日期之间的差值,单位是D。

    NumPy允许两个Datetime64值相减,这个操作产生一个带有时间单位的数字。timedelta64的参数是一个数字(用于表示单位数),以及日期/时间单位,如 (D)ay, (M)onth, (Y)ear, (h)ours, (m)inutes, 或者 (s)econds。timedelta64数据类型也接受字符串“NAT”代替“非时间”值的数字。

    >>> numpy.timedelta64(1, 'D')

    Datetimes 和 Timedeltas 一起工作,为简单的日期时间计算提供方法。

    >>> np.datetime64('2009-01-01') - np.datetime64('2008-01-01')
    numpy.timedelta64(366,'D')
    >>> np.datetime64('2009') + np.timedelta64(20, 'D')
    numpy.datetime64('2009-01-21')

    2,np.datetime64 和 datetime.datetime之间的转换

    import numpy as np
    import datetime
    
    dt = datetime.datetime(year=2020, month=6, day=1, hour=20, minute=5, second=30)
    dt64 = np.datetime64(dt, 's')
    print(dt64, dt64.dtype)
    # 2020-06-01T20:05:30 datetime64[s]
    
    dt2 = dt64.astype(datetime.datetime)
    print(dt2, type(dt2))
    # 2020-06-01 20:05:30 <class 'datetime.datetime'>

    四,numpy的空值

    nan表示空值,两个nan是不相等的。

    print(np.nan == np.nan)  # False
    print(np.nan != np.nan)  # True

    参考文档:

    作者悦光阴
    本文版权归作者和博客园所有,欢迎转载,但未经作者同意,必须保留此段声明,且在文章页面醒目位置显示原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    matplotlib 进阶之origin and extent in imshow
    Momentum and NAG
    matplotlib 进阶之Tight Layout guide
    matplotlib 进阶之Constrained Layout Guide
    matplotlib 进阶之Customizing Figure Layouts Using GridSpec and Other Functions
    matplotlb 进阶之Styling with cycler
    matplotlib 进阶之Legend guide
    Django Admin Cookbook-10如何启用对计算字段的过滤
    Django Admin Cookbook-9如何启用对计算字段的排序
    Django Admin Cookbook-8如何在Django admin中优化查询
  • 原文地址:https://www.cnblogs.com/ljhdo/p/15753805.html
Copyright © 2011-2022 走看看