... | @@ -231,9 +231,18 @@ Problem](https://www.cs.odu.edu/~tkennedy/cs417/s21/Public/approximationExample2 |
... | @@ -231,9 +231,18 @@ Problem](https://www.cs.odu.edu/~tkennedy/cs417/s21/Public/approximationExample2 |
|
|
|
|
|
The code snippets in this section are extracted from [least_squares.py](https://git-community.cs.odu.edu/tkennedy/python-workshop/-/blob/master/NumPy/least_squares.py).
|
|
The code snippets in this section are extracted from [least_squares.py](https://git-community.cs.odu.edu/tkennedy/python-workshop/-/blob/master/NumPy/least_squares.py).
|
|
|
|
|
|
|
|
The first line is the NumPy import. By convention the `numpy` module is given
|
|
|
|
the alias `np`.
|
|
|
|
|
|
```python
|
|
```python
|
|
import numpy as np
|
|
import numpy as np
|
|
|
|
```
|
|
|
|
|
|
|
|
The first function (i.e., `print_matrices`) was debugging/instrumentation
|
|
|
|
logic. It was used to display the `XTX` (X-Transpose-X) and `XTY`
|
|
|
|
(X-Transpose-Y) matrices during development.
|
|
|
|
|
|
|
|
```python
|
|
def print_matrices(matrix_XTX, matrix_XTY):
|
|
def print_matrices(matrix_XTX, matrix_XTY):
|
|
"""
|
|
"""
|
|
Print the XTX and XTY matrices
|
|
Print the XTX and XTY matrices
|
... | @@ -245,8 +254,50 @@ def print_matrices(matrix_XTX, matrix_XTY): |
... | @@ -245,8 +254,50 @@ def print_matrices(matrix_XTX, matrix_XTY): |
|
print()
|
|
print()
|
|
print("{:*^40}".format("XTY"))
|
|
print("{:*^40}".format("XTY"))
|
|
print(matrix_XTY)
|
|
print(matrix_XTY)
|
|
|
|
```
|
|
|
|
|
|
|
|
Next... let us jump to the `main` function and...
|
|
|
|
|
|
|
|
1. set up the input data (points)
|
|
|
|
|
|
|
|
```python
|
|
|
|
points = [(0., 0.), (1., 1.), (2., 4.)]
|
|
|
|
```
|
|
|
|
|
|
|
|
2. create the `X` matrix
|
|
|
|
```python
|
|
|
|
matrix_X = np.array([[1., 0., 0.],
|
|
|
|
[1., 1., 1.],
|
|
|
|
[1., 2., 4.]])
|
|
|
|
```
|
|
|
|
|
|
|
|
3. create the `Y` matrix
|
|
|
|
|
|
|
|
```python
|
|
|
|
matrix_Y = np.array([0,
|
|
|
|
1,
|
|
|
|
4])
|
|
|
|
```
|
|
|
|
|
|
|
|
4. compute X-transpose
|
|
|
|
|
|
|
|
```python
|
|
|
|
matrix_XT = matrix_X.transpose()
|
|
|
|
```
|
|
|
|
|
|
|
|
After setting everything up... it is time for a couple matrix multiplications.
|
|
|
|
|
|
|
|
```python
|
|
|
|
matrix_XTX = np.matmul(matrix_XT, matrix_X)
|
|
|
|
matrix_XTY = np.matmul(matrix_XT, matrix_Y)
|
|
|
|
```
|
|
|
|
|
|
|
|
The augmented `XTX|XTY` matrix is what we are going to solve. *Note that the
|
|
|
|
`@` operator is often used instead of `np_matmul`.*
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
```python
|
|
def _backsolve(matrix_XTX, matrix_XTY):
|
|
def _backsolve(matrix_XTX, matrix_XTY):
|
|
|
|
|
|
num_rows, _ = matrix_XTX.shape
|
|
num_rows, _ = matrix_XTX.shape
|
... | | ... | |