{%hackmd 5xqeIJ7VRCGBfLtfMi0_IQ %} # Basic NumPy ## Problem Given a matrix $A$ and a vector $\bb$, use NumPy to calculate the projection $A(A\trans A)^{-1}A\trans\bb$ and the solution $(A\trans A)^{-1}A\trans\bb$ of the least squares problem. ## Thought NumPy is a Python package for handling arrays. You may use [Colab](https://colab.research.google.com/) or [Sage Cell](https://sagecell.sagemath.org/) to run Python code. First, we have to import the package so that Python knows that you are going to use functions in the package. ```python= import numpy as np ``` Here `np` is a shorthand for `numpy` , which can be any name, but we follow the convention from the [NumPy community](https://numpy.org/doc/stable/index.html). Next, we have to construct an array (or a matrix). ```python= A = np.array([ [-1, -1], [1, 0], [0, 1] ]) ``` Here `A` is the name of your matrix, which can be a different name. In Python, a structure like `[1, 2, 3]` is called a `list` . Inside `np.array(...)` , we see the structure of `[ [...], ... [...] ]` , which is a list of lists. This is because a matrix can be viewed as a list of rows, and each row can be viewed as a list. Finally, we introduce some functions. 1. matrix addision: `A + B` 2. matrix multiplication: `A.dot(B)` 3. transpose: `A.T` 4. inverse: `np.linalg.inv(A)` ## Sample answer Run the SageMath code below or simply click [here](https://sagecell.sagemath.org/?q=okfbxa). ```python= import numpy as np A = np.array([ [-1, -1], [1, 0], [0, 1] ]) b = np.array([1,0,0]) ATAinv = np.linalg.inv(A.T.dot(A)) print("projection") print(A.dot(ATAinv).dot(A.T).dot(b)) print("least square problem") print(ATAinv.dot(A.T).dot(b)) ``` *This note can be found at Course website > Learning resources.*