1

I am trying to modify the values of the values down below to the expected values. The function down below is meant to sum out all the values between 2 consecutive elements of limits. none of the values are between 0 and 2 within Numbers so the resultant is 0. However the values between 2 and 5 are 3,4 within Numbers so the resultant is 3+4=7. The function has been gotten from issue: issue.

def formating(a, b):
    
    # Formating goes here
    x = np.sort(b);
    # digitize
    l = np.digitize(a, x)
    # output:
    result = np.bincount(l, weights=a)
    return result

Numbers = np.array([3, 4, 5, 7, 8, 10,20])
limit1 = np.array([0, 2 , 5, 12, 15])
limit2 = np.array([0, 2 , 5, 12])
limit3 = np.array([0, 2 , 5, 12, 15, 22])

result1= formating(Numbers, limit1)
result2= formating(Numbers, limit2)
result3= formating(Numbers, limit3)

Current output

result1:  [ 0.  0.  7. 30.  0. 20.] 
result2:  [ 0.  0.  7. 30. 20.] 
result3:  [ 0.  0.  7. 30.  0. 20.]

Wanted Output:

result1:  [ 0.  7. 30.  0.] 
result2:  [ 0.  7. 30. ] 
result3:  [ 0.  7. 30.  0. 20.]
2
  • Please explain the question, as it is unclear what you are trying to do Commented Mar 12, 2021 at 21:37
  • which bit is hard to recall, it goes through 2 elements at a time in Numbers like for example 0 and 2. The first and the second element in limit1 - 3. If there arent any values in between 0 and 2 which there arent since all the elements of Numbers is > 2. It gives out 0 as the result. If the Numbers values are in range then it will add up all the Numbers elements.Hopefully thats more clear. Commented Mar 12, 2021 at 21:45

1 Answer 1

1

So just throw out the bins for numbers off the end.

result1 = result1[1:len(limit1)]
result2 = result2[1:len(limit2)]
result3 = result3[1:len(limit3)]

Or, for smarter results, end the function with:

    result = np.bincount(1, weights=a)
    return result[1:len(b)]
Sign up to request clarification or add additional context in comments.

1 Comment

Hi I made another post that is kind of related to this question if you could take a look at it II would appreciate it: stackoverflow.com/questions/66609006/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.