Home > OS >  Getting max ,min and last index value within multidimensional arrays
Getting max ,min and last index value within multidimensional arrays

Time:10-16

The output of result are 3 arrays that are 2 dimensional with lengths that are getting decremented by one. I want to write a code that gets the ending index value last_incs, max value maxs and the minimum values mins. It should iterate through all the rows of each of the 2nd dimensional array, for example the result output for [-3,-1,-2,1] is array([array([ 0., 0., 0., 25.]),array([ 0,0, 33.33333333]), array([ 0., 50.]),array([100.])], dtype=object). The maximum value in each of these sub array are as follows: [25.0, 33.33333333333333, 50.0, 100.0] which is shown in the Expected Outputs below in Max:. How would I be able to do this?

import numpy as np 

def run(*args):
    
    result = np.array([np.array([((arr[i:] > 0).cumsum()/ np.arange(1, len(arr[i:]) 1) * 100) for i in range(len(arr))],dtype=object) for arr in args], dtype=object)
    #print(result)
    last_inc = result[-1]
    maxs = np.max(result)
    mins = np.min(result)
            
run(np.array([12,12,-3,-1,2,1]), np.array([-3,-1,-2,1]), np.array([12,-12]))

Expected Output:

last incs: [array([66.66666666666666, 60.0, 50.0, 66.66666666666666, 100.0, 100.0],
       dtype=object)
 array([25.0, 33.33333333333333, 50.0, 100.0], dtype=object)
 array([50.0, 0.0], dtype=object)]]

mins: [array([50.0, 33.33333333333333, 0.0, 0.0, 100.0, 100.0], dtype=object)
 array([0.0, 0.0, 0.0, 100.0], dtype=object)
 array([50.0, 0.0], dtype=object)]

maxs: [array([100.0, 100.0, 50.0, 66.66666666666666, 100.0, 100.0], dtype=object)
 array([25.0, 33.33333333333333, 50.0, 100.0], dtype=object)
 array([100.0, 0.0], dtype=object)]

CodePudding user response:

The following code gets your needed outputs, use it in your function:

    size = np.empty(0)
    for i in result:
        size = np.append(size, np.size(i))

    results_arrays = np.empty(0)
    for i in np.hstack(result).T:
        last_incs = np.float64(np.vstack(i)[-1])
        maxs = np.max(np.vstack(i))
        mins = np.min(np.vstack(i))
        results_arrays = np.append(results_arrays, np.array([last_incs, maxs, mins]))

    last_incs = np.array_split(results_arrays[0::3], np.cumsum(size, axis=0).astype(int), axis=0)[:-1]
    maxs = np.array_split(results_arrays[1::3], np.cumsum(size, axis=0).astype(int), axis=0)[:-1]
    mins = np.array_split(results_arrays[2::3], np.cumsum(size, axis=0).astype(int), axis=0)[:-1]
  • Related