Issue
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]
Solution
Please review list/array slicing in numpy (e.g. list slicing or array slicing). First off, out_arr
should be initialized with three rows. Then, the min and max need to be computed across a slice containing all values up to the v_ind
value:
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)
out_arr = np.zeros((3, np.max(digit_vals)+1))
for r_ind, (v_ind, o_ind) in enumerate(zip(val_ind, out_ind)):
out_arr[0, o_ind] = vals[r_ind][v_ind]
out_arr[1, o_ind] = np.max(vals[r_ind][:v_ind+1])
out_arr[2, o_ind] = np.min(vals[r_ind][:v_ind+1])
A slice doesn't return the last value so that is why you need the v_ind+1
, as noted in the links above.
Answered By - frederick-douglas-pearce
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.