To learn about the numpy
extension and the entire scipy
stack visit scipy.org and numpy.org. The packages provided in the scipy
stack are the cornestone of scientific computing in Python. Even though we will only be using numpy
and matplotlib
you should browse through the core packages in the scipy
stack. They will no doubt come in handy further down the road!
A numpy
array is a list-like class designed to facilitate the use of multidimensional arrays for scientific computation in Python. The Python list
type has many desireable properties. Chief among them is flexibility. We can grow them, shrink them, store whatever we want in them. All of this comes at a price though. If we are working on something computationally expensive, lists can really slow us down. In the scientific realm we are mainly concerned with arrays of numbers and we don't really need many of the nice flexibility properties that lists have. For this reason, numpy arrays were born. We give up some of the flexibility of lists and in return we get speed.
There are two major concessions we make to use numpy arrays instead of lists:
numpy
arrays.To use the numpy extension we must first install it and then import it. Fortunately for us, it comes with the Anaconda distribution of Python so it's already intalled. So we just need to import it.
numpy
array:¶n
zeros.n
ones.numpy
function arange
numpy
function linspace
numpy
function random.rand
import numpy as np
l=[1,2,3]
a=np.array(l)
a
What just happened? We created the numpy
array a
from the list l
.
import numpy as np
a=np.zeros(5)
a
What just happened? We used the numpy
function zeros
to create an array of 5 zeros. Notice that the zeros are floating point numbers, not integers. This is the default.
import numpy as np
a=np.ones(5)
a
What just happened? We used the numpy
function ones
to create an array of 5 ones. Notice that the ones are floating point numbers, not integers. This is the default.
import numpy as np
a=np.arange(10)
a
What just happened? We used the numpy
function arange
to create an array of consecutive integers from 0 to 9. This function is similar to the built-in Python function range
.
import numpy as np
a=np.linspace(0,1,11)
a
What just happened? We used the numpy
function linspace
to create an array of 11 floating point numbers evenly spaced betwen the start, 0, and the end, 1.
import numpy as np
a=np.random.rand(10)
a
What just happened? We used the numpy
function random.rand
to create an array of 10 random floating point numbers chosen uniformly from the half-open interval [0,1).
dtype
- The (numpy) type of the elements contained in the array. Usually numeric, and numpy
has its own numeric types similar to the usual Python numerics.ndim
- The number of dimensions the array is organized intoshape
- A tuple whose elements are the length of the array in each of its dimensionssize
- The total number of elements in the arrayshape
and the dtype
. However, don't try to explicitly change the dtype
, you probably won't get what you're expecting. We'll see how to do this with a the astype
method in a bit.#construct an array of 12 ones
import numpy as np
a=np.ones(12)
#heck each of the array's properties
print(a.dtype) #depending on your computer this may be float64 or float32
print(a.ndim)
print(a.shape) # since our array is 1 dimensional this will be a tuple with a single element.
print(a.size)
# explicity change the shape
a.shape=(3,4)
a
We can access individual elements of a numpy
array using the same array notation we used to access individual elements of a lists. Similarly we can slice numpy
arrays in much the same way we learned to slice lists. There are a few convenient extras though when accessing numpy
arrays.
import numpy as np
a=np.arange(12) # makes an array with 12 elements.
a.shape=(3,4) # shapes the array into 3 rows of 4 columns
print(a)
a[0][0]==a[0,0] # True because we can abbreviate the a[0][0] with a[0,0]
Because numpy
arrays are so often multidimensional we can shortcut the list notation a[i][j]
and simply comma separate the individual dimension values inside a single set of brackets like a[i,j]
.
What if we want to access the entire second row of the array above?
print(a[1,:])
# or we can also just use
print(a[1])
I prefer the first approach. It makes it clear that I'm returning more than just a single number.
Note that neither of the above array methods mutate the array they are operating on. They simply return a new array with the specified properties
print('a is:\n',a) # contiuing with a from before
print(a.reshape((4,3))) # return a new array with 4 rows of thre columns
print(a.astype(np.float)) # return a new array with element types converted to numpy floats
print('a is:\n ',a) # and a is unchanged.