NumPy is a Python package implementing efficient collections of specific types of data (generally numerical), similar to the standard array
module (but with many more features). NumPy arrays differ from lists and tuples in that the data is contiguous in memory. A Python list,
[0, 1, 2], in contrast, is actually an array of pointers to Python objects representing each number. This allows NumPy arrays to be
considerably faster for numerical operations than Python lists/tuples.
# Many useful functions are in external packages # Let's meet numpy import numpy as np
# To see what's in a package, type the name, a period, then hit tab #np? #np.
# Some examples of numpy functions and "things": print(np.sqrt(4)) print(np.pi) # Not a function, just a variable print(np.sin(np.pi)) # A function on a variable :)
2.0 3.14159265359 1.22464679915e-16
Creating a NumPy array is as simple as passing a sequence to numpy.array:
Numpy arrays are collections of things, all of which must be the same type, that work similarly to lists (as we've described them so far). The most important are:
Arrays can be created from existing collections such as lists, or instantiated "from scratch" in a few useful ways.
arr1 = np.array([1, 2.3, 4]) print(type(arr1)) print(arr1.dtype)
<type 'numpy.ndarray'> float64
You can also explicitly specify the data-type if the automatically-chosen one would be unsuitable.
arr2 = np.array([1, 2.3, 4], dtype=int) print(type(arr2)) print(arr2.dtype)
<type 'numpy.ndarray'> int64
As you might expect, creating a NumPy array this way can be slow, since it must manually convert each element of a list into its equivalent C type (int objects become C ints, etc). There are many other ways to create NumPy arrays, such as
np.zeros_like, or by manually specifying the dimensions and type of the array with the low-level creation function:
arr3 = np.ndarray((2, 3, 4), dtype=complex) # Notice : `ndarray`, not `array`! print(type(arr3))
Arrays have a
.shape attribute, which stores the dimensions of the array as a tuple:
(2, 3, 4)
For many of the examples below, we will be using
np.arange which, similar to the Python built-in function
range, returns a NumPy array
of integers from 0 to N-1, inclusive. Like
range, you can also specify a starting value and a step:
arr4 = np.arange(2, 5) print(arr4) arr5 = np.arange(1, 5, 2) print(arr5) arr6 = np.arange(1, 10, 2) print arr6
[2 3 4] [1 3] [1 3 5 7 9]
Create an array with values ranging from 0 to 10, in increments of 0.5.
Reminder: get help by typing
Since numpy exists to perform efficient numerical operations in Python, arrays have all the usual arithmetic operations available to them. These operations are performed element-wise (i.e. the same operation is performed independently on each element of the array).
A = np.arange(5) B = np.arange(5, 10) print (A+B) print(B-A) print(A*B)
[ 5 7 9 11 13] [5 5 5 5 5] [ 0 6 14 24 36]
In addition, if one of the arguments is a scalar, that value will be applied to all the elements of the array.
A = np.arange(5) print(A+10) print(2*A) print(A**2)
[10 11 12 13 14] [0 2 4 6 8] [ 0 1 4 9 16]
You can use arrays as vectors and matrices in linear algebra operations
Specifically, you can perform matrix/vector multiplication between arrays, by using the
.dot method, or the
print A.dot(B) print np.dot(A, B)
Note: This is like the '``' operator in Matlab*
Given two vectors in 3-space:
a = [1,2]
b = [4,5]
Find the angle between these vectors.
The rotation matrix of $\theta$ angle is a matrix with elements:
Create a function that takes two 3-vectors and creates the rotation matrix between them.
c=[7,8], find a vector
dthat has the same rotation as
bhas relative to
Much like the basic arithmetic operations we discussed above, comparison operations are perfomed element-wise. That is, rather than returning a
single boolean, comparison operators compare each element in both arrays pairwise, and return an
array of booleans (if the sizes of the input
arrays are incompatible, the comparison will simply return False). For example:
arr1 = np.array([1, 2, 3, 4, 5]) arr2 = np.array([1, 1, 3, 3, 5]) print(arr1 == arr2) c = (arr1 == arr2) print c.dtype
[ True False True False True] bool
Note: You can use the methods
.all() or the functions
np.all to return a single boolean indicating whether any or all values in the array are
print(np.all(c)) print(c.all()) print(c.any())
False False True
In addition to the usual methods of indexing lists with an integer (or with a series of colon-separated integers for a slice), numpy allows you to index arrays in a wide variety of different ways for more advanced operations.
First, the simple way:
a = np.array([1,2,3]) print a[0:2]
c = np.random.rand(3,3) print(c) print(c[1:3,0:2]) c[0,:] = a print(c)
[[ 0.67732547 0.92040098 0.78127807] [ 0.17040251 0.45105409 0.53734387] [ 0.01613388 0.86307387 0.01468406]] [[ 0.17040251 0.45105409] [ 0.01613388 0.86307387]] [[ 1. 2. 3. ] [ 0.17040251 0.45105409 0.53734387] [ 0.01613388 0.86307387 0.01468406]]
We can manipulate the shape of an array as follows:
A = np.arange(16).reshape(4, 4)
A = np.reshape(numpy.arange(16), (4, 4))
Using what we've learned about slicing and indexing, create a function that takes an integer as input, creates a square n-by-n array with integers from
A) and prints just the upper-left quarter of the array
For example, for A, the desired output would be:
array([[2, 3], [6, 7]])
Arrays can be indexed with other arrays, using either an array of indices, or an array of booleans of the same length. In the former case, numpy returns a view of the data in the specified indices as a new array. In the latter, numpy returns a view of the array with only the elements where the index array is True. (We'll discuss the difference between views and copies in a moment.) This makes normally-tedious operations like clamping extremely simple.
Indexing with an array of indices:
A = np.arange(5, 10) print(A) print(A[[0, 2, 3]]) A[[0, 2, 3]] = 0 print(A)
[5 6 7 8 9] [5 7 8] [0 6 0 0 9]
Indexing with a boolean array:
random = np.random A = np.array([random.randint(0, 10) for i in range(10)]) # Check out the list comprehension! print(A) A[A>5] = 5 print(A)
[0 2 9 1 9 5 6 1 3 1] [0 2 5 1 5 5 5 1 3 1]
A few more examples:
b = np.array([4,5,6]) print (a) print (b) print (a > 2) print (a[a > 2]) print (b[a > 2]) b[a == 3] = 77 print(b)
[1 2 3] [4 5 6] [False False True]   [ 4 5 77]
# There are handy ways to make arrays full of ones and zeros print(np.zeros(5)) print np.ones(5) print np.identity(5), '\n'
[ 0. 0. 0. 0. 0.] [ 1. 1. 1. 1. 1.] [[ 1. 0. 0. 0. 0.] [ 0. 1. 0. 0. 0.] [ 0. 0. 1. 0. 0.] [ 0. 0. 0. 1. 0.] [ 0. 0. 0. 0. 1.]]
A = np.arange(5)*2 print(A) B = range(5)*2 print(B)
[0 2 4 6 8] [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
Similarly, when adding two numpy arrays together, we get the vector sum back, whereas when adding two lists together, we get the concatenation back.
A = np.arange(5) + np.arange(5) print(A) B =range(5) + range(5) print(B)
[0 2 4 6 8] [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
In order to be as efficient as possible, numpy uses "views" instead of copies wherever possible. That is, numpy arrays derived from another base array generally refer to the ''exact same data'' as the base array. The consequence of this is that modification of these derived arrays will also modify the base array. The result of an array indexed by an array of indices is a ''copy'', but an array indexed by an array of booleans is a ''view''.
Specifically, slices of arrays are always views, unlike slices of lists or tuples, which are always copies.
A = np.arange(5) B = A[0:1] B = 42 print(A) A = range(5) B = A[0:1] B = 42 print(A)
[42 1 2 3 4] [0, 1, 2, 3, 4]
Figure out how to create a copy of a numpy array. Remember: since numpy slices are views, you can't use the trick you'd use for Python lists, i.e. copy = list[:].
Being designed for scientific computing, numpy also contains a host of common mathematical functions, including linear algebra functions, fast Fourier transforms, and probability/statistics functions. While there isn't space to go over ''all'' of these in detail, we will provide an overview of the most common/essential of these.
For >2-dimensional arrays, there are some other common matrix operations that can be conducted:
A = np.arange(16).reshape(4, 4) print(A) print(A.T) # transpose print(A.trace())
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] [[ 0 4 8 12] [ 1 5 9 13] [ 2 6 10 14] [ 3 7 11 15]] 30
There are many more methods like these available with NumPy arrays. Be sure to consult the numpy documentation before writing your own versions!
So far, we've used two-dimensional arrays to represent matrix-like objects. However, numpy provides a specialized class for this. The
matrix class is almost identical to a two-dimensional numpy array, but has a few changes to the interface to simplify common linear algebraic tasks. These are: * The
* operator is performs matrix multiplication * The
** operator performs matrix exponentiation * The property
.I (or the method
.getI()) returns the matrix inverse * The property
.H (or the method
.getH()) returns the conjugate transpose
la = np.linalg A = np.matrix([[3, 2, -1], [2, -2, 4], [-1, .5, -1]]) B = np.array([1, -2, 0]) print(la.solve(A, B))
[ 1. -2. -2.]
Universal functions (also called ufuncs) are high-speed, element-wise operations on NumPy arrays. They are, in essence, what allows you to operate on NumPy arrays efficiently. There are a large number of universal functions available covering most of the basic operations that get performed on data, like addition, subtraction, logarithms, and so on. Calling a ufunc is a simple matter:
A = np.arange(1,10) print(np.log10(A))
[ 0. 0.30103 0.47712125 0.60205999 0.69897 0.77815125 0.84509804 0.90308999 0.95424251]
In addition to basic operation like above, ufuncs that take two input arrays and return an output array can be used in more advanced ways.
Using ufuncs, calculate the reciprocals of each element in the following array:
[8.1, 1.6, 0.9, 4.3, 7.0, 7.3, 4.7, 8.2, 7.2, 3.0, 1.4, 9.8, 5.7, 0.7, 8.7, 4.6, 8.8, 0.9, 4.4, 4.4]
Scipy is a huge library of scientific software. It includes many useful things, such as functions for statistics, for signal processing, image processing, etc.
Because it's very large, importing it doesn't actually bring in all of its modules and those need to be imported individually.
import scipy # scipy?
For example, if you want to look at physical constants, you need to specifically import the
import scipy.constants as const
['alpha particle mass', 'alpha particle mass energy equivalent', 'alpha particle mass energy equivalent in MeV', 'alpha particle mass in u', 'alpha particle molar mass', 'alpha particle-electron mass ratio', 'alpha particle-proton mass ratio', 'electron to alpha particle mass ratio']
print(const.physical_constants['alpha particle mass'])
(6.64465675e-27, 'kg', 2.9e-34)
There are a lot of gems hidden in the
import scipy.special as sps # We'll talk about matplotlib later today: import matplotlib.pylab as pylab x = np.arange(0.0, 10.1, 0.1) for n in range(4): j = sps.jn(n, x) pylab.plot(x, j, label='Bessel order=%s'%n) pylab.legend() pylab.show()
import scipy.misc as misc
import matplotlib.pylab as pylab pylab.matshow(misc.lena()) pylab.show()
As a short-cut, you can import many useful things using the following command:
Welcome to pylab, a matplotlib-based Python environment [backend: module://IPython.kernel.zmq.pylab.backend_inline]. For more information, type 'help(pylab)'.
This pulls into your namespace numpy (as
np), as well as everything that is in numpy:
array([1,2,3]) # Notice - I am calling this 'bare' without the preceding 'np'!
array([1, 2, 3])
Also - plots will from now on appear 'inline', as part of the cell output
[<matplotlib.lines.Line2D at 0x10cbf0b90>]