Random sampling in python from a list of values with new list having elements in power of 2. i.e, 32, 64, 128 etc

Question:

I have a list with 601 sorted values which are equidistant from each other starting from -15 to 15.
i.e A = [-15, -14.95, -14.90, …, 14.95, 15]. However, I want to create a new list with elements starting from the first value(-15). The no of elements in the new list should be in the power of 2. i.e 32 or 64 or 128 or 256 etc.

New_list = [2^n] elements. where n = 4 or 5 or 6 or 7 etc

Also these values in the new list should be from my list but equidistant(difference between 2 consecutive might differ from 0.05, which is the distance in the original list). Is there any way to do it using some library function preferably in Python. Any help appreciated.

Asked By: Dhruv

||

Answers:

How about this:

import numpy as np


def get_sample(arr, power):
    num_elements = 2 ** power
    spacing = len(arr) // num_elements
    stop = spacing * num_elements

    sample = arr[:stop:spacing]
    return sample


A = np.linspace(-15, 15, 601)
print(get_sample(A, 0))
print(get_sample(A, 1))
print(get_sample(A, 2))
print(get_sample(A, 3))
print(get_sample(A, 4))
print(get_sample(A, 5))

Has the following results:

[-15.]
[-15.   0.]
[-15.  -7.5    0.     7.5]
[-15. -11.25  -7.5   -3.75   0.     3.75   7.5  11.25]
[-15. -13.15 -11.3   -9.45  -7.6   -5.75  -3.9  -2.05 -0.2  1.65 ...
[-15. -14.1  -13.2  -12.3  -11.4  -10.5   -9.6  -8.7  -7.8 -6.9  ...
Answered By: MangoNrFive
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.