16

NumPy迎规模最大版本更新,新增函数注释等功能,支持Python 3.7+

 3 years ago
source link: https://www.jiqizhixin.com/articles/2021-02-04-6
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

NumPy 1.20.0 版本上线,最新亮点包括 NumPy 函数注释、为数组提供滑动窗口视图等。

作为 Python 语言的一个扩展程序库,NumPy 支持大量的维度数组与矩阵运算,也针对数组运算提供大量的数学函数库。自初代版本上线之后,NumPy 已经成为 Python 科学计算的扩展包。如今,在计算多维数组和大型数组方面,它是使用最广的。此外,它还提供多个函数,操作起数组来效率很高,还可用来实现高级数学运算。

Zj6Jji3.png!mobile

近日,NumPy 迎来了更新。NumPy 官方表示,NumPy v1.20 是迄今为止规模最大的版本更新 ,共计 184 位开发者贡献了约 684 条 PR,新增了类型注释、滑动窗口视图等功能。

UJbYBry.png!mobile

新版本支持 Python 3.7-3.9,但不支持 Python 3.6。主要亮点如下:

  • NumPy 函数注释;

  • 扩大 SIMD 使用范围,提升 ufuncs(Universal Functions)的执行速度;

  • 更改数据类型和强制转换实现的准备工作,以便为扩展数据类型提供更简单的途径;

  • 文档改进,包括大约 185 个 PR 合并;

  • 关于移除 Python 2.7 的进一步清理(cleanups), 这样可以提高代码的可读性并消除技术负担;

  • 对即将到来的 Cython 3.0 提供初步支持。

上线新函数

新发布的 NumPy 1.20.0 版本包括以下三大新函数:

(1)random.Generator 类有一个新函数 permuted。新函数与 shuffle 和 permutation 不同,给定轴索引的子数组会被换算。例如,现在可以对一个二维数组的行或列进行换算;

(2)sliding_window_view 为 numpy 数组提供了一个滑动窗口视图。numpy.lib.stride_tricks.sliding_window_view 在 numpy 数组上构造视图,提供对数组的滑动或移动窗口访问。这使得 running means 等某些算法可以简单实现。

>>> x = np.arange(6)
*>>>* ** x.shape
(6,)
*>>>* ** v = sliding_window_view(x, 3)
*>>>* ** v.shape
(4, 3)
*>>>* ** v
array([[0, 1, 2],      
       [1, 2, 3],      
       [2, 3, 4],       
       [3, 4, 5]])

(3)一个面向用户(user-facing)的全新函数 numpy.broadcast_shapes。该函数可以从给定形状元组中通过 broadcasting 获得形状,其中 broadcasting 机制的功能是为了方便不同 shape 的 array 进行数学运算。

*>>>* np.broadcast_shapes((1, 2), (3, 1))
(3, 2)
*>>>* np.broadcast_shapes(2, (3, 1))
(3, 2)
*>>>* np.broadcast_shapes((6, 7), (5, 6, 1), (7,), (5, 1, 7))
(5, 6, 7)

弃用的功能

像 np.int 这类内置类型(builtin type)的别名已被弃用。在很长一段时间,np.int 是 int 的别名,但常常对新手造成困扰,这主要是历史原因造成的。

下表显示了已弃用别名的完整列表,其中第三列为备用 NumPy 名称。

nYzEneJ.png!mobile

此外,将 shape=None 传递给带有非可选参数shape 的函数已被弃用,mode 和 searchside 的非精确匹配、numpy.dual 模块也都被弃用。

更多更新内容请参考:https://numpy.org/doc/1.20/release/1.20.0-notes.html#deprecations


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK