gpt4 book ai didi

python - 仅累积一列的 numpy 数组

转载 作者:行者123 更新时间:2023-12-01 06:12:15 24 4
gpt4 key购买 nike

我有一个 NumPy 数组,我想累积一列的值,比如第二列。

a = np.array([[1,2],[2,4]])
# some kind of accumulate function that accumulates just one column:
np.add.accumulate(a, 2)

a 现在应该是 [[1,2],[2,6]]

有没有办法在 NumPy 中做到这一点?

最佳答案

a = np.array([[1,2],[2,4]])
np.add.accumulate(a[:,1], out=a[:,1])

a 现在是:

array([[1, 2],
[2, 6]])

关于python - 仅累积一列的 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5176968/

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