Reader small image

You're reading from  NumPy Essentials

Product typeBook
Published inApr 2016
Reading LevelIntermediate
Publisher
ISBN-139781784393670
Edition1st Edition
Languages
Tools
Right arrow
Authors (3):
Leo (Liang-Huan) Chin
Leo (Liang-Huan) Chin
author image
Leo (Liang-Huan) Chin

Leo (Liang-Huan) Chin is a data engineer with more than 5 years of experience in the field of Python. He works for Gogoro smart scooter, Taiwan, where his job entails discovering new and interesting biking patterns . His previous work experience includes ESRI, California, USA, which focused on spatial-temporal data mining. He loves data, analytics, and the stories behind data and analytics. He received an MA degree of GIS in geography from State University of New York, Buffalo. When Leo isn't glued to a computer screen, he spends time on photography, traveling, and exploring some awesome restaurants across the world. You can reach Leo at http://chinleock.github.io/portfolio/.
Read more about Leo (Liang-Huan) Chin

Tanmay Dutta
Tanmay Dutta
author image
Tanmay Dutta

Tanmay Dutta is a seasoned programmer with expertise in programming languages such as Python, Erlang, C++, Haskell, and F#. He has extensive experience in developing numerical libraries and frameworks for investment banking businesses. He was also instrumental in the design and development of a risk framework in Python (pandas, NumPy, and Django) for a wealth fund in Singapore. Tanmay has a master's degree in financial engineering from Nanyang Technological University, Singapore, and a certification in computational finance from Tepper Business School, Carnegie Mellon University.
Read more about Tanmay Dutta

Shane Holloway
Shane Holloway
author image
Shane Holloway

http://shaneholloway.com/resume/
Read more about Shane Holloway

View More author details
Right arrow

Chapter 5. Linear Algebra in NumPy

NumPy is designed for numeric computations; underneath the hood it is still the powerful ndarray object, but at the same time NumPy provides different types of objects to solve mathematical problems. In this chapter, we will cover the matrix object and polynomial object to help you solve problems using a non-ndarray way. Again, NumPy provides a lot of standard mathematical algorithms and supports multi-dimensional data. While a matrix can't perform three-dimensional data, using the ndarray objects with the NumPy functions of linear algebra and polynomials is more preferable (the extensive SciPy library is another good choice for linear algebra, but NumPy is our focus in this book). Let's use NumPy to do some math now!

The topics that will be covered in this chapter are:

  • Matrix and vector operations
  • Decompositions
  • Mathematics of polynomials
  • Regression and curve fitting

The matrix class


For linear algebra, using matrices might be more straightforward. The matrix object in NumPy inherits all the attributes and methods from ndarray, but it's strictly two-dimensional, while ndarray can be multi-dimensional. The well-known advantage of using NumPy matrices is that they provide matrix multiplication as the * notation; for example, if x and y are matrices, x * y is their matrix product. However, starting from Python 3.5/NumPy 1.10, native matrix multiplication is supported with the new operator "

