4

我的目标是编写一个函数来执行两个数组的模 2 乘法(乘法 = 和,加法 = xor)。这是我现在拥有的代码。任何关于如何改进这一点的建议都非常感激 - 特别是,我应该/如何让列表理解直接进入适当形状的数组?

import numpy as np
import operator

def m2mult(a,b):

    c = np.ndarray([reduce(operator.xor, np.logical_and(a[x,:],b[:,y]))
         for x in range(0,a.shape[0]) for y in range (0, b.shape[1])])
    c.shape = (a.shape[0], b.shape[1])

    return c
4

2 回答 2

4

你根本不应该这样做:

a = np.random.randint(0,2,(4,4))
b = np.random.randint(0,2,(4,4))

# Now note that this is true:
# (I will let you figure that out, its a lot of neat broadcasting.
# b.T would be enough as well, because of it)
np.multiply(a[:,None,:], b.T[None,:,:]).sum(-1) == np.dot(a,b)
# note also that .sum(-1) is the same as np.add.reduce(array, axis=-1)

# now we can do the same thing for your logical operations:
a = np.random.randint(0,2,(4,4)).astype(bool)
b = np.random.randint(0,2,(4,4)).astype(bool)

def m2mult(a, b):
    mult = np.logical_and(a[:,None,:], b.T[None,:,:])
    return np.logical_xor.reduce(mult, axis=-1)

它完全矢量化,速度更快,而且只是 numpy!

于 2013-01-19T00:01:15.700 回答
2

您可以只对ints 进行公共矩阵乘法,然后进行模2归约,然后转换回bool

np.mod(np.dot(a.astype('u1'), b), 2).astype('bool')

它比 seberg 的解决方案和 Jaime 对它的修改要快。

+---------------+---------+-----------+----------+
|               |  10x10  | 1000x1000 | 750x1250 |
+---------------+---------+-----------+----------+
| m2mult_tz     | 33 us   | 7.27 s    | 4.68 s   |
| m2mult_jaime  | 56.7 us | 20.4 s    | 14.2 s   |
| m2mult_seberg | 62.9 us | 20.5 s    | 14.3 s   |
+---------------+---------+-----------+----------+

对于非常大的数组或者如果您的程序经常执行此操作,这可能是一个问题。
我对这种方法和 seberg 的解决方案以及 Jaime 建议的修改进行了计时。
以下是我实现不同功能的方式:

import numpy as np

def create_ab(n, m):
    a = np.random.randint(0, 2, (n, m)).astype(bool)
    b = np.random.randint(0, 2, (m, n)).astype(bool)
    return a, b


def m2mult_tz(a, b):
    return np.mod(np.dot(a.astype('u1'), b), 2).astype(bool)


def m2mult_seberg(a, b):
    return np.logical_xor.reduce(
                np.logical_and(a[:,None,:], b.T[None,:,:]),
                axis=-1)


def m2mult_jaime(a, b):
    return np.logical_xor.reduce(
                np.logical_and(a[:, :, None], b),
                axis=1)

这是 1000x1000 时间的记录(我还检查了所有情况下的结果是否相同):

In [19]: a, b = create_ab(1000, 1000)

In [20]: timeit m2mult_tz(a, b)
1 loops, best of 3: 7.27 s per loop

In [21]: timeit m2mult_jaime(a, b)
1 loops, best of 3: 20.4 s per loop

In [22]: timeit m2mult_seberg(a, b)
1 loops, best of 3: 20.5 s per loop

In [23]: r_tz = m2mult_tz(a, b)

In [24]: r_jaime = m2mult_jaime(a, b)

In [25]: r_seberg = m2mult_seberg(a, b)

In [26]: np.all(r_tz == r_jaime)
Out[26]: True

In [27]: np.all(r_tz == r_seberg)
Out[27]: True
于 2013-01-19T23:14:46.073 回答