Quick problem
I would like to be able to compare specific fields of type dtype from two massive numpy arrays, which are guaranteed to have the same type of dtype. I would like to do this in such a way that the fields that we compare with each other, every time the function is called based on the given inputs (i.e. I cannot easily copy comparisons for each individual field)
Long problem with examples
I am trying to compare specific fields from two massive numpy arrays with the same type. for example, say that
import numpy as np
from io import BytesIO
a = np.genfromtxt(BytesIO('12 23 0|23.2|17.9|0\n12 23 1|13.4|16.9|0'.encode()),dtype=[('id','U7'),('pos',[('x',float),('y',float)]),('flag','U1')],delimiter='|')
b = np.genfromtxt(BytesIO(' |23.0|17.91|0'.encode()),dtype=[('id','U7'),('pos',[('x',float),('y',float)]),('flag','U1')],delimiter='|')
which gives
In[156]: a
Out[154]:
array([('12 23 0', (23.2, 17.9), '0'), ('12 23 1', (13.4, 16.9), '0')],
dtype=[('id', '<U7'), ('pos', [('x', '<f8'), ('y', '<f8')]), ('flag', '<U1')])
and
In[153]: b
Out[151]:
array([('', (23.0, 17.91), '0')],
dtype=[('id', '<U7'), ('pos', [('x', '<f8'), ('y', '<f8')]), ('flag', '<U1')])
Now let's say that I want to check and find any entries in awhose field is a['pos']['x']larger than the field b['pos']['x']and return these entries to a new numpy array, something like this will work
newArr = a[a["pos"]["x"]>b["pos"]["x"]]
, a, x y , b. ,
newArr = a[np.array([np.array([a['pos']['x']>b['pos']['x']),a['pos']['y']>b['pos']['y'])).all(axis=0)]
, .
, , , dtype (, 34 - . dtype, ), , , , ( , dtype , ). , , , run to run ( , ). .
()
, , . - (, ):
mask = np.ones(z.shape,dtype=[('id',bool),('pos',[('x',bool),('y',bool)]),('flag',bool)])
# unmask the x and y fields so we can compare them
mask['pos']['x']=0
mask['pos']['y']=0
maskedA = np.ma.masked_array(a, mask=mask)
# We need to do this or the masked array gets angry (at least in python 3)
b.shape = (1,)
maskedB = np.ma.masked_array(b, mask=mask)
-
test = (maskedA>maskedB).any(axis=1)
, , -
TypeError: unorderable types: MaskedArray() > MaskedArray()
test = (maskedA.compressed()>maskedB.compressed()).any(axis=1)
TypeError: ufunc 'logical_not' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
, , , , , , . - ?
, , , , , ...
, , , , , b. numpy , a. , , , .
.
, , , , , , , - , / numpy, , () , - , , . , MWE, " ", , .