比较两个 numpy 数组
我有两个大小相同的 numpy 数组(它们恰好是 48x365),其中每个元素都是 -1、0 或 1。我想比较这两个数组,看看它们有多少次相同以及有多少次不同同时将至少一个数组为零的情况视为没有数据。例如:
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
这需要很长时间。我正在考虑利用这样一个事实:将元素相乘并对所有答案求和可能会得到相同的结果,并且我正在寻找一个特殊的 numpy 函数来帮助实现这一点。我不太确定那里有什么不寻常的 numpy 函数。
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:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
简单地说,不要迭代。迭代 numpy 数组违背了使用该工具的目的。
应该给出正确的解决方案。
Simpy do not iterate. Iterating over a numpy array defeats the purpose of using the tool.
should give the correct solution.
对我来说,最简单的方法是这样做:
它允许快速知道数组是否相同并允许比较浮点值!
For me the easiest way is to do this :
It allow to know quickly if arrays are the same and allow to compare float values !!
按照以下几行进行简单计算,将帮助您选择最合适的方法来处理您的情况:
确保计算有效:
因此您的
分数
(使用这些随机值)将是:Simple calculations along the following lines, will help you to select the most suitable way to handle your case:
Ensuring that the calculations are valid:
Therefore your
score
(with these random values) would be: