# Python Foundations-10: Mastering Linear Algebra for Machine Learning

Welcome back to the continuation of our exploration of Linear Algebra for Machine Learning! In the previous articles, we covered fundamental concepts such as vector addition/subtraction, matrix multiplication, matrix transpose, eigenvalues, singular value decomposition, linear equations and so on. Now, let’s dive into a set of new problems to reinforce and expand our understanding of these crucial concepts with a focus on mastering linear algebra for machine learning .Let’s continue the journey of Python Foundations for Machine Learning!

## Problem 1: Matrix Inversion for mastering linear algebra for machine learning

Concept: Matrix inversion is a powerful operation in linear algebra. Given a square matrix A, the inverse, denoted as A⁻¹, exists if and only if A is non-singular (i.e., has a non-zero determinant). The product of a matrix and its inverse is the identity matrix.

Problem: Write a Python function to find the inverse of a 3×3 matrix.

Solution:

import numpy as np

def matrix_inverse(matrix):
if np.linalg.det(matrix) != 0:
return np.linalg.inv(matrix)
else:
raise ValueError("Matrix is singular, and the inverse does not exist.")

# Example usage:
A = np.array([[2, 1, 3],
[1, 0, 2],
[3, 2, 1]])

try:
A_inv = matrix_inverse(A)
print("Inverse of A:")
print(A_inv)
except ValueError as e:
print(e)

## Problem 2: Solving Linear Systems for mastering linear algebra for machine learning

Concept: Linear equations can be expressed in matrix form as Ax = B, where A is the coefficient matrix, x is the variable vector, and B is the constant vector. Solving for x involves finding the values that satisfy the equation.

Problem: Create a Python function to solve a system of linear equations using matrix operations.

Solution:

def solve_linear_system(A, B):
return np.linalg.solve(A, B)

# Example usage:
A = np.array([[2, -1, 3],
[1, 2, 1],
[4, 5, 2]])

B = np.array([8, 7, 10])

solution = solve_linear_system(A, B)
print("Solution to the linear system:")
print(solution)

## Problem 3: Eigenvalue Decomposition

Concept: Eigenvalue decomposition involves expressing a square matrix A as the product of its eigenvectors and eigenvalues. A = VΛV⁻¹, where V is the matrix of eigenvectors and Λ is the diagonal matrix of eigenvalues.

Problem: Implement a Python function to perform eigenvalue decomposition for a given matrix.

Solution:

def eigenvalue_decomposition(matrix):
eigenvalues, eigenvectors = np.linalg.eig(matrix)
return eigenvectors, np.diag(eigenvalues), np.linalg.inv(eigenvectors)

# Example usage:
B = np.array([[4, -2],
[1, 1]])

V, Lambda, V_inv = eigenvalue_decomposition(B)
print("Eigenvalue Decomposition:")
print("Eigenvectors:")
print(V)
print("Eigenvalues:")
print(Lambda)
print("Inverse of Eigenvectors:")
print(V_inv)

## Problem 4: Singular Value Decomposition (SVD)

Concept: Singular Value Decomposition decomposes any matrix A into three other matrices U, Σ, and Vᵀ. A = UΣVᵀ, where U and V are orthogonal matrices, and Σ is a diagonal matrix of singular values.

Problem: Write a Python function to perform Singular Value Decomposition for a given matrix.

Solution:

def singular_value_decomposition(matrix):
U, Sigma, Vt = np.linalg.svd(matrix)
return U, np.diag(Sigma), Vt

# Example usage:
C = np.array([[1, 2],
[3, 4],
[5, 6]])

U, Sigma, Vt = singular_value_decomposition(C)
print("Singular Value Decomposition:")
print("U matrix:")
print(U)
print("Sigma matrix:")
print(Sigma)
print("V transpose matrix:")
print(Vt)

## Problem 5: PCA (Principal Component Analysis)

Concept: PCA is a technique that uses SVD to perform dimensionality reduction by projecting data onto a lower-dimensional subspace, capturing the most significant variations.

Problem: Apply PCA to reduce the dimensionality of a given dataset.

Solution:

def pca_reduction(data, k):
U, Sigma, Vt = singular_value_decomposition(data)
reduced_data = U[:, :k] @ Sigma[:k, :k] @ Vt[:k, :]
return reduced_data

# Example usage:
D = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])

k = 2  # Number of principal components to keep
reduced_D = pca_reduction(D, k)
print(f"Reduced Data with {k} principal components:")
print(reduced_D)

## Problem 6: Linear Regression with Linear Algebra

Concept: Linear regression, a fundamental machine learning algorithm, can be expressed using linear algebra. The solution involves finding the parameters of the regression model through matrix operations.

Problem: Implement a Python function to perform linear regression using linear algebra for a given dataset.

Solution:

def linear_regression(X, y):
# Adding a column of ones for the intercept term
X_ = np.c_[np.ones(X.shape[0]), X]

# Compute the coefficients using the normal equation
coefficients = np.linalg.inv(X_.T @ X_) @ X_.T @ y

return coefficients

# Example usage:
# Assume X is a matrix of features and y is a column vector of target values
X = np.array([[1, 2],
[3, 4],
[5, 6]])
y = np.array([3, 7, 11])

coefficients = linear_regression(X, y)
print("Linear Regression Coefficients:")
print(coefficients)

These additional problems provide a well-rounded exploration of linear algebra concepts in the context of machine learning. Hope you enjoyed mastering linear algebra for machine learning. As you tackle more problems and apply these techniques to real-world scenarios, your proficiency in linear algebra will continue to grow. Happy coding!