0

The function down below is meant to sum out all second row values of Numbers[:,0] between 2 consecutive elements of limits limit1-3. For the first calculation if none of the values are between 0 and 2 (the first two elements of limit1) within Numbers so the resultant is 0. For the second calculation 3,4 within Numbers[:,0] is between the values 2-5 in limit1 so the second column of Numbers is summed up 1+3 =4 resulting in 4. How could I implement this to the function below?

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

Numbers = np.array([[3,1], [4,3], [5,3], [7,11], [8,9], [10,20] , [20, 45]])
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[:,0], Numbers[:,1], limit1)
result2= formating(Numbers[:,0], Numbers[:,1], limit2)
result3= formating(Numbers[:,0], Numbers[:,1], limit3)

Expected Output

result1:  [ 0.  4. 43.  0. ] 
result2:  [ 0.  4. 43. ] 
result3:  [ 0.  4. 43.  0. 45.]

Current Output

result1:  [ 0.  4. 43.  0. 45.] 
result2:  [ 0.  4. 43. 45.] 
result3:  [ 0.  4. 43.  0. 45.]

1 Answer 1

1

This:

    return result[1:len(b)]

should be

    return result[1:len(c)]

Your return vector is dependent on the length of your bins, not your input data.

Sign up to request clarification or add additional context in comments.

Comments

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.