However, starting from Python 3.5/NumPy 1.10, native matrix multiplication is supported with the new operator "@". So that is one more good reason to use ndarray ( https://docs.python.org/3/whatsnew/3.5.html#whatsnew-pep-465 ).

However, matrix objects still provide convenient conversion such as inverse and conjugate transpose while an ndarraydoes not. Let's start by creating NumPy matrices:

In [1]: import numpy as np 
In [2]: ndArray = np.arange(9).reshape(3,3) 
...

Linear algebra in NumPy


Before we get into linear algebra class in NumPy, there are five vector products we will cover at the beginning of this section. Let's review them one by one, starting with the numpy.dot() product:

In [26]: x = np.array([[1, 2], [3, 4]]) 
In [27]: y = np.array([[10, 20], [30, 40]]) 
In [28]: np.dot(x, y) 
Out[28]: 
array([[ 70, 100], 
       [150, 220]]) 

The numpy.dot() function performs matrix multiplication, and the detailed calculation is shown here:

 numpy.vdot() handles multi-dimensional arrays differently than numpy.dot(). It does not perform a matrix product, but flattens input arguments to one-dimensional vectors first:

In [29]: np.vdot(x, y) 
Out[29]: 300 

The detailed calculation of numpy.vdot() is as follows:

The numpy.outer() function is the outer product of two vectors. It flattens the input arrays if they are not one-dimensional. Let's say that the flattened input vector A has shape (M, ) and the flattened input...

Decomposition


There are there decompositions provided by numpy.linalg and in this section, we will cover two that are the most commonly used: singular value decomposition (svd) and QR factorization. Let's start by computing the eigenvalues and eigenvectors first. Before we get started, if you are not familiar with eigenvalues and eigenvectors, you may review them at https://en.wikipedia.org/wiki/Eigenvalues_and_eigenvectors. Let's start:

In [41]: x = np.random.randint(0, 10, 9).reshape(3,3) 
In [42]: x 
Out[42]: 
array([[ 1,  5,  0] 
       [ 7,  4,  0] 
       [ 2,  9,  8]]) 
In [42]: w, v = np.linalg.eig(x) 
In [43]: w 
Out[43]: array([ 8.,  8.6033,  -3.6033]) 
In [44]: v 
Out[44]: 
array([[ 0.,  0.0384,  0.6834] 
       [ 0.,  0.0583, -0.6292] 
       [ 1.,  0.9976,  0.3702]] 
) 

In the previous example, first we created a 3 x 3 ndarray using numpy.random.randint () and we computed the eigenvalues and eigenvectors...

Polynomial mathematics


NumPy also provides methods to work with polynomials, and includes a package called numpy.polynomial to create, manipulate, and fit the polynomials. A common application of this would be interpolation and extrapolation. In this section, our focus is still on using ndarray with NumPy functions instead of using polynomial instances. (Don't worry, we will still show you the usage of the polynomial class.)

As we stated in the matrix class section, using ndarray with NumPy functions is preferred since ndarray can be accepted in any functions while matrix and polynomial objects need to be converted, especially when communicating to other programs. Both of them provided handy properties, but in most cases ndarray would be good enough.

In this section, we will cover how to calculate the coefficients based on a set of roots, and how to solve a polynomial equation, and finally we will evaluate integrals and derivatives. Let's start by calculating the coefficients of a polynomial...

Application - regression and curve fitting


Since we are talking about the application of linear algebra, our experience comes from real-world cases. Let's begin with linear regression. So, let's say we are curious about the relationship between the age of a person and his/her sleeping quality. We'll use the data available online from the Great British Sleep Survey 2012 (https://www.sleepio.com/2012report/).

There were 20,814 people who took the survey, in an age range from under 20 to over 60 years old, and they evaluated their sleeping quality by scores from 4 to 6.

In this practice, we will just use 100 as our total population and simulate the age and sleeping scores followed the same distribution as the survey results. We want to know whether their age grows, sleep quality (scores) increases or decreases? As you already know, this is a hidden linear regression practice. Once we drew the regression line of the age and sleeping scores, by looking at the slope of the line, the answer will...

Summary


In this chapter, we covered the matrix class and polynomial class for linear algebra. We looked at the advanced functions provided by both classes, and also saw the performance advantage given by ndarray when doing the basic transpose. Also we introduced the numpy.linalg class, which provides many functions to deal with linear or polynomial computations with ndarray.

We did lots of math in this chapter, but we also found out how to use NumPy to help us answer some real-world questions.

In next chapter, we will get to know Fourier transformation and its application within NumPy.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
NumPy Essentials
Published in: Apr 2016Publisher: ISBN-13: 9781784393670
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (3)

author image
Leo (Liang-Huan) Chin

Leo (Liang-Huan) Chin is a data engineer with more than 5 years of experience in the field of Python. He works for Gogoro smart scooter, Taiwan, where his job entails discovering new and interesting biking patterns . His previous work experience includes ESRI, California, USA, which focused on spatial-temporal data mining. He loves data, analytics, and the stories behind data and analytics. He received an MA degree of GIS in geography from State University of New York, Buffalo. When Leo isn't glued to a computer screen, he spends time on photography, traveling, and exploring some awesome restaurants across the world. You can reach Leo at http://chinleock.github.io/portfolio/.
Read more about Leo (Liang-Huan) Chin

author image
Tanmay Dutta

Tanmay Dutta is a seasoned programmer with expertise in programming languages such as Python, Erlang, C++, Haskell, and F#. He has extensive experience in developing numerical libraries and frameworks for investment banking businesses. He was also instrumental in the design and development of a risk framework in Python (pandas, NumPy, and Django) for a wealth fund in Singapore. Tanmay has a master's degree in financial engineering from Nanyang Technological University, Singapore, and a certification in computational finance from Tepper Business School, Carnegie Mellon University.
Read more about Tanmay Dutta

author image
Shane Holloway

http://shaneholloway.com/resume/
Read more about Shane Holloway