如果与前一个元素的差异 <N,则将元素放入 numpy 数组(或 pandas 系列)
Drop element in numpy array (or pandas series) if difference to previous element is <N
我有一个看起来像这样的 numpy 数组:
a = np.array([0,10,19,20,30,40,42,49,50,51])
我想删除所有连续差值<=2的元素,最终保持
a_filtered = np.array([0,10,19,30,40,49])
如何在 numpy 中执行此操作? (可选)特别感谢如何在 pandas 系列中执行此操作(例如,删除索引差异小于 N 的所有行)
IIUC
s=pd.Series(a)
s[~(s.diff()<=2)]
Out[289]:
0 0
1 10
2 19
4 30
5 40
7 49
dtype: int32
s[~(s.diff()<=2)].to_numpy()
Out[292]: array([ 0, 10, 19, 30, 40, 49])
给你:
N = 2
s = pd.Series(a)
mask = ~s.diff().le(2)
s[mask]
# you can also do
# a[mask]
输出:
1 10
2 19
4 30
5 40
7 49
dtype: int32
在numpy上,你可以使用np.diff
和np.insert
来专门处理元素0
m = np.insert(np.diff(a, 1) > 2, 0, True)
a[m]
Out[526]: array([ 0, 10, 19, 30, 40, 49])
或使用 np.roll
并将掩码的元素 0
分配给 True
m = (a - np.roll(a, 1)) > 2
m[0] = True
a[m]
Out[534]: array([ 0, 10, 19, 30, 40, 49])
我有一个看起来像这样的 numpy 数组:
a = np.array([0,10,19,20,30,40,42,49,50,51])
我想删除所有连续差值<=2的元素,最终保持
a_filtered = np.array([0,10,19,30,40,49])
如何在 numpy 中执行此操作? (可选)特别感谢如何在 pandas 系列中执行此操作(例如,删除索引差异小于 N 的所有行)
IIUC
s=pd.Series(a)
s[~(s.diff()<=2)]
Out[289]:
0 0
1 10
2 19
4 30
5 40
7 49
dtype: int32
s[~(s.diff()<=2)].to_numpy()
Out[292]: array([ 0, 10, 19, 30, 40, 49])
给你:
N = 2
s = pd.Series(a)
mask = ~s.diff().le(2)
s[mask]
# you can also do
# a[mask]
输出:
1 10
2 19
4 30
5 40
7 49
dtype: int32
在numpy上,你可以使用np.diff
和np.insert
来专门处理元素0
m = np.insert(np.diff(a, 1) > 2, 0, True)
a[m]
Out[526]: array([ 0, 10, 19, 30, 40, 49])
或使用 np.roll
并将掩码的元素 0
分配给 True
m = (a - np.roll(a, 1)) > 2
m[0] = True
a[m]
Out[534]: array([ 0, 10, 19, 30, 40, 49])