back · main · about · writing · notes · d3 · contact

44 Numpy Functions
25 Jun 2017 · 776 words

Have you heard about the Python package numpy? Probably.

But maybe you’re looking for an easy reference of useful numpy functions. Maybe that’s why you’re here. Well, you’re in luck!

Get started by opening up a editor. First, we’ll import the numpy package: import numpy as np.

Now cast your eye over these functions. In no particular order:

  1. np.__version__ | Return the version of numpy you have loaded.
  2. np.shape(x) | return the shape of an array x. It’s essentially the number of rows and columns in x.
  3. np.ndim(x) | return the number of dimensions of an array x
  4. np.zeros(shape) | create an array of zeros in the shape you specify
  5. np.ones(shape) | create an array of ones in the shape you specify
  6. np.eye(n) | create a identity matrix with nrows and ncolumns
  7. np.arange(start, stop, step) | create evenly spaced values that are stepapart, between a startand endvalue
  8. np.linspace(start, stop, num) | create num  evenly spaced values between a start and end value.
  9. np.reshape(x, newshape) | change the shape of x to newshape
  10. np.random.random(size) | return sizerandom numbers between [0,1).
  11. np.random.rand(d0, d1, ..., dn) | random uniformly distributed values in [0,1), in shape (d0 , d1 , …, dn ).
  12. np.random.randn(d0, d1, ..., dn) | random normally distributed values from the standard normal distribution, in a shape (d0 , d1 , ..., dn ).
  13. np.random.normal(loc, scale, size) | draw size random samples from a N ( loc, scale^2 ) distribution.
  14. np.random.randint(low, high, size) | draw sizerandom numbers from a U(low, high ) distribution.
  15. np.pad(x) | pads an array. Parameters determine what you pad the array with, how large the pad is and the mode of padding (there are lots!).
  16. np.diag(x, k) | construct a diagonal array, with values x down the diagonal k.
  17. np.tile(x, reps) | repeat x a total of reps times, where reps can be of multiple dimensions.
  18. np.unravel_index(indices, dims) | in an array of shape dims , what is the index of the indicesth element? For example, np.unravel_index( 32, (3,3,5) ) # = (2, 0, 2).
  19. np.dtype() | create your own custom data types.
  20., B) | find the dot product of two matrices A and B.
  21. np.ndarray.astype(dtype) | change the data type of an array while making a copy of it.
  22. np.ceil(x) | rounds decimal numbers up to the nearest integer.
  23. np.floor(x) | rounds decimal numbers down to the nearest integer.
  24. np.copysign(x1, x2) | changes the sign of elements in array x1 to that of elements in array x2, comparing element|wise.
  25. np.intersect1d(x1, x2) | find the intersection of array x1 and array x2 , returning an ordered set.
  26. np.union1d(x1, x2) | find the union of array x1 and array x2 , returning an ordered set.
  27. np.datetime64('s1') | convert a string s1 to a numpy datetime.
  28. np.timedelta64('s1') | convert a string s1  to a numpy timedelta, with which you can perform date arithmetic.
  29. np.arange('s1', 's2', dtype='datetime64[D]') | get a list of days between two dates s1 and s2.
  30. np.add(x1, x2, out) | add two arrays x1 and x2 . If out equals x1 , then x1 will be overwritten with the result of the addition. Same thing for np.multiply, np.divide, np.negative.
  31. np.trunc(x) | get rid of decimal points in an floating point array x, leaving just the integer components.
  32. np.sort(x) | sort an array x in ascending order
  33. np.sum(x, axis) | return the sum of an array over a particular axis.
  34. np.add.reduce(x, axis) | a quicker way of finding sum of an array over a particular axis, for small x . This is an example of a ufunc.
  35. np.array_equal(x1, x2) | check to see if two arrays x1 and x2 are equal.
  36. np.meshgrid(x1, x2) | create a 2d rectangular grid of values from array x1 and array x2 . See here for further explanation.
  37. np.outer(x, y) | calculate the outer product of two vectors and y .
  38. np.setprintoptions(threshold) | change the number of elements displayed when printing an array to the console.
  39. np.argmax(x) | return the indices of the maximum values along an axis for an array x .
  40. np.argmin(x) | return the indices of the minimum values along an axis for an array x .
  41. np.put(x, ind, v) | put values v into an array x at indices ind, replacing what was there before.
  42. np.argsort(x) | return indices that would sort an array x . See here for further explanation.
  43. np.any(x, axis) | test if any array element of x along a given axis evaluates to True.
  44. np.ndarray.flat() | a flat iterator object to iterate over arrays. Can be indexed with square brackets.

Knowing how to use these functions will give you a great starting base for your numpy adventures. Good luck!

back · main · about · writing · notes · d3 · contact