Python 在 Numpy 中将行向量转换为列向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36384760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:48:28  来源:igfitidea点击:

Transforming a row vector into a column vector in Numpy

pythonnumpymultidimensional-arraylinear-algebranumpy-ndarray

提问by MY_G

Let's say I have a row vector of the shape (1, 256). I want to transform it into a column vector of the shape (256, 1) instead. How would you do it in Numpy?

假设我有一个形状为 (1, 256) 的行向量。我想将其转换为形状 (256, 1) 的列向量。你会如何在 Numpy 中做到这一点?

采纳答案by kmario23

you can use the transposeoperation to do this:

您可以使用转置操作来执行此操作:

Example:

例子:

In [2]: a = np.array([[1,2], [3,4], [5,6]])
In [5]: np.shape(a)
Out[5]: (3, 2)

In [6]: a_trans = a.transpose()
In [8]: np.shape(a_trans)
Out[8]: (2, 3)
In [7]: a_trans
Out[7]: 
array([[1, 3, 5],
       [2, 4, 6]])

Note that the original array awill still remain unmodified. The transpose operation will just make a copy and transpose it.

请注意,原始数组a仍将保持不变。转置操作只会复制并转置它。

回答by Mahdi Ghelichi

We can simply use the reshape functionality of numpy:

我们可以简单地使用 numpy 的重塑功能:

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

a.shape
(1,4)
b=a.reshape(-1,1)
b:
array([[1],
       [2],
       [3],
       [4]])

b.shape
(4,1)

回答by DuttaA

This one is a really good question.

这是一个非常好的问题。

Some of the ways I have compiled to do this are:

我编译的一些方法是:

>> import numpy as np
>> a = np.array([1, 2, 3], [2, 4, 5])
>> a
>> array([[1, 2],
       [2, 4],
       [3, 5]])

Another way to do it:

另一种方法:

>> a.T
>> array([[1, 2],
       [2, 4],
       [3, 5]])
       

Another way to do this will be:

另一种方法是:

>> a.reshape(a.shape[1], a.shape[0])
>> array([[1, 2],
       [3, 2],
       [4, 5]])
       

I have used a 2-Dimensional array in all of these problems, the real problem arises when there is a 1-Dimensional row vector which you want to columnize elegantly.

我在所有这些问题中都使用了 2 维数组,真正的问题出现在有一个 1 维行向量要优雅地列化时。

Numpy's reshape has a functionality where you pass the one of the dimension (number of rows or number of columns) you want, numpy can figure out the other dimension by itself if you pass the other dimension as -1

Numpy 的 reshape 有一个功能,你可以传递你想要的维度之一(行数或列数),如果你传递另一个维度,numpy 可以自己找出另一个维度 -1

>> a.reshape(-1, 1)
>> array([[1],
       [2],
       [3],
       [2],
       [4],
       [5]])
       
>> a = np.array([1, 2, 3])
>> a.reshape(-1, 1)
>> array([[1],
       [2],
       [3]])
       
>> a.reshape(2, -1)

>> ValueError: cannot reshape array of size 3 into shape (2,newaxis)

So, you can give your choice of 1-Dimension without worrying about the other dimension as long as (m * n) / your_choiceis an integer.

因此,只要(m * n) / your_choice是整数,您就可以选择一维而无需担心其他维度。

If you want to know more about this -1head over to: What does -1 mean in numpy reshape?

如果您想了解更多有关此内容的信息-1,请转到: numpy reshape 中的 -1 是什么意思?

Note: All these operations return a new array and does not modify the original array.

注意:所有这些操作都返回一个新数组并且不会修改原始数组。

回答by Markus Strauss

To convert a row vector into a column vectorin Python can be important e.g. to use broadcasting:

在 Python 中将行向量转换为列向量可能很重要,例如使用广播

import numpy as np

def colvec(rowvec):
    v = np.asarray(rowvec)
    return v.reshape(v.size,1)

colvec([1,2,3]) * [[1,2,3], [4,5,6], [7,8,9]]

Multiplies the first row by 1, the second row by 2 and the third row by 3:

将第一行乘以 1,第二行乘以 2,第三行乘以 3:

array([[ 1,  2,  3],
       [ 8, 10, 12],
       [  21, 24, 27]])

In contrast, trying to use a column vector typed as matrix:

相反,尝试使用类型为矩阵的列向量:

np.asmatrix([1, 2, 3]).transpose() * [[1,2,3], [4,5,6], [7,8,9]]

fails with error ValueError: shapes (3,1) and (3,3) not aligned: 1 (dim 1) != 3 (dim 0).

因错误而失败ValueError: shapes (3,1) and (3,3) not aligned: 1 (dim 1) != 3 (dim 0)