Ordered arrays

  • np.arange(b)
    • gives an array with b elements, starting from zero
  • np.arange(a,b)
    • starts the array at a and ends at b - 1
  • np.arange(a,b,c)
    • starts at a, increases in increments of c, stops at b - c.
  • np.linspace(a,b,c)
    • gives an array starting at a and ending at b with c elements
      • c defaults to 50

Similar arrays

  • np.zeros( (a,b) )
    • gives an array in the shape of (a,b) filled with zeros
  • np.ones( (a,b) )
    • gives an array in the shape of (a,b) filled with ones
  • np.full( (a,b),c)
    • gives an array in the shape of (a,b) filled with c
  • np.zeros_like(A)
    • gives an array in the same shape as A but filled with zeros
  • np.ones_like(A)
    • gives an array in the same shape as A but filled with ones
  • np.full_like(A,b)
    • gives an array in the same shape as A but filled with b

Copying/manipulating

  • np.copy()
    • creates a new array that isn't linked back to the old one.
      • example: b = a will make it so that changes to b propagate back to a
      • b = a.copy() will make b not pass any changes backwards.

Indexing

For 1d arrays:

  • A[1:] will give all but the first element of A
  • A[:-1] will give all but the last element of A
  • A[::2]] will give every other element of A
  • X = (A[1:] + A[:-1]) / 2 will create a new array X that interpolates between the values of A
    • This works by adding the offset arrays together as follows: A[1] + A[0], A[2] + A[1], … , A[-1] + A[-2] and then halving the results.

for 2d arrays:

  • B[0][0] will give the element in the first row & column of B
    • B[0,0]] does the same
  • B[1:] will give all but the first row of B
    • B[:][1:] will do the same
  • B[:-1] will give all but the last row of B
  • B[:,1:] will give all but the first column of B
  • B[a:b:c] will give rows a through b in steps of c
    • B[::-1]] will reverse the order of rows in the array
  • B[::,a,b,c]] will give columns a through b in steps of c
    • B[::,::-1]] will reverse the order of columns in the array
    • B[::-1,::-1] will reverse both the order of rows and columns in the array simultaneously (mirroring on the off-diagonal)

Indexing with logic

  • B = A>5 will create an array B the same size as A. Elements of B will be True where the matching element of A is greater than 5, and False otherwise
  • A[A>5] will return only the elements of A which are larger than 5.
    • This does not preserve the shape of the array, so be careful in 2d!
    • B = A>5 followed by A[B] does the same thing as A[A>5]
  • A[A>5 & A<8] does not work and gives an error ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
    • This error is because Numpy can't tell if we want to know if we want to know if any elements in A meet the conditions or if we want to know if all of the elements in A meet the conditions. In practice, we want neither.
    • B = A>5 then C=A<8 followed by A[B & C] behaves as intended, returning elements of A that are greater than 5 but less than 8.
    • A[(A>5) & (A<8)] works as it evaluates each clause separately and then combines them.
    • One final method is to use Python's map function, which lets you apply an operation to each element in an array. It returns an iterable object, so you need to convert it to a Python list
      • def filter(value): return value > 5 & value < 8
      • A[list(map(filter,A))]

Suppose you want to show a subset of data in a plot, where X values are between 100 and 200.

lims = (X >100) & (X < 200)

ax.plot(X[lims],Y[lims])

Loading data

  • np.loadtxt(a.txt) will try to load data from a.txt, using whitespace as a separator
    • delimiter = “,” will indicate that a , separates data entries
    • skiprows = n will skip the first n rows in the file
    • usecols = (a,b,c,…) will keep only data from the columns in the list (a,b,c,…)
      • Either a Python list or Numpy array will work here.
  • np.genfromtxt(a.txt) will also load data from a.txt using whitespace as a separator, but handles missing values more gracefully
    • skip_header = n is used instead of skiprows

Combining Arrays

  • C = np.append(A,B) will create C, which extends A by adding B data to the end of it.
    • If you are working with 1d arrays, this will result in a single longer array
    • C = np.hstack(A,B) will do the same
  • C = np.vstack(A,B) will create a 2D array C by making rows of A and B
    • This can also just add new rows onto an existing 2D array
    • This only works if the A and B are the same length

Misc utility

  • np.squeeze(A) will remove singleton dimensions of array A
    • np.squeeze([A])[0] will give you the 0th element of A
    • [A][0] will just give you the array A
    • Mostly this is useful if you've got an extra dimension of array crammed into your data randomly