User Tools

Site Tools


numpy

Numpy Lessons Learned

# -*- mode: python; -*-
import numpy as np

Plotting

  • If we don't need to show plots (only save), use the Agg backend

# -*- mode: python; -*-
import matplotlib as mpl
mpl.use('agg')
import numpy as np
import matplotlib.pyplot as plt

Indexing, Reshaping

  • Here, nx*ny=3*3 and nz=2.
  • Proper reshaping order for 3d array/matrix/tensor operations:

# -*- mode: python; -*-
a = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], dtype=float)
b = a.reshape(3,3,2)
c = b.reshape(18, 1, 1).flatten()

where the following are equivalent

# -*- mode: python; -*-
c = b.reshape(18,1,1).flatten()
c = b.reshape(18)

numpy.txt · Last modified: 2013/07/10 07:02 (external edit)