比较两个 numpy 数组

发布于 2024-11-23 19:43:09 字数 514 浏览 5 评论 0原文

我有两个大小相同的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天涯沦落人 2024-11-30 19:43:09

简单地说,不要迭代。迭代 numpy 数组违背了使用该工具的目的。

ans = np.logical_and(
    np.logical_and(array1 != 0, array2 != 0),
    array1 == array2 )

应该给出正确的解决方案。

Simpy do not iterate. Iterating over a numpy array defeats the purpose of using the tool.

ans = np.logical_and(
    np.logical_and(array1 != 0, array2 != 0),
    array1 == array2 )

should give the correct solution.

眸中客 2024-11-30 19:43:09

对我来说,最简单的方法是这样做:

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")

它允许快速知道数组是否相同并允许比较浮点值!

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 !!

念﹏祤嫣 2024-11-30 19:43:09

按照以下几行进行简单计算,将帮助您选择最合适的方法来处理您的情况:

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

确保计算有效:

In []: 3841+ 3849+ 9830== 48* 365
Out[]: True

因此您的分数(使用这些随机值)将是:

In []: a, b= A[valid], B[valid]
In []: score= (a== b).sum()- (a!= b).sum()
In []: score
Out[]: -8

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
倾听心声的旋律 2024-11-30 19:43:09
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:,:]))
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:,:]))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文