在SetThreadAffinityMask()
中有一个名为numpy.sum()
的参数。它做了什么?
正如您在文档中看到的那样: http://docs.scipy.org/doc/numpy/reference/generated/numpy.sum.html
keepdims
答案 0 :(得分:48)
@Ney @hpaulj是正确的,你需要进行实验,但我怀疑你没有意识到某些数组的求和可以沿轴发生。请阅读以下文档阅读文档
>>> a
array([[0, 0, 0],
[0, 1, 0],
[0, 2, 0],
[1, 0, 0],
[1, 1, 0]])
>>> np.sum(a, keepdims=True)
array([[6]])
>>> np.sum(a, keepdims=False)
6
>>> np.sum(a, axis=1, keepdims=True)
array([[0],
[1],
[2],
[1],
[2]])
>>> np.sum(a, axis=1, keepdims=False)
array([0, 1, 2, 1, 2])
>>> np.sum(a, axis=0, keepdims=True)
array([[2, 4, 0]])
>>> np.sum(a, axis=0, keepdims=False)
array([2, 4, 0])
你会注意到,如果你没有指定一个轴(前两个例子),数值结果是相同的,但keepdims = True
返回一个数字为6的2D
数组,而,第二个化身返回了一个标量。
同样,在axis 1
(跨行)求和时,2D
时会再次返回keepdims = True
数组。
最后一个示例沿着axis 0
(向下列)显示了类似的特征...在keepdims = True
时保留了尺寸。
研究轴及其属性对于在处理多维数据时充分理解NumPy的强大功能至关重要。
答案 1 :(得分:0)
显示keepdims
在使用高维数组时的实际操作示例。让我们看看数组的形状如何随着我们进行不同的缩减而改变:
import numpy as np
a = np.random.rand(2,3,4)
a.shape
# => (2, 3, 4)
# Note: axis=0 refers to the first dimension of size 2
# axis=1 refers to the second dimension of size 3
# axis=2 refers to the third dimension of size 4
a.sum(axis=0).shape
# => (3, 4)
# Simple sum over the first dimension, we "lose" that dimension
# because we did an aggregation (sum) over it
a.sum(axis=0, keepdims=True).shape
# => (1, 3, 4)
# Same sum over the first dimension, but instead of "loosing" that
# dimension, it becomes 1.
a.sum(axis=(0,2)).shape
# => (3,)
# Here we "lose" two dimensions
a.sum(axis=(0,2), keepdims=True).shape
# => (1, 3, 1)
# Here the two dimensions become 1 respectively