Skip to content Skip to sidebar Skip to footer

Using Multiple Functions With Correlated Arrays Numpy Python

The function below is supposed compute either the Uval and Lval functions in accordance to Set and Numbers. For the first two numbers U and 52599 are in relation and L and 52550 ar

Solution 1:

your slice notation isn't doing what you think it is:

in: Numbers[:-1] 

does not shift elements in the array by 1. It takes all elements up to but not including the last element. The output is:

in: Numbers[:-1] out: array([ 52599,  52550,  53598, 336368, 336875, 337466, 338292, 356587,
   357474, 357763, 358491, 358659, 359041, 360179])

That is every element in Numbers except for the last, 360286.

You need to use the np.roll() function to shift elements by one. This will roll the last element to the front though:

in: np.roll(Numbers, 1)
out: array([360286,  52599,  52550,  53598, 336368, 336875, 337466, 338292,
   356587, 357474, 357763, 358491, 358659, 359041, 360179])

so you have to handle the edge case of what to do with the very first and very last numbers, e.g. your arrays are length 15, so you can only get 14 new elements for either L or U.

Post a Comment for "Using Multiple Functions With Correlated Arrays Numpy Python"