gpt4 book ai didi

python - 为什么 np.where 的结果对于多维数组是只读的?

转载 作者:太空狗 更新时间:2023-10-30 01:01:11 24 4
gpt4 key购买 nike

>>> a = np.where(np.ones(5))[0]
>>> a
array([0, 1, 2, 3, 4])
>>> a.flags['WRITEABLE']
True

>>> b = np.where(np.ones((5,2)))[0]
>>> b
array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4])
>>> b.flags['WRITEABLE']
False

为什么 b 是只读的而 a 不是? the documentation中没有提到这个.

最佳答案

在内部 numpy 计算结果为维数组,但随后返回一个元组,该元组是在此数组上具有相同步幅的 View one for each dimension ;

例如,如果数组是这样的:

>>> a
array([[0, 1, 0],
[3, 0, 5]])

在numpy内部先计算

>>> i
array([0, 1, 1, 0, 1, 2])

其中 i[2 * k]i[2 * k + 1] 是第 k 个非零值;但是,返回的结果是:

>>> (i[::2], i[1::2])
(array([0, 1, 1]), array([1, 0, 2]))

当它创建 View 时 passes 0 as the flags argument .因此,可写标志未设置。

当输入数组是一维时,it takes a short-cut因此标志设置不同。

关于python - 为什么 np.where 的结果对于多维数组是只读的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28663142/

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