Numpy 数据类型及数组创建

Numpy 数据类型及数组创建

    • 常量
      • numpy.nan
      • numpy.inf
      • numpy.pi
      • numpy.e
    • 数据类型
      • 基本数据类型
      • 创建基本数据类型
    • 时间日期、时间增量
    • 数组创建
    • 参考资料

常量

numpy.nan

nan(NAN,Nan):“ not a number”,即不是数字,表示空值。

何时出现

  • 当我们读取本地文件为float的时候,如果有缺失,就会出现nan
  • 当做了一个恰当计算的时候(比如无穷大(inf)减去无穷大)

特点

  • 两个Nan不相等
  • nan 和任何值计算都为 nan

numpy.inf

inf(-inf,inf):全称 infinity,inf 表示正无穷,-inf 表示负无穷。

何时出现

  • 比如一个数字除以 0,在 Python 中直接会报错,但在 Numpy 中则是一个 inf 或者 -inf

特点

  • 两个 inf 是相等的

numpy.pi

表示圆周率

numpy.e

表示自然常数

数据类型

基本数据类型

  • bool_ = bool8,8位,布尔类型
  • int8 = byte,8位,整型
  • int16 = short,16位,整型
  • int32 = intc,32位,整型
  • int_ = int64 = long = int0 = intp,64位,整型
  • uint8 = ubyte,8位,无符号整型
  • uint16 = ushort,16位,无符号整型
  • uint32 = uintc,32位,无符号整型
  • uint64 = uintp = uint0 = uint,64位,无符号整型
  • float16 = half,16位,浮点型
  • float32 = single,32位 浮点型
  • float_ = float64 = double,64位,浮点型
  • str = unicode = str0 = unicode ,Unicode 字符串
  • datetime64,日期时间类型
  • timedelta64,表示两个时间之间的间隔

创建基本数据类型

生成dtype类的实例

  • b boolean ‘b1’
  • i signed integer ‘i1’, ‘i2’, ‘i4’, ‘i8’
  • u unsigned integer ‘u1’, ‘u2’ ,‘u4’ ,‘u8’
  • f floating-point ‘f2’, ‘f4’, ‘f8’
  • c complex floating-point
  • m timedelta64 表示两个时间之间的间隔
  • M datetime64 日期时间类型
  • O object
  • S (byte-)string S3表示长度为3的字符串
  • U Unicode Unicode 字符串
  • V void

时间日期、时间增量

将字符串转换成时间日期类型 datetime64(datetime 已被 python 包含的日期时间库所占用.

数组创建

numpy 提供的最重要的数据结构是ndarray,它是 python 中list的扩展。

  • array()函数创建
  • asarray()函数创建
  • fromfunction()函数创建
  • zeros(),zeros_like() 0数组
  • ones(),ones_like() 1数组
  • empty(),empty_like() 空数组
  • eye(),identity() 单位数组
  • diag() 对角数组
  • full(),full_like() 常数数组
  • arange()函数:返回给定间隔内的均匀间隔的值。
  • linspace()函数:返回指定间隔内的等间隔数字
  • logspace()函数:返回数以对数刻度均匀分布。
  • numpy.random.rand() 返回一个由[0,1)内的随机数组成的数组。

参考资料

  1. https://www.pythonf.cn/read/78971
  2. https://zhuanlan.zhihu.com/p/112567081
  3. https://github.com/datawhalechina/team-learning-program/tree/master/IntroductionToNumpy/task01%20%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8F%8A%E6%95%B0%E7%BB%84%E5%88%9B%E5%BB%BA

你可能感兴趣的:(numpy,numpy,python,数据分析)