Challenge: Sum of Squared Error Calculator

Implement a Python program to calculate the sum of squared error.

Statement

Complete the calculateSSE function that accepts arrays, A, b, and wA,\ \bold{b},\ \text{and}\ \bold{w} representing a linear system and computes the Sum of Squared Error for the respective system in given w\bold{w}.

Example

A=[123111]w=[11]b=[958] A = \begin{bmatrix} 1 & -2 \\ 3 & 1 \\ 1 & 1 \end{bmatrix} \bold w = \begin{bmatrix} 1\\1 \end{bmatrix} \bold b = \begin{bmatrix} 9\\5\\8 \end{bmatrix}

d=Aw^b=[123111][11][958]=[1016]dTd=[1016][1016]SSE(1,1)=137\begin{align*} \bold d = A\bold{\hat{w}} - \bold b&=\begin{bmatrix} 1 & -2 \\ 3 & 1 \\ 1 & 1 \end{bmatrix} \begin{bmatrix} 1\\1 \end{bmatrix} - \begin{bmatrix} 9\\5\\8 \end{bmatrix} =\begin{bmatrix} -10\\-1\\-6 \end{bmatrix} \\ \bold d^T\bold d&=\begin{bmatrix} -10 & -1 & -6 \end{bmatrix} \begin{bmatrix} -10\\-1\\-6 \end{bmatrix} \\ SSE(1,1) &= 137 \end{align*}

Sample inputs and outputs

The following table provides example inputs and corresponding outputs:

Input Output
A=[[1,2],[3,1],[1,1]]A = [[1 , -2],[3 , 1],[1 , 1]] w=[[1],[1]]\bold{w}=[[1],[1]] b=[[9],[5],[8]]\bold{b}=[[9],[5],[8]] 137
A=[[7,2,5],[9,7,3],[6,9,1]]A = [[7 , 2 , 5],[9,7,3],[6,9,1]] w=[[2],[2],[0]]\bold{w}=[[2],[-2],[0]] b=[[3],[3],[2]]\bold{b}=[[3],[3],[2]] 114

Try it yourself

The signature of the function is given below:

Get hands-on with 1200+ tech skills courses.