Python 格式化多维numpy数组以查找2个值之间的和
下面的函数用于求出两个连续的限制元素Python 格式化多维numpy数组以查找2个值之间的和,python,arrays,function,numpy,multidimensional-array,Python,Arrays,Function,Numpy,Multidimensional Array,下面的函数用于求出两个连续的限制元素limit1-3之间的number[:,0]的所有第二行值的总和。对于第一次计算,如果数值中没有一个在0和2之间(限制1的前两个元素),则结果为0。对于第二次计算,3,Numbers[:,0]中的4在limit1中的值2-5之间,因此将Numbers的第二列相加1+3=4,得到4。我如何实现下面的功能 def formating(a, b, c): # Formating goes here x = np.sort(c); #
limit1-3
之间的number[:,0]
的所有第二行值的总和。对于第一次计算,如果数值中没有一个在0和2之间(限制1的前两个元素),则结果为0。对于第二次计算,3,Numbers[:,0]
中的4
在limit1
中的值2-5
之间,因此将Numbers
的第二列相加1+3=4
,得到4。我如何实现下面的功能
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(c)]
返回向量取决于箱子的长度,而不是输入数据
return result[1:len(c)]