Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding range of a numpy array elements

I have a NumPy array of size 94 x 155:

a = [1  2  20  68  210  290..      2  33 34  55  230  340..      .. .. ... ... .... .....] 

I want to calculate the range of each row, so that I get 94 ranges in a result. I tried looking for a numpy.range function, which I don't think exists. If this can be done through a loop, that's also fine.

I'm looking for something like numpy.mean, which, if we set the axis parameter to 1, returns the mean for each row in the N-dimensional array.

like image 759
khan Avatar asked Oct 03 '12 03:10

khan


People also ask

How do you find the range of a NumPy array in Python?

In this case axis=1 means you want to find the range of each row of your data so it would return a N element numpy array where N is the number of rows. If axis=0 , it would find the range of each column independently.

What is NumPy range?

NumPy arange() is one of the array creation routines based on numerical ranges. It creates an instance of ndarray with evenly spaced values and returns the reference to it. You can define the interval of the values contained in an array, space between them, and their type with four parameters of arange() : numpy.

How do you Range an array in Python?

The "array" is called list in Python. arrayX = [] . for i in range(20080711, 20080714): arrayX. append(i) This will result [20080711, 20080712, 20080713, 20080714] .


2 Answers

I think np.ptp might do what you want:

http://docs.scipy.org/doc/numpy/reference/generated/numpy.ptp.html

r = np.ptp(a,axis=1) 

where r is your range array.

like image 73
JoshAdel Avatar answered Sep 19 '22 14:09

JoshAdel


Try this:

def range_of_vals(x, axis=0):     return np.max(x, axis=axis) - np.min(x, axis=axis) 
like image 35
Austin Waters Avatar answered Sep 16 '22 14:09

Austin Waters