格式化多维numpy数组以找到2个值之间的和 [英] Formating multidimensional numpy arrays to find the sum betyween 2 values Python

查看:48
本文介绍了格式化多维numpy数组以找到2个值之间的和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的函数旨在求和2个连续的限制 limit1-3 之间的 Numbers [:,0] 的所有第二行值.对于第一个计算,如果数值都不在Numbers的0和2( limit1 的前两个元素)之间,则结果为0.对于第二个计算, 3,4 Numbers [:,0] 中的>在 limit1 中的 2-5 值之间,因此 Numbers 总结为 1 + 3 = 4 ,结果为4.如何在下面的函数中实现呢?

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)

预期产量

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

电流输出

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

推荐答案

此:

    return result[1:len(b)]

应该是

    return result[1:len(c)]

您的返回向量取决于垃圾箱的长度,而不是输入数据.

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

这篇关于格式化多维numpy数组以找到2个值之间的和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