Numpy:各种形状的一维阵列

时间:2023-02-07 21:24:27

I try to understand how to handle a 1D array (vector in linear algebra) with NumPy.

我尝试了解如何使用NumPy处理一维数组(线性代数中的向量)。

In the following example, I generate two numpy.array a and b:

在下面的示例中,我生成了两个numpy.array a和b:

>>> import numpy as np
>>> a = np.array([1,2,3])
>>> b = np.array([[1],[2],[3]]).reshape(1,3)
>>> a.shape
(3,)
>>> b.shape
(1, 3)

For me, a and b have the same shape according linear algebra definition: 1 row, 3 columns, but not for NumPy.

对我来说,a和b根据线性代数定义具有相同的形状:1行3列,但不适用于NumPy。

Now, the NumPy dot product:

现在,NumPy点产品:

>>> np.dot(a,a)
14
>>> np.dot(b,a)
array([14])
>>> np.dot(b,b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: objects are not aligned

I have three different outputs.

我有三种不同的输出。

What's the difference between dot(a,a) and dot(b,a)? Why dot(b,b) doesn't work?

点(a,a)和点(b,a)之间有什么区别?为什么dot(b,b)不起作用?

I also have some differencies with those dot products:

我对这些点产品也有一些不同之处:

>>> c = np.ones(9).reshape(3,3)
>>> np.dot(a,c)
array([ 6.,  6.,  6.])
>>> np.dot(b,c)
array([[ 6.,  6.,  6.]])

1 个解决方案

#1


18  

Notice you are not only working with 1D arrays:

请注意,您不仅使用1D数组:

In [6]: a.ndim
Out[6]: 1

In [7]: b.ndim
Out[7]: 2

So, b is a 2D array. You also see this in the output of b.shape: (1,3) indicates two dimensions as (3,) is one dimension.

所以,b是一个2D数组。您还可以在b.shape的输出中看到这一点:(1,3)表示两个维度(3,)是一个维度。

The behaviour of np.dot is different for 1D and 2D arrays (from the docs):

对于1D和2D数组(来自文档),np.dot的行为是不同的:

For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors

对于二维阵列,它相当于矩阵乘法,而对于一维阵列则相当于向量的内积

That is the reason you get different results, because you are mixing 1D and 2D arrays. Since b is a 2D array, np.dot(b, b) tries a matrix multiplication on two 1x3 matrices, which fails.

这就是你得到不同结果的原因,因为你正在混合1D和2D数组。由于b是2D数组,np.dot(b,b)在两个1x3矩阵上尝试矩阵乘法,这会失败。


With 1D arrays, np.dot does a inner product of the vectors:

使用1D数组,np.dot执行向量的内积:

In [44]: a = np.array([1,2,3])

In [45]: b = np.array([1,2,3])

In [46]: np.dot(a, b)
Out[46]: 14

In [47]: np.inner(a, b)
Out[47]: 14

With 2D arrays, it is a matrix multiplication (so 1x3 x 3x1 = 1x1, or 3x1 x 1x3 = 3x3):

对于2D阵列,它是矩阵乘法(因此1x3 x 3x1 = 1x1,或3x1 x 1x3 = 3x3):

In [49]: a = a.reshape(1,3)

In [50]: b = b.reshape(3,1)

In [51]: a
Out[51]: array([[1, 2, 3]])

In [52]: b
Out[52]:
array([[1],
       [2],
       [3]])

In [53]: np.dot(a,b)
Out[53]: array([[14]])

In [54]: np.dot(b,a)
Out[54]:
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

In [55]: np.dot(a,a)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-32e36f9db916> in <module>()
----> 1 np.dot(a,a)

ValueError: objects are not aligned

#1


18  

Notice you are not only working with 1D arrays:

请注意,您不仅使用1D数组:

In [6]: a.ndim
Out[6]: 1

In [7]: b.ndim
Out[7]: 2

So, b is a 2D array. You also see this in the output of b.shape: (1,3) indicates two dimensions as (3,) is one dimension.

所以,b是一个2D数组。您还可以在b.shape的输出中看到这一点:(1,3)表示两个维度(3,)是一个维度。

The behaviour of np.dot is different for 1D and 2D arrays (from the docs):

对于1D和2D数组(来自文档),np.dot的行为是不同的:

For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors

对于二维阵列,它相当于矩阵乘法,而对于一维阵列则相当于向量的内积

That is the reason you get different results, because you are mixing 1D and 2D arrays. Since b is a 2D array, np.dot(b, b) tries a matrix multiplication on two 1x3 matrices, which fails.

这就是你得到不同结果的原因,因为你正在混合1D和2D数组。由于b是2D数组,np.dot(b,b)在两个1x3矩阵上尝试矩阵乘法,这会失败。


With 1D arrays, np.dot does a inner product of the vectors:

使用1D数组,np.dot执行向量的内积:

In [44]: a = np.array([1,2,3])

In [45]: b = np.array([1,2,3])

In [46]: np.dot(a, b)
Out[46]: 14

In [47]: np.inner(a, b)
Out[47]: 14

With 2D arrays, it is a matrix multiplication (so 1x3 x 3x1 = 1x1, or 3x1 x 1x3 = 3x3):

对于2D阵列,它是矩阵乘法(因此1x3 x 3x1 = 1x1,或3x1 x 1x3 = 3x3):

In [49]: a = a.reshape(1,3)

In [50]: b = b.reshape(3,1)

In [51]: a
Out[51]: array([[1, 2, 3]])

In [52]: b
Out[52]:
array([[1],
       [2],
       [3]])

In [53]: np.dot(a,b)
Out[53]: array([[14]])

In [54]: np.dot(b,a)
Out[54]:
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

In [55]: np.dot(a,a)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-32e36f9db916> in <module>()
----> 1 np.dot(a,a)

ValueError: objects are not aligned