<jupyter_start><jupyter_text>1 Matrix operations 1.1 Create a 4*4 identity matrix<jupyter_code>#This project is designed to get familiar with python list and linear algebra
#You cannot use import any library yourself, especially numpy

A = [[1,2,3], 
     [2,3,3], 
     [1,2,5]]

B = [[1,2,3,5], 
     [2,3,3,5], 
     [1,2,5,1]]

#TODO create a 4*4 identity matrix 
I = None<jupyter_output><empty_output><jupyter_text>1.2 get the width and height of a matrix.<jupyter_code>#TODO Get the height and weight of a matrix.
def shape(M):
    return 0,0
# run following code to test your shape function
%run -i -e test.py LinearRegressionTestCase.test_shape<jupyter_output><empty_output><jupyter_text>1.3 round all elements in M to certain decimal points<jupyter_code># TODO in-place operation, no return value
# TODO round all elements in M to decPts
def matxRound(M, decPts=4):
    pass
# run following code to test your matxRound function
%run -i -e test.py LinearRegressionTestCase.test_matxRound<jupyter_output><empty_output><jupyter_text>1.4 compute transpose of M<jupyter_code>#TODO compute transpose of M
def transpose(M):
    return None
# run following code to test your transpose function
%run -i -e test.py LinearRegressionTestCase.test_transpose<jupyter_output><empty_output><jupyter_text>1.5 compute AB. return None if the dimensions don't match<jupyter_code>#TODO compute matrix multiplication AB, return None if the dimensions don't match
def matxMultiply(A, B):
    return None
# run following code to test your matxMultiply function
%run -i -e test.py LinearRegressionTestCase.test_matxMultiply<jupyter_output><empty_output><jupyter_text>--- 2 Gaussian Jordan Elimination 2.1 Compute augmented Matrix $ A = \begin{bmatrix}    a_{11}    & a_{12} & ... & a_{1n}\\    a_{21}    & a_{22} & ... & a_{2n}\\    a_{31}    & a_{22} & ... & a_{3n}\\    ...    & ... & ... & ...\\    a_{n1}    & a_{n2} & ... & a_{nn}\\\end{bmatrix} , b = \begin{bmatrix}    b_{1}  \\    b_{2}  \\    b_{3}  \\    ...    \\    b_{n}  \\\end{bmatrix}$Return $ Ab = \begin{bmatrix}    a_{11}    & a_{12} & ... & a_{1n} & b_{1}\\    a_{21}    & a_{22} & ... & a_{2n} & b_{2}\\    a_{31}    & a_{22} & ... & a_{3n} & b_{3}\\    ...    & ... & ... & ...& ...\\    a_{n1}    & a_{n2} & ... & a_{nn} & b_{n} \end{bmatrix}$<jupyter_code>#TODO construct the augment matrix of matrix A and column vector b, assuming A and b have same number of rows
def augmentMatrix(A, b):
    return None
# run following code to test your augmentMatrix function
%run -i -e test.py LinearRegressionTestCase.test_augmentMatrix<jupyter_output><empty_output><jupyter_text>2.2 Basic row operations- exchange two rows- scale a row- add a scaled row to another<jupyter_code># TODO r1 <---> r2
# TODO in-place operation, no return value
def swapRows(M, r1, r2):
    pass
# run following code to test your swapRows function
%run -i -e test.py LinearRegressionTestCase.test_swapRows
# TODO r1 <--- r1 * scale
# TODO in-place operation, no return value
def scaleRow(M, r, scale):
    pass
# run following code to test your scaleRow function
%run -i -e test.py LinearRegressionTestCase.test_scaleRow
# TODO r1 <--- r1 + r2*scale
# TODO in-place operation, no return value
def addScaledRow(M, r1, r2, scale):
    pass
