Python Numpy - numpy 轴性能

신대규*_*신대규 5 python arrays numpy

默认情况下,numpy 是行主要的。\n因此,以下结果对我来说是自然接受的。

\n\n
a = np.random.rand(5000, 5000)\n\n%timeit a[0,:].sum()\n3.57 \xc2\xb5s \xc2\xb1 13.9 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 100000 loops each)\n\n%timeit a[:,0].sum()\n38.8 \xc2\xb5s \xc2\xb1 8.19 ns per loop (mean \xc2\xb1 std. dev. of 7 runs, 10000 loops each)\n
Run Code Online (Sandbox Code Playgroud)\n\n

因为是行主序,自然是通过[0,:]来计算更快。\n但是,如果使用numpy sum函数,结果就不一样了。

\n\n
%timeit a.sum(axis=0)\n16.9 ms \xc2\xb1 13.5 \xc2\xb5s per loop (mean \xc2\xb1 std. dev. of 7 runs, 100 loops each)\n\n%timeit a.sum(axis=1)\n29.5 ms \xc2\xb1 90.3 \xc2\xb5s per loop (mean \xc2\xb1 std. dev. of 7 runs, 10 loops each)\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果使用 numpy sum 函数,沿列计算它会更快。

\n\n

所以我的观点是为什么沿轴= 0(沿列计算)的速度比沿轴= 1(沿行)更快。

\n\n

例如

\n\n
a = np.array([[0, 1, 2],\n[3, 4, 5],\n[6, 7, 8]], order='C')\n
Run Code Online (Sandbox Code Playgroud)\n\n

按照行主顺序,[1,2,3]和[4,5,6],[7,8,9]分别分配到相邻的内存。

\n\n

因此,沿 axis = 1 计算的速度应该比 axis = 0 快。\n但是,当使用 numpy sum 函数时,沿列(axis = 0)计算速度更快。

\n\n

你如何解释这一点?

\n\n

谢谢

\n

cs9*_*s95 2

你不计算同样的事情。

前两个命令仅计算整个数组中的一行/列。

a[0, :].sum().shape   # sums just the first row only
()
Run Code Online (Sandbox Code Playgroud)

后两个命令对二维数组的全部内容求和,但沿着某个轴。这样,您不会得到单个结果(如前两个命令中所示),而是得到一个一维总和数组。

a.sum(axis=0).shape   # computes the row-wise sum for each column
(5000,)
Run Code Online (Sandbox Code Playgroud)

总之,两组命令执行不同的操作。


a
array([[1, 6, 9, 1, 6],
       [5, 6, 9, 1, 3],
       [5, 0, 3, 5, 7],
       [2, 8, 3, 8, 6],
       [3, 4, 8, 5, 0]])

a[0, :]
array([1, 6, 9, 1, 6])

a[0, :].sum()
23
Run Code Online (Sandbox Code Playgroud)

a.sum(axis=0)
array([16, 24, 32, 20, 22])
Run Code Online (Sandbox Code Playgroud)

  • 另外值得注意的是,如果您执行 `c = np.random.randn(5000, 5000)` 和 `f = np.asfortranarray(c)`,您确实会看到 0 和 0 上的和的不可忽略的反向时序差异1 轴。 (3认同)