Numpy tutorial- Getting started | applied electronics engineering

# Numpy tutorial- Getting started

By Applied Electronics - Wednesday, May 25, 2016 No Comments
Numpy is a python library that is optimized for array computation for python programming language. In this blog post, we show how to do basic things in Numpy. Also we show which free IDE or software you can use for learning Numpy.

To write python code and program or work interactively in python language there are lots of IDE out there. Some of the well know includes the python IDLE, pycharm, wingIDE, Anaconda etc. In many cases, like if you have IDLE, you have to install Numpy package separately using package install like pip. Therefore in order to avoid such hassle, you can download the Anaconda. Anaconda comes with build in packages such as Numpy, Scipy, Matplotlib as well Spyder IDE(Integrated Development Environment). See the previous blog post Using Anaconda for numpy, scipy and matplotlib programming.

Below is a screenshot of how the Spyder IDE looks like.

Spyder IDE is completely customize-able. In the above picture, you can see the python script editor window and the console window but you can also choose to show other windows such as variable explorer, object explorer, file explorer etc.

The console that you see is the Ipython console which is a little bit different than the ordinary python console. In this console you can write python codes or statements and get immediate outputs. This is because python being an interpreted language you don't have to compile the code.

Once you have the Spyder IDE you can start typing in python codes. To use Numpy you have to first import the library itself. There are several ways to do so.

One way to import the Numpy library is to use the import function. This is shown below.

# import numpy

Once you have imported numpy, you then can use the functions of numpy like the arange function.

# numpy.arange(5)
= array([0, 1, 2, 3, 4])

This works but you may find(maybe later on) that type numpy for every function you want to use is tedious. So the alternative way would to import numpy as np namespace. Doing so you can use np instead of typing numpy.

# import numpy as np

# np.arange(5)
= array([0, 1, 2, 3, 4])

Yet another quicker way to do the same thing would be not to use the numpy or np at all for using the numpy functions. To do this type the following.

# from numpy import *

# arange(5)
= array([0, 1, 2, 3, 4])

As you can you can now directly use the numpy function.

So now that you know how to get the software to learn numpy for free let's look at the arange function of numpy that we have used earlier.

The arange function simply creates a one dimensional array. It is like a 1D list but it has more optional parameters. Specifying only one parameter creates a 1D array with elements starting from 0 upto n-1 where n is the number specified. In the above example, n is 5 so the arange function creates 1D array 0 upto 4. Similarly, arange function can be given two parameters, where the first is the start of the array and the second is the end of the array excluding the inputted number itself. For example, arange(2,5) means 2,3,4. arrange function also accepts a third parameter which is used to specify the step size. For example if we use arange(2,5,0.2) we get an array of 2. ,  2.2,  2.4,  2.6,  2.8,  3. ,  3.2,  3.4,  3.6,  3.8,  4. , 4.2,  4.4,  4.6,  4.8. If we don't specify step size the default of 1 will be used. Likewise we can have a 4th parameter to specify the data type of the elements. This is shown below.

# arange(2,5, dtype = float32)
= array([ 2.,  3.,  4.], dtype=float32)

Now we show you how to get help for numpy function. To know for example how the function arange works, what arguments or parameters it expects you can use the help function in Ipython. To use the help function just type help followed by the name of the function inside parenthesis of the help function.

# from numpy import *

# help arange
-> help(arange)
Help on built-in function arange in module numpy.core.multiarray:

arange(...)
arange([start,] stop[, step,], dtype=None)

So this part of the Numpy tutorial showed what software IDE you can use to learn Numpy, showed you one basic numpy function called arange( ) and also showed you how to get help in Ipython for the numpy functions or objects.