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 are in relation, since the second number has the label L the Lval equation is used. The previous number is function is previous = numbers[1:] and the current function is current = numbers[:-1]. So (52550 - 52599)/52550 * -100 will be the computation for the first two numbers. The equations are supposed to be computed until the end of the Set and Numbers arrays. However the code gives me the error down below, both the Set and Numbers array have the length of 15.
Function:
Set = np.array(['U', 'L', 'U', 'L', 'U', 'L', 'U', 'L', 'U', 'L', 'U', 'L', 'U', 'L', 'U'])
Numbers = np.array([ 52599, 52550, 53598, 336368, 336875, 337466, 338292, 356587, 357474, 357763, 358491, 358659, 359041, 360179, 360286])
Lval = (Numbers[:-1] - Numbers[1:])/Numbers[:-1] * -100
Uval = (Numbers[1:] - Numbers[:-1])/ Numbers[1:] * -100
Numbers * np.where(Set == 'U', Uval, Lval)
Error Output:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-12-3ddad609950b> in <module>
4 Uval = (Numbers[1:] - Numbers[:-1])/ Numbers[1:] * -100
5 print(len(Set))
----> 6 Numbers * np.where(Set == 'U', Uval, Lval)
<__array_function__ internals> in where(*args, **kwargs)
ValueError: operands could not be broadcast together with shapes (15,) (14,) (14,)
Numbers = np.array([ 1, 52599, 52550, ..., 360179, 360286, 1])Fake values depend on your context. In this way you are defining a previous value for the first element and a subsequent value for the last one. When you apply where, just discard padding valuesNumbers[1:-1] * np.where(Set == 'U', Uval, Lval)