Python: How to find the a unique element pattern from 2 arrays? -
i have 2 numpy
arrays, a
, b
:
a = ([1, 2, 3, 2, 3, 1, 2, 1, 3]) b = ([2, 3, 1, 2])
where b
unique pattern within a
.
i need output elements of a
, aren't present in b
.
output = ([1, 2, 3, 1, 3])
easiest use python's builtins, i.e. string type:
a = "123231213" b = "2312" result = a.replace(b, "")
to efficiently convert numpy.array
str
, use these functions:
x = numpy.frombuffer("3452353", dtype="|i1") x array([51, 52, 53, 50, 51, 53, 51], dtype=int8) x.tostring() "3452353"
(*) mixes ascii codes (1 != "1"
), substring search work fine. data type should better fit in 1 char, or may false match.
to sum up, quick hack looks this:
a = numpy.array([1, 2, 3, 2, 3, 1, 2, 1, 3]) b = numpy.array([2, 3, 1, 2]) numpy.fromstring(a.tostring().replace(b.tostring(), ""), dtype=a.dtype) array([1, 2, 3, 1, 3]) # note, here dtype int, i'm relying on fact that: # "1 matches 1" equivalent "0001 matches 00001" # holds long values of b typically non-zero. # # trick can conceptually used floating point too, # beware of multiple floating point representations of same number
in depth explanation:
assuming size of , b arbitrary, naive approach runs in quadratic time. better, probabilistic algorithms exit, example rabin-karp, relies on sliding window hash.
which main reason text oriented functions, such xxx in str
or str.replace
or re
faster custom numpy
code.
if need function integrated numpy, can write extension, it's not easy :)
Comments
Post a Comment