[英]Use of np.where()[0]
My code detects all the points under a threshold, then locates the start and end points. 我的代码检测到阈值以下的所有点,然后找到起点和终点。
below = np.where(self.data < self.threshold)[0]
startandend = np.diff(below)
startpoints = np.insert(startandend, 0, 2)
endpoints = np.insert(startandend, -1, 2)
startpoints = np.where(startpoints>1)[0]
endpoints = np.where(endpoints>1)[0]
startpoints = below[startpoints]
endpoints = below[endpoints]
I don't really get the use of [0] after np.where() function here 我在这里的np.where()函数之后并没有真正使用[0]
below = np.where(self.data < self.threshold)[0]
means: 手段:
take the first element from the tuple of ndarrays returned by np.where() and assign it to
below
.从np.where()返回的ndarrays元组中获取第一个元素,并将其分配给
below
。
np.where
is tricky. np.where
很棘手。 It returns an array of lists of indices where the conditions are met, even if the condition is never satisfied. 它返回满足条件的索引列表的数组 , 即使条件从未满足。 In the case of
np.where(my_numpy_array==some_value)[0]
specifically, this means that you want the first value in the array, which is a list, and which contains the list of indices of condition-meeting cells. 特别是在
np.where(my_numpy_array==some_value)[0]
的情况下,这意味着您需要数组中的第一个值,该值是一个列表,并且包含满足条件的单元格的索引列表。
Quite a mouthful. 满口。 In simple terms,
np.where(array==x)[0]
returns a list of indices where the conditions have been met. 简单来说,
np.where(array==x)[0]
返回满足条件的索引列表。 I'm guessing this is a result of designing numpy for extensively broad applications. 我猜这是为广泛应用程序设计numpy的结果。
Keep in mind that no matches still returns an empty list ; 请记住,没有匹配项仍返回空列表 ; errors like
only size-1 arrays can be converted to python (some type)
may be attributed to that. only size-1 arrays can be converted to python (some type)
类的错误only size-1 arrays can be converted to python (some type)
可能是由于该错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.