if input array general objects numpy.unique, result unique based upon what?
i have tried:
import numpy np class a(object): #probably exists nice mixin :p def __init__(self, a): self.a = def __lt__(self, other): return self.a < other.a def __le__(self, other): return self.a <= other.a def __eq__(self, other): return self.a == other.a def __ge__(self, other): return self.a >= other.a def __gt__(self, other): return self.a > other.a def __ne__(self, other): return self.a != other.a def __repr__(self): return "a({})".format(self.a) def __str__(self): return self.__repr__() np.unique(map(a, range(3)+range(3))) which returns
array([a(0), a(0), a(1), a(1), a(2), a(2)], dtype=object) but intentions get:
array([a(0), a(1), a(2)], dtype=object)
assuming duplicate a(2) typo, think need define __hash__ (see docs):
import numpy np functools import total_ordering @total_ordering class a(object): def __init__(self, a): self.a = def __lt__(self, other): return self.a < other.a def __eq__(self, other): return self.a == other.a def __ne__(self, other): return self.a != other.a def __hash__(self): return hash(self.a) def __repr__(self): return "a({})".format(self.a) def __str__(self): return repr(self) produces
>>> map(a, range(3)+range(3)) [a(0), a(1), a(2), a(0), a(1), a(2)] >>> set(map(a, range(3)+range(3))) set([a(0), a(1), a(2)]) >>> np.unique(map(a, range(3)+range(3))) array([a(0), a(1), a(2)], dtype=object) where i've used total_ordering reduce proliferation of methods, guessed possible. :^)
[edited after posting correct missing __ne__.]
Comments
Post a Comment