0

I'm trying to figure out an efficient way of taking a numpy array of float values and converting them to an index associated with a specific range.

eg numpy array of x floats [ -999.99, 433.000, -56.00....] (this array is actually quite large typically from 6000 to 25000 values.

The range info effectively consists of a much smaller around 3 to 20 rows (y) of range start values (arranged in ascending sequence) . eg [-9999.0, 0.0, 0.0, 500.0 99999.0]. A value can be repeated as shown with the 0.0 value.

This is then used to build a set of ranges such that start of the range = [:yrows - 2] and end = [1:yrows -1] such that this gives a series of ranges [(-9999.0, 0.0), (0.0, 0.0), (0.0, 500.0), (500.0, 99999.0) with a total number of rows of yrows -1 (an index can then be generated corresponding to each row

What I need to derive is then the equivalent of the index of the y row that the original x float value was in (there will be only one per x float). I will then use the index to derive further information associated with that specific range.

eg indexes of [ -999.99, 433.000, -56.00....] would yield indexvalues[ 0, 2, 0...] Note for clarity the x values are not in any way sorted but will always be larger than the lowest array range value and smaller than the highest array range value.

The indexing would work such that the required index is one where x >= range start and less than range end thus the (0.0, 0.0) entry above is a range that will never get picked and are there to create new end/start attributes of the previous, next ranges only.

OK This was resolved. The numpy.digitize does exactly what I need eg numpy.digitize(xfloatarray, ybins, right=False).

I just hadn't come across the function and couldn't google a find to the function until after posting... I just kept getting results about splitting arrays or creating indexes.

5
  • Have you tried anything so far? Where are you stuck? Commented Jun 27, 2014 at 11:38
  • Hi, I couldn't figure out a function which might help and didn't want to set up a python loop. Typically, just finally found something and think I need to use numpy.digitize. Looks like that will do the trick Commented Jun 27, 2014 at 11:56
  • OK, this was as usual pretty straight forward when you know the right function. numpy.digitize() did the trick nicely. Commented Jun 27, 2014 at 12:14
  • If you solved the problem, it is perfectly acceptable to post it as an answer below, then mark it as the solution. This helps if someone searches Stack Overflow with a similar question. Commented Jun 27, 2014 at 12:15
  • OK thanks, I've now done that. Commented Jun 27, 2014 at 12:27

1 Answer 1

1

OK This was resolved. The numpy.digitize does exactly what I need, e.g.

numpy.digitize(xfloatarray, ybins, right=False)

where xfloatarray was my array of floats [ -999.99, 433.000, -56.00....]

and ybins was my array of range values [-9999.0, 0.0, 0.0, 500.0 99999.0]

the result gives [1,3,1..]

if a value of 0.0 is used in the xfloatarray it returns the value of 3 ie the value of 2 will not be returned.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.