# Python: Differentiating between row and column vectors

1 view
in Python

In Python, Is there any way for differentiating between column and row vectors? Till now I'm using scipy, numpy, and let's say if I was about to give one a vector,

from numpy import *

Vector = array([1,2,3])

It can't able to say, whether I referred to a row or a column vector.

Moreover,

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

True

I also realized that functions on vectors from the described modules, don't need differentiation. Example, outer(a,b) or a.dot(b), but I want to differentiate them. Anyone give me some suggestions.

by (26.4k points)

Try the following code, just add 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, try to force it to be a column vector

>>> a.shape = (3,1)

>>> a

array([,

,

])

>>> a.transpose()

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

>>> a.dot(a.transpose())

array([[1, 2, 3],

[2, 4, 6],

[3, 6, 9]])

If want to make a distinction, you can use np.newaxis:

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

>>> a

array([1, 2, 3])

>>> a[:, np.newaxis]

array([,

,

])

>>> a[np.newaxis, :]

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

Want to become a Expert in Python? Come and Join: Python course