Home > Back-end >  Getting the max, min and last index of formatted arrays Numpy Python
Getting the max, min and last index of formatted arrays Numpy Python

Time:01-12

I want to make a code that goes through the lists within vals array one by one for each unique digit_vals value. The digit_vals value shows the nth number for the expected output, so since the first value in digit_vals is 24 then it means that all the numbers before it will be a filled with zeroes and the 24th number will contain value from vals. Since there are two 24s within digit_vals it means that the 2nd index within the first list of vals is the last index so it will take ([-3.3, -4.3]) to get the max:-3.3,min:-4.3 and last index: -4.3 value out of ([-3.3, -4.3, 23.05, 23.08, 23.88, 3.72]) will contain the 24th value in the Expected Output. The 4th index of the 2nd list within vals will contain the value for the 27th value in digit_vals and so on. The gaps between the digit_vals will be filled with zeroes as well in the results so between 24 and 27 there will be 2 zeroes for the 25th and 26th place respectively. I want modify out_arr to create a 2d array with identical values like [[zeroes],[zeroes],[zeroes]]. The max and min values also don't work max/min(vals[r_ind][v_ind]).How would I be able to fix those 2 functions?

import pandas as pd 
import numpy as np 

digit_vals = np.array([24, 24, 27, 27, 27, 27,
                       28, 28, 28, 31])

vals = np.array([list([-3.3, -4.3, 23.05, 23.08, 23.88, 3.72]),
 list([2.3, 2.05, 3.08, -4.88, 4.72]),
 list([5.3, 2.05, 6.08, -13.88, -17.2]),
 list([9.05, 6.08, 3.88, -13.72])], dtype=object)

def Monthly_CAP_movement():
    #Val_ind is used to count the number of repetitive numbers
    #out_ind shows the unique numbers 

    val_ind = []
    out_ind = []
    for ind, cnt in enumerate(np.bincount(digit_vals)):
        if cnt > 0:
            val_ind.append(cnt-1)
            out_ind.append(ind)

    # Turn the out_arr function to a 2 dimensional of coppied arrays [[zeroes],[zeroes],[zeroes]]
    # Assign 3 of the zeroes one for each (last index, Max and Min)
    out_arr = np.zeros(np.max(digit_vals) 1)
    for r_ind, (v_ind, o_ind) in enumerate(zip(val_ind, out_ind)):
        # Last Index Recording
        out_arr[0][o_ind] = vals[r_ind][v_ind]
        # Max Recording 
        out_arr[1][o_ind] = max(vals[r_ind][v_ind])
        # Min Recording
        out_arr[2][o_ind] = min(vals[r_ind][v_ind])

Expected Output:

Last Index: [ 0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
  0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
 -4.3   0.    0.   -4.88  6.08  0.    0.    9.05]
Max Value: [ 0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
  0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
 -3.3   0.    0.   3.08   6.08  0.    0.    9.05]
Min Value: [ 0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
  0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.    0.
 -4.3   0.    0.   -4.88  2.05  0.    0.    9.05]

CodePudding user response:

Please review list/array slicing in numpy (e.g. Resulting Output

A slice doesn't return the last value so that is why you need the v_ind 1, as noted in the links above.

  •  Tags:  
  • Related