[英]Find n-dimensional point in numpy array
I am investigating whether storing points in a numpy array helps me search for points, and I have several questions about it. 我正在研究将点存储在numpy数组中是否可以帮助我搜索点,对此我有几个问题。
I have a Point class that represents a 3-dimensional point. 我有一个表示3维点的Point类。
class Point( object ):
def __init__( self, x, y, z ):
self.x = x
self.y = y
self.z = z
def __repr__( self ):
return "<Point (%r, %r, %r)>" % ( self.x, self.y, self.z )
I build a list of Point objects. 我建立了一个Point对象列表。 Notice that the coordinates
(1, 2, 3)
deliberately occurs twice; 请注意,坐标
(1, 2, 3)
故意出现两次; that is what I am going to search for. 那就是我要寻找的。
>>> points = [Point(1, 2, 3), Point(4, 5, 6), Point(1, 2, 3), Point(7, 8, 9)]
I store the Point objects in a numpy array. 我将Point对象存储在一个numpy数组中。
>>> import numpy
>>> npoints = numpy.array( points )
>>> npoints
array([<Point (1, 2, 3)>, <Point (4, 5, 6)>, <Point (1, 2, 3)>,
<Point (7, 8, 9)>], dtype=object)
I search for all points with coordinates (1, 2, 3)
in the following manner. 我以以下方式搜索坐标为
(1, 2, 3)
所有点。
>>> numpy.where( npoints == Point(1, 2, 3) )
>>> (array([], dtype=int64),)
But, the result is not useful. 但是,结果没有用。 So, that does not seem to be the correct way to do it.
因此,这似乎不是正确的方法。 Is
numpy.where
the thing to use? 是
numpy.where
要使用的东西吗? Is there another way to express the condition for numpy.where
that would be successful? 还有另一种方式来表达
numpy.where
条件会成功吗?
The next thing I try is to store just the coordinates of the points in a numpy array. 我尝试的下一件事是将点的坐标仅存储在numpy数组中。
>>> npoints = numpy.array( [(p.x, p.y, p.z) for p in points ])
>>> npoints
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3],
[7, 8, 9]])
I search for all points with coordinates (1,2,3)
in the following manner. 我以以下方式搜索坐标为
(1,2,3)
所有点。
>>> numpy.where( npoints == [1,2,3] )
(array([0, 0, 0, 2, 2, 2]), array([0, 1, 2, 0, 1, 2]))
The result is, at least, something I can deal with. 结果至少是我可以处理的。 The array of row indexes in the first return value,
array([0, 0, 0, 2, 2, 2])
, does indeed tell me that the coordinates I am searching for are in rows 0 and 2 of npoints
. 实际上,第一个返回值
array([0, 0, 0, 2, 2, 2])
的行索引array([0, 0, 0, 2, 2, 2])
确实告诉我,我要搜索的坐标位于npoints
0行和第2行中。 I could get away with doing something like the following. 我可以像下面这样做。
>>> rows, cols = numpy.where( npoints == [1,2,3] )
>>> rows
array([0, 0, 0, 2, 2, 2])
>>> cols
array([0, 1, 2, 0, 1, 2])
>>> foundRows = set( rows )
>>> foundRows
set([0, 2])
>>> for r in foundRows:
... # Do something with npoints[r]
However, I feel that I am not really using numpy.where
appropriately, and that I am just getting lucky in this particular situation. 但是,我觉得我并没有真正
numpy.where
适当的地方使用numpy.where
,在这种特殊情况下我很幸运。
What is the appropriate way to find all occurrences of a n-dimensional point (ie, a row with particular values) in a numpy array? 查找numpy数组中所有n维点(即具有特定值的行)的所有出现的合适方法是什么?
Preserving the order of the array is essential. 保持阵列的顺序至关重要。
You can create a “rich comparison” method object.__eq__(self, other)
inside your Point
class to be able to use ==
among Point
objects: 您可以在
Point
类中创建一个“丰富的比较”方法object.__eq__(self, other)
可以在Point
对象之间使用==
:
class Point( object ):
def __init__( self, x, y, z ):
self.x = x
self.y = y
self.z = z
def __repr__( self ):
return "<Point (%r, %r, %r)>" % ( self.x, self.y, self.z )
def __eq__(self, other):
return self.x == other.x and self.y == other.y and self.z == other.z
import numpy
points = [Point(1, 2, 3), Point(4, 5, 6), Point(1, 2, 3), Point(7, 8, 9)]
npoints = numpy.array( points )
found = numpy.where(npoints == Point(1, 2, 3))
print(found) # => (array([0, 2]),)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.