In [1]:
# Tips and tricks when using np to avoid hard-to-find bugs
import numpy as np

In [3]:
a = np.random.randn(5)  # Random 5 element np array
print(a)


[ 1.78197376 -0.2610038   0.46281843  0.1357311   0.77853057]

In [4]:
print(a.shape)  # Rank 1 array; NOTE: neither a row or col vector


(5,)

In [5]:
# Transpose doesn't appear to do anything
print(a.T)


[ 1.78197376 -0.2610038   0.46281843  0.1357311   0.77853057]

In [6]:
# Since a.T doesn't appear to do anything, might expect this to
# return a matrix, but instead returns a scalar
print(np.dot(a, a.T))


4.0822871537412375

In [8]:
# To avoid weird effects, just explicitly use row or col vectors:
a = np.random.randn(5, 1)  # 5 x 1 col vector
print(a)


[[-1.26940263]
 [ 1.10523065]
 [ 0.41045117]
 [ 0.62152304]
 [-0.45385147]]

In [11]:
# Note the two square brackets instead of just 1.
# BEWARE OF THE 1 SQUARE BRACKET (means np array)
print(a.T)  # Row vector


[[-1.26940263  1.10523065  0.41045117  0.62152304 -0.45385147]]

In [12]:
# Also a good idea in code to check expected shape of matrices
assert(a.shape == (5, 1))

In [15]:
# If needed, can reshape rank 1 np array's to row or col vectors
a = a.reshape((5, 1))