Python 如何操作以下numpy.where数组
我有一个索引数组,如下所示:Python 如何操作以下numpy.where数组,python,arrays,numpy,Python,Arrays,Numpy,我有一个索引数组,如下所示: indices=np.where(f!=1) (array([ **247, 248**, 249, 250, 251, 252, 253, 254, **255, 256**, **802, 803**, 804, 805, 806, 807, 808, 809, **810, 811** etc....]),) 这将生成以下数组: (array([ 249, 250, 251, 252, 253, 254, 804, 805,
indices=np.where(f!=1)
(array([ **247, 248**, 249, 250, 251, 252, 253, 254, **255, 256**, **802, 803**, 804, 805, 806, 807, 808, 809, **810, 811** etc....]),)
这将生成以下数组:
(array([ 249, 250, 251, 252, 253, 254, 804, 805, 806, 807, 808,
809, 1365, 1366, 1367, 1368, 1369, 1860, 1861, 1862, 1863, 1864,
2424, 2425, 2426, 2427, 2428, 2948, 2949, 2950, 2951, 2952, 2953,
3501, 3502, 3503, 3504, 3505, 3506, 4061, 4062, 4063, 4064, 4065,
4555, 4556, 4557, 4558, 4559, 5111, 5112, 5113, 5114, 5115, 5116,
6188, 6189, 6190, 6191, 6752, 6753, 6754, 6755, 6756, 7261, 7262,
7263, 7264, 7265, 7821, 7822, 7823, 7824, 7825, 7826, 8385, 8386,
8387, 8388, 8389]),)
该阵列基本上是光曲线中的倾斜。我想在每次下探的两侧再选择两个指数。因此,基本上,数组将如下所示:
indices=np.where(f!=1)
(array([ **247, 248**, 249, 250, 251, 252, 253, 254, **255, 256**, **802, 803**, 804, 805, 806, 807, 808, 809, **810, 811** etc....]),)
首先,以数组形式获取索引,其中包含:
indices=np.where(f!=1)[0] # or use np.flatnonzero
所以,我们应该-
In [804]: indices
Out[804]:
array([ 249, 250, 251, 252, 253, 254, 804, 805, 806, 807, 808,
809, 1365, 1366, 1367, 1368, 1369, 1860, 1861, 1862, 1863, 1864,
2424, 2425, 2426, 2427, 2428, 2948, 2949, 2950, 2951, 2952, 2953,
3501, 3502, 3503, 3504, 3505, 3506, 4061, 4062, 4063, 4064, 4065,
4555, 4556, 4557, 4558, 4559, 5111, 5112, 5113, 5114, 5115, 5116,
6188, 6189, 6190, 6191, 6752, 6753, 6754, 6755, 6756, 7261, 7262,
7263, 7264, 7265, 7821, 7822, 7823, 7824, 7825, 7826, 8385, 8386,
8387, 8388, 8389])
下一步,我们需要得到每个下陷的开始和停止位置。然后,通过一些广播
获得扩展号码。最后,只需插入带有连接和排序的内容
因此,实施将是必要的-
idx = np.r_[0,np.flatnonzero(np.diff(indices) > 1)+1,len(indices)]
start_pad = indices[idx[:-1]][:,None] + range(-2,0)
stop_pad = indices[idx[1:]-1][:,None] + range(1,3)
out = np.sort(np.r_[start_pad.ravel(), indices, stop_pad.ravel()])
In [832]: out
Out[832]:
array([ 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 802,
803, 804, 805, 806, 807, 808, 809, 810, 811, 1363, 1364,
1365, 1366, 1367, 1368, 1369, 1370, 1371, 1858, 1859, 1860, 1861,
1862, 1863, 1864, 1865, 1866, 2422, 2423, 2424, 2425, 2426, 2427,
2428, 2429, 2430, 2946, 2947, 2948, 2949, 2950, 2951, 2952, 2953,
2954, 2955, 3499, 3500, 3501, 3502, 3503, 3504, 3505, 3506, 3507,
3508, 4059, 4060, 4061, 4062, 4063, 4064, 4065, 4066, 4067, 4553,
4554, 4555, 4556, 4557, 4558, 4559, 4560, 4561, 5109, 5110, 5111,
5112, 5113, 5114, 5115, 5116, 5117, 5118, 6186, 6187, 6188, 6189,
6190, 6191, 6192, 6193, 6750, 6751, 6752, 6753, 6754, 6755, 6756,
6757, 6758, 7259, 7260, 7261, 7262, 7263, 7264, 7265, 7266, 7267,
7819, 7820, 7821, 7822, 7823, 7824, 7825, 7826, 7827, 7828, 8383,
8384, 8385, 8386, 8387, 8388, 8389, 8390, 8391])
产出将是-
idx = np.r_[0,np.flatnonzero(np.diff(indices) > 1)+1,len(indices)]
start_pad = indices[idx[:-1]][:,None] + range(-2,0)
stop_pad = indices[idx[1:]-1][:,None] + range(1,3)
out = np.sort(np.r_[start_pad.ravel(), indices, stop_pad.ravel()])
In [832]: out
Out[832]:
array([ 247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 802,
803, 804, 805, 806, 807, 808, 809, 810, 811, 1363, 1364,
1365, 1366, 1367, 1368, 1369, 1370, 1371, 1858, 1859, 1860, 1861,
1862, 1863, 1864, 1865, 1866, 2422, 2423, 2424, 2425, 2426, 2427,
2428, 2429, 2430, 2946, 2947, 2948, 2949, 2950, 2951, 2952, 2953,
2954, 2955, 3499, 3500, 3501, 3502, 3503, 3504, 3505, 3506, 3507,
3508, 4059, 4060, 4061, 4062, 4063, 4064, 4065, 4066, 4067, 4553,
4554, 4555, 4556, 4557, 4558, 4559, 4560, 4561, 5109, 5110, 5111,
5112, 5113, 5114, 5115, 5116, 5117, 5118, 6186, 6187, 6188, 6189,
6190, 6191, 6192, 6193, 6750, 6751, 6752, 6753, 6754, 6755, 6756,
6757, 6758, 7259, 7260, 7261, 7262, 7263, 7264, 7265, 7266, 7267,
7819, 7820, 7821, 7822, 7823, 7824, 7825, 7826, 7827, 7828, 8383,
8384, 8385, 8386, 8387, 8388, 8389, 8390, 8391])
您可能希望完全避免使用
其中
/非零
:
dip = f != 1
# below is `scipy.ndimage.morphology.binary_dilation(dip, iterations=2)`
dip_adj1 = dip.copy()
dip_adj1[1:] |= dip[:-1]
dip_adj1[:-1] |= dip[1:]
dip_adj2 = dip_adj1.copy()
dip_adj2[1:] |= dip_adj1[:-1]
dip_adj2[:-1] |= dip_adj1[1:]
dip_adj2
是一个bool
数组,其中True
位于您想要保留的位置。如果确实需要索引,可以调用np.nonzero(dip_adj2)
如果您使用的是scipy,您可以将这6行替换为f是什么样子?如果两个凹陷相邻会发生什么?您是否希望重复索引?非常感谢您的回复!我是新的编码,所以我仍然试图了解一切。我尝试了以下对我来说更有意义的方法(在工作之前经过了大量的尝试和错误):index=np。其中(f!=1)[0]index_1=[x+2 for x in index]index_2=[x-2 for x in index]index_new=sorted(list(set(index_1)| set(index_2)),它似乎也能工作。感谢您的时间和回复。我真的很感谢你的解释!