【问题标题】:Filtering with itemgetter and numpy arrays使用 itemgetter 和 numpy 数组过滤
【发布时间】:2013-04-22 21:37:53
【问题描述】:

我收到以下错误:

Traceback (most recent call last):
  File "calibrating.py", line 160, in <module>
    intrinsic = calibrate2(corners, cb_points, (640,480))
  File "calibrating.py", line 100, in calibrate2
    valid_corners = filter(itemgetter(0), image_corners)
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

image_corners 是一个 numpy 数组的列表,即

[array([[ 261.45239258,  140.88212585],
   [ 301.11242676,  156.306427  ],
   [ 343.38937378,  168.20132446],
   [ 382.79559326,  180.48405457],...
   [ 392.16989136,  338.6171875 ],
   [ 439.97772217,  337.2124939 ]], dtype=float32), ... ]

我想做的是在没有dtype=float32 的情况下获取矩阵,我做错了什么?

【问题讨论】:

  • 您能否澄清一下“采用没有 dtype=float32 的矩阵”是什么意思?
  • 什么是“有效角”?除了[0,0],还有其他意义吗? (你想用filter 消除什么?)
  • 用“取不带 dtype=float32 的矩阵”我的意思是我想取出 numpy 数组元组的第二个元素,换句话说,得到一个“传统”数组。
  • 有效角是我给列表的名称,其中包含我可以操作的矩阵。

标签: python arrays matrix numpy


【解决方案1】:

itemgetter 无法访问 dtype 属性。

试试这个过滤器:

filter(lambda arr: arr.dtype != float32, image_corners)

这将为您提供没有dtype==float32 的所有矩阵。

【讨论】:

    【解决方案2】:

    或者list comprehensions:

    [a for a in image_corners if a.dtype is not float32]
    

    【讨论】:

      猜你喜欢
      • 2019-01-31
      • 2018-02-15
      • 2021-04-17
      • 1970-01-01
      • 2020-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多