[Python] Numpy Slicing problem: Possible way to reduce array size while retaining the...

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I want to finding a general formula or algorithm to determine all possible values of `step` that satisfy the three conditions (boundary, equal spacing, and symmetry) when slicing an array with the same properties.

    Let says I have an initial_array = np.linspace(-10, 10, 11)


    [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10]
    ​

    I want to slice the array such that the reduced_array retains 3 condition:

    1. Boundary condition. The minimum and maximum elements should remain unchanged.
    2. Equal spacing property. Spacing between each elements should be constant.
    3. Symmetry property. The whole array should have an symmetry around zero, whether or not the zero is included in the array.

    One way to slice the initial_array is to set reduced_array=initial_array[::2], which get us


    [-10, -6, -2, 2, 6, 10]
    ​

    For initial_array = np.linspace(-10, 10, 11),


    [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    ​

    I can slice it by setting reduced_array=initial_array[::2],


    [-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10]
    ​

    or reduced_array=initial_array[::4]


    [-10, -6, -2, 2, 6, 10]
    ​

    Right now, I am trying to find out, for an arbitrary size of array

    #initial condition
    max = 200
    min = -max
    size= 314
    initial_array = np.linspace(min,max,size)

    step= ? #unknown, to be solved
    reduced_array = initial_array[::step]


    Some pattern I observed is that for size of 11,21 and 31, value of step is the factor of 10,20 and 30 etc.

    Clearly, step<=floor(size/2). Ultimately, I want to locate the reduced_array which does not include 0. One application for me is to create a high resolution linspace for calculation. Then, when doing visualization, I slice it down to a much smaller size for plotting. One example of wanting to exclude 0 from the reduced array is when 0 is a singularity point that cannot be visualized.

    How many and how can I slice the array while keeping all 3 proposed conditions? Is there a way to formulate the problem mathematically?

    Continue reading...

Compartilhe esta Página