我有一个名为data
的NxM
numpy数组。
我还有一个长度为N
的数组start_indices
。
我想要一个新的长度为M
的数组,其中第i个元素是sum(data[i][start_indices[i]:])
。
以下是一种方法:
import numpy as np
data = np.linspace(0, 11, 12).reshape((3, 4))
data
array([[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11]])
start_indices = np.array([0, 1, 2])
sums = []
for start_index, row in zip(start_indices, data):
sums.append(np.sum(row[start_index:]))
sums = np.array(sums)
有更numpy风格的方法吗?
np.array([np.sum(row[s:]) for s, row in zip(start,data)])
。但速度上没有实质性的差别。 - hpaulj