简体   繁体   English

numpy:如何从矩阵向量构造向量矩阵

[英]numpy: how to construct a matrix of vectors from vector of matrix

I'm new to numpy, so, with numpy, is it possible to use a vector of matrix to get a matrix of vectors" for example:我是 numpy 的新手,因此,对于 numpy,是否可以使用矩阵向量来获取向量矩阵”,例如:

matrix1(  
[  
 [1, 2, 3],  
 [1, 2, 3],  
 [1, 2, 3]  
])

matrix2(  
[  
 [2, 4, 6],  
 [2, 4, 6],  
 [2, 4, 6]  
])

-->

matrix(  
[  
 [array('1 2'), array('2 4'), array('3 6')],  
 [array('1 2'), array('2 4'), array('3 6')],  
 [array('1 2'), array('2 4'), array('3 6')]  
])

I'm new to numpy, so I'm not sure if it is allowed to put any thing in numpy's matrix or just numbers.我是 numpy 的新手,所以我不确定是否允许在 numpy 的矩阵中放入任何东西或只是数字。 And it's not easy to get answer from google with descriptions like "matrix of vectors and vectors of matrix"而且从谷歌那里得到答案并不容易,比如“矩阵的向量和矩阵的向量”这样的描述

numpy doesn't have a concept of "vector" separate from "matrix." numpy没有与“矩阵”分开的“向量”概念。 It does have distinct concepts of "matrix" and "array," but most people avoid the matrix representation entirely.它确实有不同的“矩阵”和“数组”概念,但大多数人完全避免使用矩阵表示。 If you use arrays, the concepts of "vector," "matrix," and "tensor" are all subsumed under the general concept of an array's "shape" attribute.如果使用数组,则“向量”、“矩阵”和“张量”的概念都包含在数组“形状”属性的一般概念下。

In this worldview, vectors and matrices are both 2-dimensional arrays, distinguished only by their shape.在这种世界观中,向量和矩阵都是二维数组,仅通过它们的形状来区分。 Row vectors are arrays with the shape (1, n) , while column vectors are arrays with the shape (n, 1) .行向量是形状为(1, n)的数组,而列向量是形状为(n, 1)数组。 Matrices are arrays with the shape (n, m) .矩阵是形状为(n, m)数组。 1-dimensional arrays can behave like vectors sometimes, depending on context, but often you'll find that you won't get what you want unless you "upgrade" them.一维数组有时会表现得像向量,这取决于上下文,但通常你会发现除非你“升级”它们,否则你不会得到你想要的。

With all that in mind, here's one possible answer to your question.考虑到所有这些,这里有一个可能的答案来回答您的问题。 First, we create a 1-d array:首先,我们创建一个一维数组:

>>> a1d = numpy.array([1, 2, 3])
>>> a1d
array([1, 2, 3])

Now we reshape it to create a column vector.现在我们重塑它以创建一个列向量。 The -1 here tells numpy to figure out the right size given the input.这里的-1告诉numpy找出给定输入的正确大小。

>>> vcol = a1d.reshape((-1, 1))
>>> vcol
array([[1],
       [2],
       [3]])

Observe the doubled brackets at the beginning and ending of this.请注意开头和结尾处的双括号。 That's a subtle cue that this is a 2-d array, even though one dimension has a size of just 1.这是一个微妙的提示,表明这是一个二维数组,即使一维的大小仅为 1。

We can do the same thing, swapping the dimensions, to get a row.我们可以做同样的事情,交换维度,得到一行。 Note again the doubled brackets.再次注意双括号。

>>> vrow = a1d.reshape((1, -1))
>>> vrow
array([[1, 2, 3]])

You can tell that these are 2-d arrays, because a 1-d array would have only one value in its shape tuple:您可以看出这些是二维数组,因为一维数组的shape元组中只有一个值:

>>> a1d.shape
(3,)
>>> vcol.shape
(3, 1)
>>> vrow.shape
(1, 3)

