gpt4 book ai didi

python - numpy nonzero() 返回按行索引排序的索引?

转载 作者:行者123 更新时间:2023-11-30 23:07:51 25 4
gpt4 key购买 nike

与 Matlab 中一样,nonzeros返回按列排序的索引。在 NumPy 中,返回的索引似乎按行排序(对于 2D 矩阵)。但这并没有在其documentation中阐明。 。

那么,这样的假设是否安全?

一个例子:

test = np.array([[0,2], [3,0]])
test[test.nonzero()]

给出 array([2, 3]) 而不是 array([3, 2])

最佳答案

有以下评论on the C source code of PyArray_NonZero ,处理所有非零调用的 C 函数:

/*NUMPY_API
* Nonzero
*
* TODO: In NumPy 2.0, should make the iteration order a parameter.
*/
NPY_NO_EXPORT PyObject *
PyArray_Nonzero(PyArrayObject *self)

迭代顺序is now hardcoded to be C-order ,即最后一个索引变化最快,即对于 2D 情况,先排序行,然后列。鉴于该评论,可以非常安全地假设,如果这种情况发生变化,那将是通过提供默认为当前行为的新功能来实现。

关于python - numpy nonzero() 返回按行索引排序的索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32036357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com