0

I'm a new learner in the Python language. When a new vector is generated, is it a column or row vector by default?

import numpy as np

theta = np.arange(3)
a = len(theta.T)
b = len(theta)
print('theta = {} \n theta.T = {}'.format(theta,theta.T))
c = theta.T.dot(theta)
d = theta.dot(theta.T)

It turns out a == b == 3, c == d, and both theta and theta.T are displayed as a row vector.

But this matters when I want to calculate the derivative of of symbolic function x · xT with x a row vector.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • By default, numpy will treat both vector and transpose vector as the same, so when you find the dot product of `x@x`, you can still get the answer. –  Sep 25 '22 at 11:55
  • It's neither a row nor a column vector since it is not a 2D array. `np.array` are not vectors or matrices. – Michael Szczesny Sep 25 '22 at 12:30
  • `np.transpose` explains what happens to a 1-D array. `np.dot` explains how it handles 1-D arrays. – hpaulj Sep 25 '22 at 16:04
  • Does this answer your question? [Python: Differentiating between row and column vectors](https://stackoverflow.com/questions/17428621/python-differentiating-between-row-and-column-vectors) – T C Molenaar Sep 26 '22 at 07:07

1 Answers1

1

Neither, it is a 1D array as:

>>> theta.shape
(3,)

A column vector would have a shape equal to (3,1), a row vector: (1,3). You can create it by changing the shape

>>> theta.shape = (1,3)
>>> theta
array([[0, 1, 2]])
>>> theta.shape = (3,1)
>>> theta
array([[0],
       [1],
       [2]])
T C Molenaar
  • 3,205
  • 1
  • 10
  • 26