# NumPy: Linear Algebra¶

Ha Khanh Nguyen (hknguyen)

## 1. Why Linear Algebra?¶

• Linear algebra, like matrix multiplication, decompositions, determinants, and other square matrix math, is an important part of any array library.
• Why is it so important?
• Multivariate statistics is built on linear algebra!
• Think Principal Component Analysis (PCA), Linear Regression, etc.

## 2. Matrix Multiplication & Transpose¶

• First, note that NumPy array by default operates using element-wise operation.
• That is multiplying 2 two-dimensional arrays with * is an element-wise product instead of a matrix dot product.
• To perform a matrix dot product, we use the dot function.
• x.dot(y) is equivalent to np.dot(x, y):
• The @ symbol (as of Python 3.5) also works as a matrix multiplication operator:
• With transpose, all NumPy objects have a transpose attribute named T.

## 3. Functions in numpy.linalg¶

• The sub-module linalg inside NumPy has a standard set of matrix decompositions and functions like finding inverse matrix and determinant.
• So mat is the dot product of X and the transpose of X.

## 4. Exercise¶

We are given the following matrices:

Evaluate the following expression using NumPy matrix functions:

$$(X^TX)^{-1}X^Ty$$

This lecture note is modified from Chapter 4 of Wes McKinney's Python for Data Analysis 2nd Ed.