To build a matrix from column vectors we can use hstack .要从列向量构建矩阵,我们可以使用hstack There are lots of other methods that may be faster, but this is a good starting point.还有很多其他方法可能更快,但这是一个很好的起点。 Here, note that [vcol] is not a numpy object, but an ordinary python list, so [vcol] * 3 means the same thing as [vcol, vcol, vcol] .这里,请注意[vcol]不是一个numpy对象,而是一个普通的 python 列表,所以[vcol] * 3[vcol, vcol, vcol]意思相同。

>>> mat = numpy.hstack([vcol] * 3)
>>> mat
array([[1, 1, 1],
       [2, 2, 2],
       [3, 3, 3]])

And vstack gives us the same thing from row vectors. vstack从行向量中给了我们同样的东西。

>>> mat2 = numpy.vstack([vrow] * 3)
>>> mat2
array([[1, 2, 3],
       [1, 2, 3],
       [1, 2, 3]])

It's unlikely that any other interpretation of "construct a matrix of vectors from vector of matrix" will generate something you actually want in numpy ! “从矩阵向量构造向量矩阵”的任何其他解释都不太可能在numpy生成您真正想要的东西!

Since you mention wanting to do linear algebra, here are a couple of operations that are possible.既然你提到想要做线性代数,这里有几个可能的操作。 This assumes you're using a recent-enough version of python to use the new @ operator, which provides an unambiguous inline notation for matrix multiplication of arrays.这假设您使用的是最新版本的 python 来使用新的@运算符,它为数组的矩阵乘法提供了明确的内联符号。 1 1

For arrays, multiplication is always element-wise.对于数组,乘法始终是按元素进行的。 But sometimes there is broadcasting.但有时有广播。 For values with the same shape, it's plain element-wise multiplication:对于具有相同形状的值,它是纯元素乘法:

>>> vrow * vrow
array([[1, 4, 9]])
>>> vcol * vcol
array([[1],
       [4],
       [9]])

When values have different shapes, they are broadcast together if possible to produce a sensible result:当值具有不同的形状时,如果可能,它们会一起广播以产生合理的结果:

>>> vrow * vcol
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])
>>> vcol * vrow
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

Broadcasting works in the way you'd expect for other shapes as well:广播也以您期望的其他形状的方式工作:

>>> vrow * mat
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])
>>> vcol * mat
array([[1, 1, 1],
       [4, 4, 4],
       [9, 9, 9]])

If you want a dot product, you have to use the @ operator:如果你想要一个点积,你必须使用@操作符:

>>> vrow @ vcol
array([[14]])

Note that unlike the * operator, this is not symmetric:请注意,与*运算符不同,这不是对称的:

>>> vcol @ vrow
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

This can be a bit confusing at first, because this looks the same as vrow * vcol , but don't be fooled.起初这可能有点令人困惑,因为这看起来与vrow * vcol相同,但不要被愚弄。 * will produce the same result regardless of argument order. *无论参数顺序如何,都会产生相同的结果。 Finally, for a matrix-vector product:最后,对于矩阵向量乘积:

>>> mat @ vcol
array([[ 6],
       [12],
       [18]])

Observe again the difference between @ and * :再次观察@*之间的区别:

>>> mat * vcol
array([[1, 1, 1],
       [4, 4, 4],
       [9, 9, 9]])

1. Sadly, this only exists as of Python 3.5. 1. 遗憾的是,这只存在于 Python 3.5。 If you need to work with an earlier version, all the same advice applies, except that instead of using inline notation for a @ b , you have to use np.dot(a, b) .如果您需要使用较早的版本,则所有相同的建议都适用,不同之处在于,您必须使用np.dot(a, b)而不是对a @ b使用内联符号。 numpy 's matrix type overrides * to behave like @ ... but then you can't do element-wise multiplication or broadcasting the same way! numpymatrix类型覆盖*以表现得像@ ... 但是你不能以相同的方式进行元素乘法或广播! So even if you have an earlier version, I don't recommend using the matrix type.所以即使你有更早的版本,我也不建议使用matrix类型。

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

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