-2

I am guessing there a fast way to do this. I have 3 arrays of same size which represent coordinate of x,y,z such as:

In[85]: xxn
Out[85]: 
array([ 0.08333333,  0.08333333,  0.08333333,  0.08333333,  0.08333333,
        0.08333333,  0.08333333,  0.08333333,  0.08333333,  0.25      ,
        0.25      ,  0.25      ,  0.25      ,  0.25      ,  0.25      ,
        0.25      ,  0.25      ,  0.25      ,  0.5       ,  0.5       ,
        0.5       ,  0.5       ,  0.5       ,  0.5       ,  0.5       ,
        0.5       ,  0.5       ,  1.        ,  1.        ,  1.        ,
        1.        ,  1.        ,  1.        ,  1.        ,  1.        ,
        1.        ,  2.        ,  2.        ,  2.        ,  2.        ,
        2.        ,  2.        ,  2.        ,  2.        ,  2.        ,
        3.        ,  3.        ,  3.        ,  3.        ,  3.        ,
        3.        ,  3.        ,  3.        ,  3.        ,  4.        ,
        4.        ,  4.        ,  4.        ,  4.        ,  4.        ,
        4.        ,  4.        ,  4.        ,  5.        ,  5.        ,
        5.        ,  5.        ,  5.        ,  5.        ,  5.        ,
        5.        ,  5.        ])
yyn
Out[86]: 
array([ 1306.89 ,  1524.705,  1742.52 ,  1960.335,  2178.15 ,  2395.965,
        2613.78 ,  2831.595,  3049.41 ,  1306.89 ,  1524.705,  1742.52 ,
        1960.335,  2178.15 ,  2395.965,  2613.78 ,  2831.595,  3049.41 ,
        1306.89 ,  1524.705,  1742.52 ,  1960.335,  2178.15 ,  2395.965,
        2613.78 ,  2831.595,  3049.41 ,  1306.89 ,  1524.705,  1742.52 ,
        1960.335,  2178.15 ,  2395.965,  2613.78 ,  2831.595,  3049.41 ,
        1306.89 ,  1524.705,  1742.52 ,  1960.335,  2178.15 ,  2395.965,
        2613.78 ,  2831.595,  3049.41 ,  1306.89 ,  1524.705,  1742.52 ,
        1960.335,  2178.15 ,  2395.965,  2613.78 ,  2831.595,  3049.41 ,
        1306.89 ,  1524.705,  1742.52 ,  1960.335,  2178.15 ,  2395.965,
        2613.78 ,  2831.595,  3049.41 ,  1306.89 ,  1524.705,  1742.52 ,
        1960.335,  2178.15 ,  2395.965,  2613.78 ,  2831.595,  3049.41 ])

    In[87]: zzn
Out[87]: 
array([ 0.4837052 ,  0.3976288 ,  0.3076519 ,  0.2105963 ,  0.1015546 ,
        0.1162558 ,  0.1723646 ,  0.2173536 ,  0.2547635 ,  0.3767569 ,
        0.3196527 ,  0.2606447 ,  0.1983554 ,  0.1291423 ,  0.09786849,
        0.1277448 ,  0.1560009 ,  0.1802875 ,  0.3420683 ,  0.2938885 ,
        0.2452067 ,  0.1958042 ,  0.144459  ,  0.1026045 ,  0.1086459 ,
        0.1256328 ,  0.1419562 ,  0.3090272 ,  0.2726449 ,  0.236535  ,
        0.200679  ,  0.1647521 ,  0.1310315 ,  0.1132389 ,  0.1129602 ,
        0.118809  ,  0.284265  ,  0.257173  ,  0.2310047 ,  0.205817  ,
        0.18154   ,  0.1586908 ,  0.1393701 ,  0.1264879 ,  0.1204383 ,
        0.2760804 ,  0.2540095 ,  0.2330927 ,  0.2133592 ,  0.1947658 ,
        0.1775263 ,  0.1622754 ,  0.1498286 ,  0.1407699 ,  0.274541  ,
        0.2560495 ,  0.2387175 ,  0.222547  ,  0.2075007 ,  0.1936717 ,
        0.1812974 ,  0.1706293 ,  0.1618527 ,  0.2802191 ,  0.2641784 ,
        0.2491889 ,  0.2352521 ,  0.2223443 ,  0.2105051 ,  0.199825  ,
        0.1903785 ,  0.1822064 ])

I want to figure out the fastest way possible to get zzn values based on a a matching position in xxn and yyn such has [1, 2395.965] would return 0.1310315 which is the position matching position of [1, 2395.965] in array zzn.

in pandas I would do zz[(xx == 1) & (yy == 2395.965)] = 0.1310315 but unfortunately there is a massive loop over it and it's way to slow.

I would appreciate any help thanks!

edit:

my current loop is using pandas such has

for coordinate in df.itertuples():
    sTL = zz[(xx == x_match) & (yy == y_match)].values
    sBL = zz[(xx == x_match) & (yy == sB)].values
    sTR = zz[(xx == sR) & (yy == y_match)].values
    sBR = zz[(xx == sR) & (yy == sB)].values

where coordinate is x_match, y_match, sR, sB values and has 100k rows

Divakar
  • 218,885
  • 19
  • 262
  • 358
Steven G
  • 16,244
  • 8
  • 53
  • 77

3 Answers3

1

You could stack xxn and yyn into one array, search this new array and use the result to get the value from zzn:

a = numpy.vstack((xxn, yyn)).T

idx = numpy.all(a==numpy.array([1.0, 2395.965]), axis=1)
print zzn[idx]
Nils Werner
  • 34,832
  • 7
  • 76
  • 98
  • I would be to consider (by the OP) replacing `a == np.array([....])` with `np.isclose(a, np.array(...))` as the `==` operator doesn't play very well with floats. – Imanol Luengo Aug 17 '16 at 15:16
0

after investigation I figured an easy way to do it like this :

np.where((xxn == x_match) & (yyn ==y_match), zzn, 0).sum()

this looks much faster than the pandas equivalent :

 %timeit np.where((xxn == x_match) & (yyn ==y_match), zzn, 0).sum()
The slowest run took 8.72 times longer than the fastest. This could mean 

that an intermediate result is being cached.
100000 loops, best of 3: 8.19 �s per loop

 %timeit zz[(xx == x_match) & (yy == y_match)].values
1000 loops, best of 3: 1.43 ms per loop
Steven G
  • 16,244
  • 8
  • 53
  • 77
0

Here's how I'd do it in Pandas:

xyz = pd.DataFrame({'x':xxn, 'y':yyn, 'z':zzn})
xyz.set_index(['x', 'y'], inplace=True)

hunt = pd.DataFrame({'x':df[:,0], 'y':df[:,1]}) # coords to look for
print hunt.join(xyz, ['x', 'y'])
John Zwinck
  • 239,568
  • 38
  • 324
  • 436