Tips for python and numpy from weird bugs

Tips to avoid weird bugs in Python code:


  • Don't work with data structure with shape like (5, ) or (, 3) such as a.np.random.randn(5) creating a "rank 1 array" with shape (5, ). Should always making data either a row vector (1, 5) or a column vector (5, 1)
  • Use assert(a.shape == (5,1)) to check for shape
  • Re-shape a rank 1 array by using reshape: a = a.reshape((5,1))

Comments