I have two equally sized numpy arrays (they happen to be 48x365) where every element is either -1, 0, or 1. I want to compare the two and see how many times they are both the same and how many times they are different while discounting all the times where at least one of the arrays has a zero as no data. For instance:
我有两个同样大小的numpy数组(它们恰好是48x365),其中每个元素都是-1,0或1.我想比较两者,看看它们是多少次相同,多少次它们是不同的同时折扣所有时间,其中至少有一个数组为0,没有数据。例如:
for x in range(48):
for y in range(365):
if array1[x][y] != 0:
if array2[x][y] != 0:
if array1[x][y] == array2[x][y]:
score = score + 1
else:
score = score - 1
return score
This takes a very long time. I was thinking to take advantage of the fact that multiplying the elements together and summing all the answers may give the same outcome, and I'm looking for a special numpy function to help with that. I'm not really sure what unusual numpy function are out there.
这需要很长时间。我正在考虑利用将元素相乘并总结所有答案的事实可能会给出相同的结果,而我正在寻找一个特殊的numpy函数来帮助解决这个问题。我不确定那里有什么不寻常的numpy功能。
4 个解决方案
#1
12
Simpy do not iterate. Iterating over a numpy array defeats the purpose of using the tool.
Simpy不迭代。迭代numpy数组会破坏使用该工具的目的。
ans = np.logical_and(
np.logical_and(array1 != 0, array2 != 0),
array1 == array2 )
should give the correct solution.
应该给出正确的解决方案。
#2
6
For me the easiest way is to do this :
对我来说,最简单的方法是这样做:
A = numpy.array()
B = numpy.array()
T = A - B
max = numpy.max(numpy.abs(T))
epsilon = 1e-6
if max > epsilon:
raise Exception("Not matching arrays")
It allow to know quickly if arrays are the same and allow to compare float values !!
它允许快速了解数组是否相同并允许比较浮点值!!
#3
0
Simple calculations along the following lines, will help you to select the most suitable way to handle your case:
以下几行的简单计算将帮助您选择最合适的方式来处理您的案例:
In []: A, B= randint(-1, 2, size= (48, 365)), randint(-1, 2, size= (48, 365))
In []: ignore= (0== A)| (0== B)
In []: valid= ~ignore
In []: (A[valid]== B[valid]).sum()
Out[]: 3841
In []: (A[valid]!= B[valid]).sum()
Out[]: 3849
In []: ignore.sum()
Out[]: 9830
Ensuring that the calculations are valid:
确保计算有效:
In []: 3841+ 3849+ 9830== 48* 365
Out[]: True
Therefore your score
(with these random values) would be:
因此,您的得分(使用这些随机值)将是:
In []: a, b= A[valid], B[valid]
In []: score= (a== b).sum()- (a!= b).sum()
In []: score
Out[]: -8
#4
0
import numpy as np
A = np.array()
B = np.array()
...
Z = np.array()
to_test = np.array([A, B, .., Z])
# compare linewise if all lines are equal
np.all(map(lambda x: np.all(x==to_test[0,:]), to_test[1:,:]))
#1
12
Simpy do not iterate. Iterating over a numpy array defeats the purpose of using the tool.
Simpy不迭代。迭代numpy数组会破坏使用该工具的目的。
ans = np.logical_and(
np.logical_and(array1 != 0, array2 != 0),
array1 == array2 )
should give the correct solution.
应该给出正确的解决方案。
#2
6
For me the easiest way is to do this :
对我来说,最简单的方法是这样做:
A = numpy.array()
B = numpy.array()
T = A - B
max = numpy.max(numpy.abs(T))
epsilon = 1e-6
if max > epsilon:
raise Exception("Not matching arrays")
It allow to know quickly if arrays are the same and allow to compare float values !!
它允许快速了解数组是否相同并允许比较浮点值!!
#3
0
Simple calculations along the following lines, will help you to select the most suitable way to handle your case:
以下几行的简单计算将帮助您选择最合适的方式来处理您的案例:
In []: A, B= randint(-1, 2, size= (48, 365)), randint(-1, 2, size= (48, 365))
In []: ignore= (0== A)| (0== B)
In []: valid= ~ignore
In []: (A[valid]== B[valid]).sum()
Out[]: 3841
In []: (A[valid]!= B[valid]).sum()
Out[]: 3849
In []: ignore.sum()
Out[]: 9830
Ensuring that the calculations are valid:
确保计算有效:
In []: 3841+ 3849+ 9830== 48* 365
Out[]: True
Therefore your score
(with these random values) would be:
因此,您的得分(使用这些随机值)将是:
In []: a, b= A[valid], B[valid]
In []: score= (a== b).sum()- (a!= b).sum()
In []: score
Out[]: -8
#4
0
import numpy as np
A = np.array()
B = np.array()
...
Z = np.array()
to_test = np.array([A, B, .., Z])
# compare linewise if all lines are equal
np.all(map(lambda x: np.all(x==to_test[0,:]), to_test[1:,:]))