# run following code to test your addScaledRow function
%run -i -e test.py LinearRegressionTestCase.test_addScaledRow<jupyter_output><empty_output><jupyter_text>2.3  Gauss-jordan method to solve Ax = b Hint：Step 1: Check if A and b have same number of rowsStep 2: Construct augmented matrix AbStep 3: Column by column, transform Ab to reduced row echelon form [wiki link](https://en.wikipedia.org/wiki/Row_echelon_formReduced_row_echelon_form)        for every column of Ab (except the last one)        column c is the current column        Find in column c, at diagonal and under diagonal (row c ~ N) the maximum absolute value        If the maximum absolute value is 0            then A is singular, return None （Prove this proposition in Question 2.4）        else            Apply row operation 1, swap the row of maximum with the row of diagonal element (row c)            Apply row operation 2, scale the diagonal element of column c to 1            Apply row operation 3 mutiple time, eliminate every other element in column c            Step 4: return the last column of Ab Remark：We don't use the standard algorithm first transfering Ab to row echelon form and then to reduced row echelon form.  Instead, we arrives directly at reduced row echelon form. If you are familiar with the stardard way, try prove to yourself that they are equivalent.<jupyter_code>#TODO implement gaussian jordan method to solve Ax = b

""" Gauss-jordan method to solve x such that Ax = b.
        A: square matrix, list of lists
        b: column vector, list of lists
        decPts: degree of rounding, default value 4
        epsilon: threshold for zero, default value 1.0e-16
        
    return x such that Ax = b, list of lists 
    return None if A and b have same height
    return None if A is (almost) singular
"""

def gj_Solve(A, b, decPts=4, epsilon = 1.0e-16):
    return None
# run following code to test your addScaledRow function
%run -i -e test.py LinearRegressionTestCase.test_gj_Solve<jupyter_output><empty_output><jupyter_text>2.4 Prove the following proposition:**If square matrix A can be divided into four parts: ** $ A = \begin{bmatrix}    I    & X \\    Z    & Y \\\end{bmatrix} $, where I is the identity matrix, Z is all zero and the first column of Y is all zero, **then A is singular.**Hint: There are mutiple ways to prove this problem.  - consider the rank of Y and A- consider the determinate of Y and A - consider certain column is the linear combination of other columns TODO Please use latex （refering to the latex in problem may help）TODO Proof： --- 3 Linear Regression:  3.1 Compute the gradient of loss function with respect to parameters  (Choose one between two 3.1 questions)We define loss funtion $E$ as $$E(m, b) = \sum_{i=1}^{n}{(y_i - mx_i - b)^2}$$and we define vertex $Y$, matrix $X$ and vertex $h$ :$$Y =  \begin{bmatrix}    y_1 \\    y_2 \\    ... \\    y_n\end{bmatrix},X =  \begin{bmatrix}    x_1 & 1 \\    x_2 & 1\\    ... & ...\\    x_n & 1 \\\end{bmatrix},h =  \begin{bmatrix}    m \\    b \\\end{bmatrix}$$Proves that $$\frac{\partial E}{\partial m} = \sum_{i=1}^{n}{-2x_i(y_i - mx_i - b)}$$$$\frac{\partial E}{\partial b} = \sum_{i=1}^{n}{-2(y_i - mx_i - b)}$$$$\begin{bmatrix}    \frac{\partial E}{\partial m} \\    \frac{\partial E}{\partial b} \end{bmatrix} = 2X^TXh - 2X^TY$$ TODO Please use latex （refering to the latex in problem may help）TODO Proof：  3.1 Compute the gradient of loss function with respect to parameters  (Choose one between two 3.1 questions)We define loss funtion $E$ as $$E(m, b) = \sum_{i=1}^{n}{(y_i - mx_i - b)^2}$$and we define vertex $Y$, matrix $X$ and vertex $h$ :$$Y =  \begin{bmatrix}    y_1 \\    y_2 \\    ... \\    y_n\end{bmatrix},X =  \begin{bmatrix}    x_1 & 1 \\    x_2 & 1\\    ... & ...\\    x_n & 1 \\\end{bmatrix},h =  \begin{bmatrix}    m \\    b \\\end{bmatrix}$$Proves that $$E = Y^TY -2(Xh)^TY + (Xh)^TXh$$$$\frac{\partial E}{\partial h} = 2X^TXh - 2X^TY$$ TODO Please use latex （refering to the latex in problem may help）TODO Proof：  3.2  Linear Regression Solve equation $X^TXh = X^TY $ to compute the best parameter for linear regression.<jupyter_code>#TODO implement linear regression 
'''
points: list of (x,y) tuple
return m and b
'''
def linearRegression(points):
    return 0,0<jupyter_output><empty_output><jupyter_text>3.3 Test your linear regression implementation<jupyter_code>#TODO Construct the linear function

#TODO Construct points with gaussian noise
import random

#TODO Compute m and b and compare with ground truth<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11875239677872938, 'fraction_numerical': 0.020708168221909753, 'mean_word_length': 3.3636363636363638, 'pattern_counts': {'":': 0, '<': 44, '<?xml version=': 0, '>': 42, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '98363', 'license': 'MIT', 'n_tokens_mistral': 2649, 'n_tokens_neox': 2514, 'n_words': 951, 'path': '4.linear algebra/linear_regression_project_en.ipynb', 'repo_name': 'Wilson-Sunshine/AI-program-basic', 'size': '7823'}