numpy

36 阅读1分钟

NumPy是一个功能强大的Python库,主要用于对多维数组执行计算

有三个概念为NumPy提供了强大的功能:

  • 矢量化
  • 广播
  • 索引

矢量化

矢量化是NumPy中的一种强大功能,可以将操作表达为在整个数组上而不是在各个元素上发生

在Python中循环数组或任何数据结构时,会涉及很多开销。 NumPy中的向量化操作将内部循环委托给高度优化的C和Fortran函数,从而实现更清晰,更快速的Python代码。

axis

Axis 0 (Direction along Rows) – Axis 0 is called the first axis of the Numpy array. This axis 0 runs vertically downward along the rows of Numpy multidimensional arrays, i.e., performs column-wise operations.

Axis 1 (Direction along with columns) – Axis 1 is called the second axis of multidimensional Numpy arrays. As a result, Axis 1 sums horizontally along with the columns of the arrays. It performs row-wise ****operations.

广播

当运算中的 2 个数组的形状不同时,numpy 将自动触发广播机制

Two dimensions are compatible when

  1. they are equal, or
  2. one of them is 1.