【问题标题】:AttributeError- 'numpy.ndarray' object has no attribute 'index'AttributeError-“numpy.ndarray”对象没有属性“index”
【发布时间】:2021-04-03 11:32:06
【问题描述】:

我不断收到此错误消息:

newmaxleft=cl1count.index(max(cl1count))
AttributeError: 'numpy.ndarray' object has no attribute 'index'

代码的目的是找到白色像素最多的列的第一个出现点。

我的代码:

cl = cl[top:bottom, left:right]
            cl1mask = np.uint8(np.where(cl == 0, 0, 1))
            cl1count = cv2.reduce(cl1mask, 0, cv2.REDUCE_SUM, dtype=cv2.CV_32SC1)
            cl1count.flatten().tolist()
            newmaxleft=cl1count.index(max(cl1count))

【问题讨论】:

  • numpy.ndarraypandas.Series 不同。
  • 你想要argmax

标签: python arrays python-3.x indexing


【解决方案1】:

Numpy 没有索引方法。它使用 where (用于一般目的)或某些特定功能。在你的情况下,最好的选择是

newmaxleft = cl1count.argmax()

你也可以使用

newmaxleft = np.where(a==max(a))[0][0]

但效率较低。第一个 [0] 返回 np.array 位置,第二个 [0] 返回第一个匹配项。

【讨论】:

    【解决方案2】:

    发生错误是因为 numpy 数组没有 index 属性,如错误所述。因此,不要使用索引,而是使用newmaxleft=np.where(cl1count == max(cl1count)) 之类的东西,您可以使用newmaxleft[0] 来获得第一次出现。你也可以使用argmax

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-03
      • 2020-11-29
      • 2020-10-06
      • 2018-01-25
      • 2016-06-29
      • 2020-03-25
      • 2013-12-07
      • 2017-10-16
      相关资源
      最近更新 更多