【问题标题】:Use of np.where()[0]使用 np.where()[0]
【发布时间】:2019-03-10 00:20:05
【问题描述】:

我的代码检测到低于阈值的所有点,然后定位起点和终点。

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]

这里的 np.where() 函数之后我并没有真正得到 [0] 的使用

【问题讨论】:

  • 试试print(below)[0] 看看有什么不同。这些事情你可以很容易地调查自己

标签: python numpy


【解决方案1】:

np.where 很棘手。它返回一个 列表数组 满足条件的索引,即使条件永远不会满足。 特别是在 np.where(my_numpy_array==some_value)[0] 的情况下,这意味着您想要数组中的第一个值,它是一个列表,其中包含条件满足单元格的索引列表。

一口。简单来说,np.where(array==x)[0] 返回满足条件的索引列表。我猜这是为广泛的应用程序设计 numpy 的结果。

请记住,没有匹配项仍然返回一个空列表only size-1 arrays can be converted to python (some type) 之类的错误可能与此有关。

【讨论】:

    【解决方案2】:
    below = np.where(self.data < self.threshold)[0]
    

    意思是:

    从 np.where() 返回的 ndarrays 元组中取出第一个元素,然后 将其分配给below

    【讨论】:

      猜你喜欢
      • 2019-08-11
      • 2018-06-04
      • 2019-10-03
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      相关资源
      最近更新 更多