如何编写算法以在python中查找numpy数组的特定值 [英] how to write an algorithm for finding specific values of a numpy array in python
问题描述
我有一条线,想要提取其中的一些.这是行数:
I have a serie of lines and want to extract some of them. This is the number of lines:
line_no= np.arange (17, 34)
这些线沿两个垂直方向排列.我在无花果中用蓝色和红色线条显示了它们.我知道方向正在改变,这叫做sep:
These lines are arranged in two perependicular direction. I have shown them with bluw and red lines in the fig. I know where the direction is changing, it is called sep:
sep=25 # lines from 17 to 25 are blue and from 26 to end are red
然后,我有创建线的点数.我称它们为块,因为每个数字都可以是块:
Then, I have the number of the points that create the lines. I call them chunks, because each number can be chunk:
chunk_val=np.array([1,2,3,3,4])
此 chunk_val
说明如何创建行.我需要提取特定的行号,该行号在图中用蓝色圆圈突出显示.对于红线,算法更简单. chunk_val [0]
是 1
,这表示我那里没有红线. chunk_val [1]
是 2
,所以我只有一行,并且我希望该行( 26
),因为前一个块没有给我任何行. chunk_val [2]
是 3
,所以我在这里有两行,我想要最后一行( 27 中的
28
/code>和 28
),因为前面的代码块给了我一行. chunk_val [3]
是 3
,又有两行,但是我什么都不想要,因为在此块中创建的红线数量等于先前的块. chunk_val [3]
是 4
,它创建了三行,我想从 31中导出最后一个数字(
33
, 32
和 33
),因为它比以前的代码块创建的行多了一行.如果 chunk_val [3]
为 5
,那么我想保存最后两行.
This chunk_val
says how lines are created. I need to extract specific line numbers which are highlighted by a blue circle in the fig. For the red lines the algorithm is simpler. chunk_val[0]
is 1
, it means I have no red line there. chunk_val[1]
is 2
, so I have one line and I want that line (26
) because the previous chunk gave me no line. chunk_val[2]
is 3
, so I have two lines here and I want the one last line (28
out of 27
and 28
), because previous chunk gave me one line. chunk_val[3]
is 3
, there are again two line but I want none of them because the number of created red lines in this chunk is equal to previous chunk. chunk_val[3]
is 4
, it creates three line and I want to export the number of last one (33
out of 31
, 32
and 33
) because it is creating one line more than previous chunk. If chunk_val[3]
was 5
, then I wanted to save the last two line.
对于连接块的蓝线来说,要复杂一些. chunk_val [0]
为 1
,而 chunk_val [1]
为 2
,这意味着存在一条连接线,因为第一个 chunk_value
定义可以有多少行.同时,我需要该行( 17
),因为只有一行.如果还有更多行,我想选择最后一行.从 chunk_val [1]
到 chunk_val [2]
,我有两行,我希望最后一行( 18 <中的
19
/code>和 19
).然后, chunk_val [2]
到 chunk_val [3]
共有三行,我希望最后一行( 20中的
22
, 21
和 22
).从 chunk_value [3]
到 chunk_value [4]
再次有三行,我希望最后一行( 23中的
25
, 24
和 25
).关于如何选择这些连接的(蓝色)线,有一个非常重要的注意事项:我想从第一个 chunk_val
开始选择它们,直到 chunk_val
的值更改为止.在我的示例中,直到最后一个 chunk_val
为止,我看到了一个变化( chunk_val [-2]
为 3
,而 chunk_val [-1]
是 4
).如果 chunk_val [-1]
也是 3
,那么我想在从 chunk_val [1]
转到 chunk_val [2]
(我是说我只想拥有 17
和 19
).这些是我算法的想法,但我不知道用python编写代码.总而言之,我要列出以下数字:
For blue lines, that connect the chunks it is a little bit more complicated. chunk_val[0]
is 1
and chunk_val[1]
is 2
, it means there is one connecting line because first chunk_value
defines how many lines can be there. Meanwhile, I want that line (17
), because there is only one line. If there were more lines, I wanted to pick the last one. From chunk_val[1]
to chunk_val[2]
I have two lines and I want the last one (19
out of 18
and 19
). Then, chunk_val[2]
to chunk_val[3]
there are three line and I want the last one (22
out of 20
, 21
and 22
). From chunk_value[3]
to chunk_value[4]
again there are three line and I want the last one (25
out of 23
, 24
and 25
). There is one very important note on how to pick these connecting (blue) lines: I want to start picking them from the first chunk_val
untill where values of chunk_val
change. In my example until the last number of chunk_val
I see a change (chunk_val[-2]
is 3
while chunk_val[-1]
is 4
). If chunk_val[-1]
was also 3
, then I wanted to stop picking the blue lines after going from chunk_val[1]
to chunk_val[2]
(I mean I only wanted to have 17
and 19
). These are the idea of my algorithm but I have no idea to code it in python. In summary I want to have the following list of numbers:
[17, 26, 19, 28, 22, 25, 33]
在此,我感谢您所做的任何贡献.
In advance, I do appreciate any contribution.
推荐答案
通过反思,可以找到一个更简单的解决方案(也可以很容易地将其应用于
Upon reflection, there is a simpler solution to this problem (which can also be easily adapted for its twin problem):
import numpy as np
from scipy.ndimage.interpolation import shift
# Input data
line_no = np.arange (17, 34)
sep = 25 # lines from 17 to 25 are blue and from 26 to end are red
chunk_val = np.array([1,2,3,3,4])
# Blue candidates
blue_A = line_no[:np.where(line_no == sep)[0][0]+1]
# Red candidates
red_A = line_no[np.where(line_no == sep)[0][0]+1:]
# Select blue lines
blue_index = (chunk_val[:-1]).cumsum() - 1
blues = blue_A[blue_index]
blue_shift = (chunk_val[-1] - chunk_val)[:-1]
blues[blue_shift == 0] = 0 # set value of zero if no further jagged lines
# Select red lines
red_index = (chunk_val[1:] - 1).cumsum() - 1
reds = red_A[red_index]
red_shift = (chunk_val - shift(chunk_val, 1, cval=0))[1:] # increments between successive series of red lines
reds[red_shift == 0] = 0 # set value of zero if no increment
# Merge blue lines and red lines
result = np.ravel(np.column_stack((blues, reds)))
result = result[result != 0] # remove zeros
print(result)
输出:
[17 26 19 28 22 25 33]
这篇关于如何编写算法以在python中查找numpy数组的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!