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

前端 未结 3 1745
无人及你
无人及你 2021-01-01 20:34

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

相关标签:
3条回答
  • 2021-01-01 21:28

    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)
    
    0 讨论(0)
  • 2021-01-01 21:30

    If you want to know more about finding indices that match values you seek, also have a look at numpy.argmin and numpy.where. Implementing the former:

    import numpy as np
    
    test = np.arange(17)
    
    def nearest_index(array, value):
        return (np.abs(np.asarray(array) - value)).argmin()
    
    def evenly_spaced_indices(array, steps):
        return [nearest_index(array, value) for value in np.linspace(np.min(array), np.max(array), steps)]
    
    print(evenly_spaced_indices(test,4))
    

    You should keep in mind that this is an unnecessary amount of function calls for the initial question you asked as switftly demonstrated by coldspeed. np.round intuitively rounds to the closest matching integer serving as index, implementing a similar process but optimised in C++. If you are interested in the indices too, you could have your function simply return both:

    import numpy as np
    
    def GetSpacedElements(array, numElems=4, returnIndices=False):
        indices = np.round(np.linspace(0, len(arr) - 1, numElems)).astype(int)
        values = array[indices]
        return (values, indices) if returnIndices else (values)
    
    arr = np.arange(17) + 42
    print(arr)
    print(GetSpacedElements(arr, 4))                            # values only
    print(GetSpacedElements(arr, 4, returnIndices=True)[0])     # values only
    print(GetSpacedElements(arr, 4, returnIndices=True)[1])     # indices only
    
    0 讨论(0)
  • 2021-01-01 21:35

    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')
    
    0 讨论(0)
提交回复
热议问题