numpy for Numerical and Scientific Computing

As you approach the end of this introduction to constructing arrays in numpy, read this section to familiarize yourself with various numpy methods used throughout the course. Specifically, the need for methods such as shape, size, linspace, reshape, eye, and zeros often arise when manipulating arrays.

Numpy (numerical and scientific computing)

We start by importing the Numpy package into Python using the alias np.

import numpy as np

Numpy provides both arrays (vectors, matrices, higher dimensional arrays) and vectorized functions which are very fast. Let's see how this works.

z = [1,2,3,4,5,6,7,8,9.3,10.6] # This is a list
 z_array = np.array(z)
 z_array
array([ 1. ,  2. ,  3. ,  4. ,  5. ,  6. ,  7. ,  8. ,  9.3, 10.6])

Now, we have already seen functions in Python earlier. In Numpy, there are functions that are optimized for arrays that can be accessed directly from the array objects. This is an example of object-oriented programming in Python, where functions are provided for particular classes of objects, and which can be directly accessed from the objects. We will use several such functions over the course of this workshop, but we won't actually talk about how to do this program development here.

Numpy functions are often very fast, and are vectorized, i.e., they are written to work on vectors of numbers rather than single numbers. This is an advantage in data science since we often want to do the same operation to all elements of a column of data, which is essentially a vector

We apply the functions sum, min (minimum value) and max (maximum value) to z_array.

z_array.sum()
55.9
z_array.min()
1.0
z_array.max()
10.6

The versions of these functions in Numpy are optimized for arrays and are quite a bit faster than the corresponding functions available in base Python. When doing data work, these are the preferred functions.

These functions can also be used in the usual function manner:

np.max(z_array)
10.6

Calling np.max ensures that we are using the max function from numpy, and not the one in base Python.