jax.numpy.argsort#
- jax.numpy.argsort(a, axis=-1, *, kind=None, order=None, stable=True, descending=False)[源代码]#
返回对数组进行排序的索引。
JAX 实现的
numpy.argsort()
。- 参数:
a (Array | ndarray | bool | number | bool | int | float | complex) – 要排序的数组
axis (int | None) – 沿其排序的整数轴。默认为
-1
,即最后一个轴。如果为None
,则在排序前将a
展平。stable (bool) – 布尔值,指定是否应使用稳定排序。默认值=True。
descending (bool) – 布尔值,指定是否应按降序排序。默认值=False。
kind (None) – 已弃用;请改用 stable=True 或 stable=False 指定排序算法。
order (None) – JAX 不支持
- 返回:
对数组进行排序的索引数组。返回数组的形状将为
a.shape
(如果axis
是整数)或(a.size,)
(如果axis
为 None)。- 返回类型:
示例
简单的一维排序
>>> x = jnp.array([1, 3, 5, 4, 2, 1]) >>> indices = jnp.argsort(x) >>> indices Array([0, 5, 4, 1, 3, 2], dtype=int32) >>> x[indices] Array([1, 1, 2, 3, 4, 5], dtype=int32)
沿数组的最后一个轴排序
>>> x = jnp.array([[2, 1, 3], ... [6, 4, 3]]) >>> indices = jnp.argsort(x, axis=1) >>> indices Array([[1, 0, 2], [2, 1, 0]], dtype=int32) >>> jnp.take_along_axis(x, indices, axis=1) Array([[1, 2, 3], [3, 4, 6]], dtype=int32)
另请参阅
jax.numpy.sort()
:直接返回排序后的值。jax.numpy.lexsort()
:多个数组的字典序排序。jax.lax.sort()
:包装 XLA Sort 运算符的较低级别函数。