的Python:行和列向量之间的区别 [英] Python: Differentiating between row and column vectors

查看:4343
本文介绍了的Python:行和列向量之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在Python的行和列向量区分的好办法?到目前为止,我使用numpy的和SciPy的和我所看到的迄今为止的是,如果我是给一个矢量,说

Is there a good way of differentiating between row and column vectors in python? So far I'm using numpy and scipy and what I see so far is that If I was to give one a vector, say

from numpy import *
Vector = array([1,2,3])

他们不能够说天气我的意思是一行或一列向量。此外:

they wouldn't be able to say weather I mean a row or a column vector. Moreover:

array([1,2,3]) == array([1,2,3]).transpose()
True

其中在现实世界简直是不真实的。
我意识到,大多数对媒介功能的形成提到的模块并不需要分化。例如外(A,B) a.dot(B)但我想区分我自己方便。

Which in "real world" is simply untrue. I realize that most of the functions on vectors form the mentioned modules don't need the differentiation. For example outer(a,b) or a.dot(b) but I'd like to differentiate for my own convenience.

推荐答案

您可以通过添加另一个维度的数组进行区分明确。

You can make the distinction explicit by adding another dimension to the array.

>>> a = np.array([1, 2, 3])
>>> a
array([1, 2, 3])
>>> a.transpose()
array([1, 2, 3])
>>> a.dot(a.transpose())
14

现在迫使它是一个列向量:

Now force it to be a column vector:

>>> a.shape = (3,1)
>>> a
array([[1],
       [2],
       [3]])
>>> a.transpose()
array([[1, 2, 3]])
>>> a.dot(a.transpose())
array([[1, 2, 3],
       [2, 4, 6],
       [3, 6, 9]])

另一种选择是,当你想区分使用np.newaxis:

Another option is to use np.newaxis when you want to make the distinction:

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

这篇关于的Python:行和列向量之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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