<jupyter_start><jupyter_text>$A \cdot r \sim b$$A \cdot (b - Ar) = 0$$A^{T}(b - Ar) = 0$$A^{T}Ar = A^{T}b$$r = (A^{T}A)^{-1}A^{T}b$<jupyter_code>lr_algebra <- function(A, b) { solve(t(A) %*% A) %*% t(A) %*% b }<jupyter_output><empty_output><jupyter_text>$Cost = (b - Ar)^2 + \lambda r^2$$\frac{\partial}{\partial r}Cost = -2A \cdot (b- Ar) + 2 \lambda r $$ Ab = (A^2 + \lambda)r$$r = (A^TA+\lambda I)^{-1} \cdot A^Tb$<jupyter_code>l2_algebra <- function(A, b, lambda = 0.5) {
ATA <- t(A) %*% A
lambda_I <- lambda*diag(dim(ATA)[1])
solve(ATA + lambda_I) %*% t(A) %*% b
}
iris_mat <- as.matrix(iris[,1:4])
lr_algebra(iris_mat[, 1:3], iris_mat[, 4])
l2_algebra(iris_mat[, 1:3], iris_mat[, 4], 0.5)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2776243093922652, 'fraction_numerical': 0.03314917127071823, 'mean_word_length': 4.991735537190083, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 9, 'https://': 0, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '422587', 'license': 'Apache-2.0', 'n_tokens_mistral': 384, 'n_tokens_neox': 356, 'n_words': 63, 'path': 'Ridge_Regression.ipynb', 'repo_name': 'WOuOK-93/IBM-DataScience-Capstone', 'size': '724'}