numerical analysis with python
n Linear least squares problem Ax≅b, we seek a solution x such that the residual
‖b−Ax‖22
is minimized. A regularization term can be included to give preference to a solution x with a desired property. This regularization is minimizing,
‖b−Ax‖22+α‖x‖22.
This is equivalent to the augmented least squares problem [Aα‾‾√I]x≅[b0]
You are given a matrix A, vector b and scalar α. Calculate x such that it minimizes the residual for Ax≅b and x2 such that it minimizes the regularization condition. Also calculate the residual and norm for both.
code below
import numpy as np
import scipy.sparse
INPUT:
A:numpyarray of shape(m, n)b:numpyarray of shape(m,)alpha: Python float
OUTPUT:
x:numpyarray of shape(n,)that minimizes the residual.x2:numpyarray of shape(n,)that minimizes the L2-regularization condition.norm_x: Norm ofx.norm_x2: Norm ofx2.residual_x: Norm of the residual ofx.residual_x2: Norm of the residual ofx2.
n = np.random.randint(100, 200)
A = scipy.sparse.diags([-1,2,-1],[-1,0,1],shape=(n, n//4)).A
b = np.ones(n)
alpha = 1.5
8 years ago 5
Answer(1)
Purchase the answer to view it
NOT RATED
- matrixAvectorbandscalar..docx
other Questions(10)
- Building a Training Program
- HOMEWORK
- Discussion 6
- Dealing with Stress and Violence in the Workplace
- answer questions about a play
- answer the Question on the attachment?
- APA format, Systems mitigate risk and assist in organizational decision making
- Psychology slides
- Statistics Homework
- research paper and research outline