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.
In [1]:
# Many useful functions are in external packages
# Let's meet numpy
import numpy as np
In [2]:
# To see what's in a package, type the name, a period, then hit tab
#np?
#np.
In [3]:
# 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 :)
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.
In [4]:
arr1 = np.array([1, 2.3, 4])
print(type(arr1))
print(arr1.dtype)
You can also explicitly specify the data-type if the automatically-chosen one would be unsuitable.
In [5]:
arr2 = np.array([1, 2.3, 4], dtype=int)
print(type(arr2))
print(arr2.dtype)
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.identity
, np.zeros
, np.zeros_like
, or by manually specifying the dimensions and type of the array with the low-level creation function:
In [6]:
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:
In [7]:
print(arr3.shape)
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:
In [8]:
arr4 = np.arange(2, 5)
print(arr4)
arr5 = np.arange(1, 5, 2)
print(arr5)
arr6 = np.arange(1, 10, 2)
print arr6
In [9]:
A = np.arange(5)
B = np.arange(5, 10)
print (A+B)
print(B-A)
print(A*B)
In [10]:
A = np.arange(5)
print(A+10)
print(2*A)
print(A**2)
In [11]:
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:
[[cos($\theta$), -sin($\theta$],
[sin($\theta$, cos($\theta$)]]
Create a function that takes two 3-vectors and creates the rotation matrix between them.
c=[7,8]
, find a vector d
that has the same rotation as b
has relative to a
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:
In [12]:
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
Note: You can use the methods .any()
and .all()
or the functions np.any
and np.all
to return a single boolean indicating whether any or all values in the array are True
, respectively.
In [13]:
print(np.all(c))
print(c.all())
print(c.any())
In [14]:
a = np.array([1,2,3])
print a[0:2]
In [15]:
c = np.random.rand(3,3)
print(c)
print(c[1:3,0:2])
c[0,:] = a
print(c)
We can manipulate the shape of an array as follows:
A = np.arange(16).reshape(4, 4)
Or even:
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 0
to n**2-1
(like 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:
In [16]:
A = np.arange(5, 10)
print(A)
print(A[[0, 2, 3]])
A[[0, 2, 3]] = 0
print(A)
Indexing with a boolean array:
In [17]:
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)
A few more examples:
In [18]:
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)
In [19]:
# 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'
In [20]:
A = np.arange(5)*2
print(A)
B = range(5)*2
print(B)
Similarly, when adding two numpy arrays together, we get the vector sum back, whereas when adding two lists together, we get the concatenation back.
In [21]:
A = np.arange(5) + np.arange(5)
print(A)
B =range(5) + range(5)
print(B)
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.
In [22]:
A = np.arange(5)
B = A[0:1]
B[0] = 42
print(A)
A = range(5)
B = A[0:1]
B[0] = 42
print(A)
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:
In [23]:
A = np.arange(16).reshape(4, 4)
print(A)
print(A.T) # transpose
print(A.trace())
There are many more methods like these available with NumPy arrays. Be sure to consult the numpy documentation before writing your own versions!
matrix
classSo 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
In [24]:
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))
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:
In [25]:
A = np.arange(1,10)
print(np.log10(A))
In [25]:
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]
In [26]:
import scipy
# scipy?
For example, if you want to look at physical constants, you need to specifically import the constants
module.
In [27]:
import scipy.constants as const
In [28]:
print(const.c)
In [29]:
print(const.find("alpha"))
In [30]:
print(const.physical_constants['alpha particle mass'])
There are a lot of gems hidden in the special
and misc
modules
In [31]:
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()
In [32]:
import scipy.misc as misc
In [33]:
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:
In [34]:
%pylab inline
This pulls into your namespace numpy (as np
), as well as everything that is in numpy:
In [35]:
array([1,2,3]) # Notice - I am calling this 'bare' without the preceding 'np'!
Out[35]:
Also - plots will from now on appear 'inline', as part of the cell output
In [36]:
plot(array([1,2,3]))
Out[36]:
In [ ]: