简体   繁体   English

numpy数组减法,怎么来?

[英]numpy array subtraction, how come?

Cannot quite understand why xy is a matrix? 无法完全理解为什么xy是矩阵?

and what is the difference between the shape of x (4,) and y (4,1), aren't they all treated as vectors? x(4,)和y(4,1)的形状有什么区别,难道它们都不都是矢量吗?

>>> x = np.array([1,2,3,4])
>>> y = x[:,None]
>>> x.shape
(4,)
>>> y.shape
(4, 1)
>>> x
array([1, 2, 3, 4])
>>> y
array([[1],
       [2],
       [3],
       [4]])
>>> x-y
array([[ 0,  1,  2,  3],
       [-1,  0,  1,  2],
       [-2, -1,  0,  1],
       [-3, -2, -1,  0]])

This is explained well in the documentation for numpy broadcasting - numpy广播的文档对此进行了很好解释-

General Broadcasting Rules 一般广播规则

When operating on two arrays, NumPy compares their shapes element-wise. 在两个数组上进行操作时,NumPy逐元素比较其形状。 It starts with the trailing dimensions, and works its way forward . 它从尾随尺寸开始,一直向前发展 Two dimensions are compatible when 两种尺寸兼容

  1. they are equal, or 它们相等,或者
  2. one of them is 1 其中之一是1

If these conditions are not met, a ValueError: frames are not aligned exception is thrown, indicating that the arrays have incompatible shapes. 如果不满足这些条件,则将引发ValueError: frames are not aligned异常,表明数组的形状不兼容。 The size of the resulting array is the maximum size along each dimension of the input arrays. 所得数组的大小是沿输入数组每个维度的最大大小。

Arrays do not need to have the same number of dimensions. 数组不必具有相同数量的维。 For example, if you have a 256x256x3 array of RGB values, and you want to scale each color in the image by a different value, you can multiply the image by a one-dimensional array with 3 values. 例如,如果您具有256x256x3的RGB值数组,并且希望将图像中的每种颜色缩放一个不同的值,则可以将图像乘以一维数组,并带有3个值。 Lining up the sizes of the trailing axes of these arrays according to the broadcast rules, 根据广播规则来排列这些数组的尾轴的大小,

(Emphasis mine) (强调我的)

When operating array of shape (4,) with array of shape (4,1) , the first one is broadcasted to (1,4) (As it starts at the trailing dimension and works its way forward, replacing them by 1 if no value at that dimension, hence (4,) becomes (1,4) .) and then operating both of them together create an array of shape (4,4) . 当操作形状为(4,)的数组和形状为(4,1)数组时,第一个广播到(1,4) (因为它从尾随尺寸开始并向前移动,如果没有,则将其替换为1该值在该维度上的值,因此(4,)变为(1,4) 。)然后将它们一起操作会创建形状为(4,4)的数组。

And the shape (4,1) is not treated as vector. 并且形状(4,1)不被视为向量。 It is a 2-D array. 它是一个二维数组。

Same would have occured if x was array([[1, 2, 3, 4]]) , an array of shape (1,4) - 如果xarray([[1, 2, 3, 4]]) (形状为(1,4)的数组-

Example - 范例-

In [70]: y
Out[70]:
array([[1],
       [2],
       [3],
       [4]])

In [71]: x = np.array([x])

In [72]: x
Out[72]: array([[1, 2, 3, 4]])

In [73]: x - y
Out[73]:
array([[ 0,  1,  2,  3],
       [-1,  0,  1,  2],
       [-2, -1,  0,  1],
       [-3, -2, -1,  0]])

Broadcasting always matches starting at the last dimensions, hence trying something like below throws an error - 广播总是从最后一个维度开始匹配,因此尝试以下操作会引发错误-

In [79]: y = np.array([[1,2,3,4],[5,6,7,8]])

In [80]: y.shape
Out[80]: (2, 4)

In [81]: x = np.array([1,2])

In [82]: x.shape
Out[82]: (2,)

In [83]: y - x
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-83-4abb3bd0a148> in <module>()
----> 1 y - x

ValueError: operands could not be broadcast together with shapes (2,4) (2,)

This is because first it checks the last dimensions of both arrays, one is 4 and the other is 2 , they don't match according to the rules, hence it throws the error. 这是因为首先检查两个数组的最后一个维度,一个是4 ,另一个是2 ,它们不符合规则,因此会引发错误。 If the shape of x was (2,1) , it would have worked. 如果x的形状为(2,1) ,则它会起作用。 Example - 范例-

In [84]: y - x.reshape((2,1))
Out[84]:
array([[0, 1, 2, 3],
       [3, 4, 5, 6]])

what is the difference between the shape of x (4,) and y (4,1) x(4,)和y(4,1)的形状有什么区别

x is a vector ; x是一个向量; you would need 1 index to access it's elements. 您将需要1个索引来访问其元素。

y is a 2-D array ; y是一个二维数组; you would need 2 indexes to access it's elements. 您将需要2个索引来访问其元素。

Cannot quite understand why xy is a matrix? 无法完全理解为什么xy是矩阵?

This is because x and y get broadcasted when you subtract them. 这是因为xy减去后会广播。

x - y becomes : x - y变为:

[1 2 3 4          [1 1 1 1

 1 2 3 4           2 2 2 2
            -     
 1 2 3 4           3 3 3 3

 1 2 3 4]          4 4 4 4]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM