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)
In [4]:
print(a.shape) # Rank 1 array; NOTE: neither a row or col vector
In [5]:
# Transpose doesn't appear to do anything
print(a.T)
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))
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)
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
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))