If we go back to high school mathematics, then we should remember the concepts of scalars and vectors. The number 2 for instance is a scalar. When we add 2 and 2, we are performing scalar addition. We can form a vector out of a group of scalars. In Python programming terms, we will then have a one-dimensional array. This concept can of course be extended to higher dimensions. Performing an operation on two arrays such as addition can be reduced to a group of scalar operations. In straight Python, we will do that with loops going through each element in the first array and adding it to the corresponding element in the second array. However, this is more verbose than the way it is done in mathematics. In mathematics, we treat the addition of two vectors as a single operation. That's the way NumPy arrays do it too and there are certain optimizations using low-level C routines, which make these basic operations more efficient. We will cover NumPy arrays in more detail in the next chapter.
Imagine that we want to add two vectors called a
and b
. A vector is used here in the mathematical sense, which means a one-dimensional array. We will learn in Chapter 4, Simple Predictive Analytics with NumPy, about specialized NumPy arrays that represent matrices. The vector a
holds the squares of integers 0
to n
, for instance. If n
is equal to 3
, then a
contains 0
, 1
, or 4
. The vector b
holds the cubes of integers 0
to n
, so if n
is equal to 3
, then the vector b
is equal to 0
, 1
, or 8
. How would you do that using plain Python? After we come up with a solution, we will compare it with the NumPy equivalent.
The following function solves the vector addition problem using pure Python without NumPy:
The following is a function that achieves the same with NumPy:
Notice that numpysum()
does not need a for
loop. Also, we used the arange
function from NumPy, which creates a NumPy array for us with integers 0
to n
. The arange
function was imported; that is why it is prefixed with numpy
.
Now comes the fun part. Remember that it is mentioned in the Preface that NumPy is faster when it comes to array operations. How much faster is Numpy, though? The following program will show us by measuring the elapsed time in microseconds, for the numpysum
and pythonsum
functions. It also prints the last two elements of the vector sum. Let's check that we get the same answers when using Python and NumPy:
The output of the program for the 1000
, 2000
, and 3000
vector elements is as follows:
Tip
Downloading the example code
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
Clearly, NumPy is much faster than the equivalent normal Python code. One thing is certain: we get the same results whether we are using NumPy or not. However, the result that is printed differs in representation. Notice that the result from the numpysum
function does not have any commas. How come? Obviously we are not dealing with a Python list, but with a NumPy array. It was mentioned in the Preface that NumPy arrays are specialized data structures for numerical data. We will learn more about NumPy arrays in Chapter 2, NumPy Basics.