Numpy:具有各种形状的一维数组 [英] Numpy: 1D array with various shape

查看:44
本文介绍了Numpy:具有各种形状的一维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了解如何使用 NumPy 处理 1D 数组(线性代数中的向量).

在下面的例子中,我生成了两个 numpy.array ab :

<预><代码>>>>将 numpy 导入为 np>>>a = np.array([1,2,3])>>>b = np.array([[1],[2],[3]]).reshape(1,3)>>>一个形状(3,)>>>b.形状(1, 3)

对我来说,ab 根据线性代数定义具有相同的形状:1 行 3 列,但对于 NumPy 则不是.

现在,NumPy dot 产品:

<预><代码>>>>np.dot(a,a)14>>>np.dot(b,a)数组([14])>>>np.dot(b,b)回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.ValueError:对象未对齐

我有三种不同的输出.

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

我也与那些点积有一些不同:

<预><代码>>>>c = np.ones(9).reshape(3,3)>>>np.dot(a,c)数组([ 6., 6., 6.])>>>np.dot(b,c)数组([[ 6., 6., 6.]])

解决方案

请注意,您不仅要使用一维数组:

在[6]中:a.ndim出[6]:1在 [7]: b.ndim出[7]:2

所以,b 是一个二维数组.您还可以在 b.shape 的输出中看到这一点:(1,3) 表示二维,因为 (3,) 是一维.

np.dot 的行为对于一维和二维数组是不同的(来自 文档):

<块引用>

对于二维数组相当于矩阵乘法,对于一维数组数组到向量的内积

这就是您得到不同结果的原因,因为您正在混合一维和二维数组.由于 b 是一个二维数组,np.dot(b, b) 尝试对两个 1x3 矩阵进行矩阵乘法,但失败了.

<小时>

对于一维数组,np.dot 做向量的内积:

在[44]中:a = np.array([1,2,3])在 [45] 中:b = np.array([1,2,3])在 [46] 中: np.dot(a, b)出[46]:14在 [47] 中:np.inner(a, b)出[47]:14

对于二维数组,它是一个矩阵乘法(所以 1x3 x 3x1 = 1x1,或 3x1 x 1x3 = 3x3):

在[49]中:a = a.reshape(1,3)在 [50] 中:b = b.reshape(3,1)在 [51] 中:一个输出[51]:数组([[1, 2, 3]])在 [52] 中:b出[52]:数组([[1],[2],[3]])在 [53] 中:np.dot(a,b)出[53]:数组([[14]])在 [54] 中:np.dot(b,a)出[54]:数组([[1, 2, 3],[2, 4, 6],[3, 6, 9]])在 [55] 中:np.dot(a,a)---------------------------------------------------------------------------ValueError 回溯(最近一次调用)<ipython-input-55-32e36f9db916>在 <module>()---->1 np.dot(a,a)ValueError:对象未对齐

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

In the following example, I generate two numpy.array a and 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.

Now, the NumPy dot product:

>>> 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?

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.]])

解决方案

Notice you are not only working with 1D arrays:

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.

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

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.


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

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):

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

这篇关于Numpy:具有各种形状的一维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