Book Image

Mastering Numerical Computing with NumPy

By : Umit Mert Cakmak, Tiago Antao, Mert Cuhadaroglu
Book Image

Mastering Numerical Computing with NumPy

By: Umit Mert Cakmak, Tiago Antao, Mert Cuhadaroglu

Overview of this book

NumPy is one of the most important scientific computing libraries available for Python. Mastering Numerical Computing with NumPy teaches you how to achieve expert level competency to perform complex operations, with in-depth coverage of advanced concepts. Beginning with NumPy's arrays and functions, you will familiarize yourself with linear algebra concepts to perform vector and matrix math operations. You will thoroughly understand and practice data processing, exploratory data analysis (EDA), and predictive modeling. You will then move on to working on practical examples which will teach you how to use NumPy statistics in order to explore US housing data and develop a predictive model using simple and multiple linear regression techniques. Once you have got to grips with the basics, you will explore unsupervised learning and clustering algorithms, followed by understanding how to write better NumPy code while keeping advanced considerations in mind. The book also demonstrates the use of different high-performance numerical computing libraries and their relationship with NumPy. You will study how to benchmark the performance of different configurations and choose the best for your system. By the end of this book, you will have become an expert in handling and performing complex data manipulations.
Table of Contents (11 chapters)

Introduction to vectors and matrices

A matrix is a group of numbers or elements which are arranged as a rectangular array. The matrix's rows and columns are usually indexed by letter. For a n x m matrix, n represents the number of rows and m represents the number of columns. If we have a hypothetical n x m matrix, it will be structured as follows:

If n = m, then it is called a square matrix:

A vector is actually a matrix with one row or one column with more than one element. It can also be defined as the 1-by-m or n-by-1 matrix. You can interpret a vector as an arrow or direction in an m dimensional space. Generally, the capital letter denotes a matrix, like X in this example, and lowercase letters with subscripts like X11 denote the element of the matrix X.

In addition, there are some important special matrices: the zero matrix (null matrix) and the identity matrix. 0 denotes the zero matrix, which is a matrix of all 0s (MacDufee 1943 p.27). In a 0 matrix, it's optional to add subscripts:

The identity matrix denoted by I, and its diagonal elements are 1 while the others are 0:

When you multiply a matrix X with the identity matrix, the result will be equal to X:

An identity matrix is very useful for calculating the inverse of a matrix. When you multiply any given matrix with its inverse, the result will be an identity matrix:

Let's briefly see the matrix algebra on NumPy arrays. Addition and subtraction operations for matrices are similar to math equations with ordinary single numbers. As an example:

Scalar multiplication is also pretty straightforward. As an example, if you multiply your matrix X by 4, the only thing that you should do is multiply each element with the value 4 as follows:

The seemingly complicated part of matrix manipulation at the beginning is matrix multiplication.

Imagine you have two matrices as X and Y, where X is an matrix and Y is an matrix:

The product of these two matrices will be as follows:

So each element of the product matrix is calculated as follows:

Don't worry if you didn't understand the notation. The following example will make things clearer. You have matrices X and Y and the goal is to get the matrix product of these matrices:

The basic idea is that the product of the ith row of X and the jth of column Y will become the ith, jth element of the matrix in the result. Multiplication will start with the first row of X and the first column of Y, so their product will be Z[1,1]:

You can cross-check the results easily with the following four lines of code:

In [1]: import numpy as np
x = np.array([[1,0,4],[3,3,1]])
y = np.array([[2,5],[1,1],[3,2]])
x.dot(y)
Out[1]: array([[14, 13],[12, 20]])

The previous code block is just a demonstration of how easy to calculate the dot product of two matrices by use of NumPy. In later chapters, we will go more in deep into matrix operations and linear algebra.