python - 仅累积一列的 numpy 数组

标签 python numpy

我有一个 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/

相关文章:

python - 如何在 Python 中获取绝对文件路径

python - 调用PutObject操作时发生错误(AccessDenied): Access Denied python

python - Plotly:向条形图添加线条

python - 合并/连接具有不同元素的数组

python - 如何删除 3D numpy 数组中的特定元素

python - 如何将图像添加到我的博客文章中?

python - Seaborn自动选色

Python:将数据类型从整数更改为 float 时出现内存错误

python - Sklearn 随机森林回归器出错

python - 使用 np.interp 插值缺失值