numpy笔记

207 阅读1分钟

一、basic

ndarray —— numpy的核心研究对象

  • ndarray.ndim number of axes
  • ndarray.shape dimensions
  • ndarray.size total number of elements
  • ndarray.dtype type of elements
  • ndarray.itemsize size in bytes of each element
  • ndarray.data buffer containing the actual elements

创建

1 通过可迭代对象

array()

2 通过一些功能函数

  • arange()
  • zeros()/zeros_like()
  • empty()/empty_like()
  • ones()/ones_like()
  • 一些random函数

打印

完全打印

np.set_printoptions(threshold=np.nan)

基本计算

算数运算都是元素级的

  • np.sum(axis)
  • np.max(axis)/np.min(axis)
  • np.cumsum(axis)
  • np.add()
  • np.sqrt()
  • np.exp()
  • np.floor()/np.ceil()

索引、切片和迭代

  • [1, :, 2] 多轴索引切片
  • 布尔索引
  • 花式索引

shape操作

  • ndarray.T 转置
  • np.reshape() 任意改变

堆积和拆分

  • np.vstack()
  • np.hstack()
  • np.hsplit()
  • np.vsplit()

复制和视图

直接赋值,不是复制,二者完全相等;

视图是深复制 copy()是浅复制

二、api

numpy.random

randint(low, high=None, size=None, dtype='l')  # [low, hight)
random(size=None)  # [0,1)
rand(d0, d1, ...)  # 均匀分布 [0,1)
randn(d0, d1, ...)  # 正态分布 [0, 1)