Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select N evenly spaced out elements in array, including first and last

Tags:

I have an array of arbitrary length, and I want to select N elements of it, evenly spaced out (approximately, as N may be even, array length may be prime, etc) that includes the very first arr[0] element and the very last arr[len-1] element.

Example:

>>> arr = np.arange(17) >>> arr array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16]) 

Then I want to make a function like the following to grab numElems evenly spaced out within the array, which must include the first and last element:

GetSpacedElements(numElems = 4) >>> returns 0, 5, 11, 16 

Does this make sense?

I've tried arr[0:len:numElems] (i.e. using the array start:stop:skip notation) and some slight variations, but I'm not getting what I'm looking for here:

>>> arr[0:len:numElems] array([ 0,  4,  8, 12, 16]) 

or

>>> arr[0:len:numElems+1] array([ 0,  5, 10, 15]) 

I don't care exactly what the middle elements are, as long as they're spaced evenly apart, off by an index of 1 let's say. But getting the right number of elements, including the index zero and last index, are critical.

like image 827
JDS Avatar asked Jun 04 '18 16:06

JDS


People also ask

Which function is used to create an array with evenly spaced points?

Creating Sequential Arrays: arange and linspace The linspace function allows you to generate evenly-spaced points within a user-specified interval ( and are included in the interval).


2 Answers

To get a list of evenly spaced indices, use np.linspace:

idx = np.round(np.linspace(0, len(arr) - 1, numElems)).astype(int) 

Next, index back into arr to get the corresponding values:

arr[idx] 

Always use rounding before casting to integers. Internally, linspace calls astype when the dtype argument is provided. Therefore, this method is NOT equivalent to:

# this simply truncates the non-integer part idx = np.linspace(0, len(array) - 1, numElems).astype(int) idx = np.linspace(0, len(arr) - 1, numElems, dtype='int') 
like image 100
cs95 Avatar answered Nov 03 '22 20:11

cs95


Your GetSpacedElements() function should also take in the array to avoid unfortunate side effects elsewhere in code. That said, the function would need to look like this:

import numpy as np  def GetSpacedElements(array, numElems = 4):     out = array[np.round(np.linspace(0, len(array)-1, numElems)).astype(int)]     return out  arr = np.arange(17) print(array) spacedArray = GetSpacedElements(arr, 4) print (spacedArray) 
like image 45
Justin Avatar answered Nov 03 '22 21:11

Justin