Friday 15 June 2012

numpy - Speeding up summation for loop in python -


I have the following constraints and I wonder if anyone can suggest ways to speed it up

I have three lists of x, y, z length, N . And I apply the following

  def abs_val_diff (x1, x2, x3, y1, y2, y3): "Find the full value of the difference between x and y" py For .jq in xrange (i + 1, n) xrange (x1 - y1): for x in xrange (n) 2.0 + (x2 - y2) ** 2.0 + (x3 - y3) ** 2.0) r = 0.1 sumV = 0.0: If R & gt; I have tried to use I + NMP array, but either I am doing something wrong or there is a lack of speed of one factor of 2.  

Any idea will be highly appreciated

I believe that You can use numpy a little more efficiently by doing something like following. Numpy.sqrt: Make a small amendment to your function to import samples in the form of np def abs_val_diff (x1, x2, x3, y1, y2, y3): " "Absolute Find Price of the Difference Between X and Y np.sqrt ((x1 - y1) ** 2.0 + (x2 - y2) ** 2.0 + (x3 - y3) ** 2.0)

then call with full array:

  res = abs_val_diff (x [: - 1], y [: - 1], z [: - 1], x [ 1:], Y [1:], z [1:])   

Then, because you are adding 1 to each match, you simply take the length of the array generated from a query Against the result: sumV = len (res [R> res])

This gives numpy running handle hopes that works for you

No comments:

Post a Comment