We learned the basics of Python Programming Language in the previous chapter. This chapter is fully focused on learning the basics of NumPy library. We will have a lot of hands-on programming in this chapter. While the programming is not very difficult when it comes to NumPy and Python, the concepts are worth learning. I recommend all readers to spend some time to comprehend the ideas presented in this chapter.
One of the major prerequisites of this chapter is that readers should have explored Jupyter Notebook for Python programming. If you have not already (as I prescribed toward the end of the previous chapter), I recommend you to learn to use it effectively. The following is the list of web pages teaching how to use Jupyter Notebook effectively:
www.dataquest.io/blog/jupyter-notebook-tutorial/
https://jupyter.org/documentation
https://realpython.com/jupyter-notebook-introduction/
www.tutorialspoint.com/jupyter/jupyter_notebook_markdown_cells.htm
www.datacamp.com/community/tutorials/tutorial-jupyter-notebook
Getting started with NumPy
Multidimensional Ndarrays
Indexing of Ndarrays
Ndarray properties
NumPy constants
After studying this chapter, we will be comfortable with the basic aspects of programming with NumPy.
Getting Started with NumPy
I hope that you are comfortable with Jupyter enough to start writing small snippets. Create a new Jupyter Notebook for this chapter, and we will always write our code for each chapter in separate Jupyter Notebooks. This will keep the code organized for reference.
We know that Python 3 and pip3 are accessible in Linux by default, and we added Python’s installation directory in the system environment variable PATH in Windows OS while installing. That is why the command we just executed should run without any errors and install NumPy library to your computer.
In case the output of the execution of the command shows a warning saying that the pip has a new version, you may wish to upgrade your pip utility with the following command:
Remember that this will not have any impact on the libraries we install or the code examples we demonstrate. Libraries are fetched from the repository at https://pypi.org/ (also known as Python Package Index), and any version of pip utility can install the latest version of the libraries.
Once we are done installing the NumPy (and upgrading the pip if you choose to do so), we are ready to start programming with NumPy. But wait! What is NumPy and why are we learning it? How is it related to machine learning? All these questions must be bothering you since you started reading this chapter. Let me answer them.
NumPy is the fundamental library for the numerical computation. It is an integral part of the Scientific Python Ecosystem. If you wish to learn any other library of the ecosystem, I (or any seasoned professional for that matter) will recommend you learn NumPy first. NumPy is important because it is used to store the data. It has a basic yet very versatile data structure known as Ndarray. It means N Dimensional Array. Python has many array-like data structures (e.g., list). But Ndarray is the most versatile and the most preferred structure to store scientific and numerical data.
Many libraries have their own data structures, and most of them use Ndarrays as their base. And Ndarrays are compatible with many data structures and routine just like the lists. We will see the examples of these in the next chapter. But for now, let’s focus on Ndarrays.
This URL has a full list of the data types supported by Ndarray:
https://numpy.org/devdocs/user/basics.types.html
Multidimensional Ndarrays
Indexing of Ndarrays
Just like lists, it follows C style indexing where the first element is at the position of 0 and the nth element is at the position (n-1).
We can access elements of Ndarrays this way.
Ndarray Properties
NumPy Constants
Summary
In this chapter, we familiarized ourselves with Python and IDLE. We learned how to use Python interpreter and how to write Python scripts. We also learned how to execute Python programs. In the next chapter, we will continue our journey with NumPy.