# Python Foundations-11: Advanced Linear Algebra Concepts for Machine Learning

Welcome back to our ongoing exploration of Python foundations for machine learning! In our previous articles on linear algebra for machine learning, we delved into fundamental concepts like vector operations, matrix manipulations, and eigenvalues. In this installment, we will tackle more advanced linear algebra concepts to enhance your grasp of these crucial principles and further advance our journey into mastering linear algebra for machine learning. So, let’s embark on the next phase of Python Foundations for Machine Learning.

## 1. Orthogonality and Projections:

Orthogonality refers to the mathematical concept of perpendicularity between two vectors. In linear algebra, two vectors are considered orthogonal if their dot product equals zero, indicating that the vectors meet at a right angle. This concept is fundamental in various mathematical and engineering applications.

Projections, on the other hand, involve the process of mapping one vector onto another, often in the context of linear algebra and geometry. In the context of orthogonality, projecting a vector onto another means finding the component of the first vector that lies along the direction of the second vector. This projection provides a way to understand how much of one vector aligns with another, offering insights into optimising data representation and dimensionality reduction in machine learning and other mathematical applications.

Explore the significance of orthogonal vectors and their role in projections. Understand how to project a vector onto another using Python and NumPy. This will help to get clarity on using such advanced linear algebra concepts for machine learning.

# Python code for vector projection
import numpy as np

def vector_projection(v, u):
return np.dot(v, u) / np.linalg.norm(u) * (u / np.linalg.norm(u))

# Example usage
v = np.array([3, 4])
u = np.array([1, 2])
projection = vector_projection(v, u)
print("Vector Projection:", projection)

## 2. Singular Value Decomposition (SVD) Applications:

Singular Value Decomposition (SVD) is a method in linear algebra that breaks down a matrix into three others, revealing the matrix’s inherent structure. This decomposition finds applications in various fields, particularly in machine learning and data analysis. One notable use is in compressing images, where SVD reduces the dimensionality of the image matrix, making storage and transmission more efficient.

SVD is also crucial in Principal Component Analysis (PCA), a technique for reducing dimensionality and extracting features. In recommendation systems, SVD helps with collaborative filtering by revealing latent factors in user-item matrices, improving recommendation accuracy. Overall, SVD is a versatile tool for extracting meaningful patterns and insights from complex datasets across different domains.

Explore practical applications of SVD, such as image compression. Demonstrate how to compress and reconstruct an image using SVD.

# Python code for image compression using SVD
import numpy as np
import matplotlib.pyplot as plt

# Perform SVD
U, S, V = np.linalg.svd(image, full_matrices=False)

# Select top k singular values for compression
k = 50
compressed_image = np.dot(U[:, :k], np.dot(np.diag(S[:k]), V[:k, :]))

# Display original and compressed images
plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('Original Image')

plt.subplot(1, 2, 2)
plt.imshow(compressed_image, cmap='gray')
plt.title(f'Compressed Image (k={k})')

plt.show()

## 3. Eigenfaces in Facial Recognition:

Eigenfaces in facial recognition use eigenvalues and eigenvectors from linear algebra to represent and identify human faces. This method treats facial images as vectors in a high-dimensional space and applies Principal Component Analysis (PCA) to extract the main components, known as eigenfaces, from a set of training images. These eigenfaces capture the essential features of the faces in the dataset. During recognition, a new face is compared to known faces by projecting it onto this eigenface space.

Eigenfaces enable efficient dimensionality reduction and serve as a foundation for representing facial features, playing a crucial role in facial recognition systems widely used in computer vision and biometrics.

Apply eigenfaces for facial recognition. Use a dataset of facial images, perform PCA, and recognize faces using the eigenfaces approach.

# Python code for eigenfaces in facial recognition
from sklearn.decomposition import PCA
from sklearn.datasets import fetch_olivetti_faces
import matplotlib.pyplot as plt

faces_data = fetch_olivetti_faces(shuffle=True, random_state=42)

# Apply PCA
pca = PCA(n_components=25)
faces_pca = pca.fit_transform(faces_data.data)

# Display original and reconstructed faces
fig, axes = plt.subplots(2, 5, figsize=(10, 4),
subplot_kw={'xticks':[], 'yticks':[]},
gridspec_kw=dict(hspace=0.1, wspace=0.1))
for i in range(5):
axes[0, i].imshow(faces_data.data[i].reshape(64, 64), cmap='gray')
axes[1, i].imshow(pca.inverse_transform(faces_pca[i]).reshape(64, 64), cmap='gray')

plt.show()

## 4. Linear Regression using Matrix Form:

Linear Regression using Matrix Form is a method to find the best-fit line for a set of data points. It simplifies the process by using matrices instead of dealing with individual data points. The optimal parameters for the line are directly calculated using the normal equation. In simple terms, it’s a matrix-based approach to determine the best-fit line for given data.

Revisit linear regression using the matrix form. Implement linear regression using matrices for both simple and multiple linear regression.

# Python code for linear regression using matrices
import numpy as np

# Generate random data
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)

# Add a bias term to X
X_b = np.c_[np.ones((100, 1)), X]

# Compute the optimal parameters using the normal equation
theta_best = np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)

print("Optimal Parameters:", theta_best)

## 5. Kernel Trick in Support Vector Machines:

Kernel Trick in Support Vector Machines (SVM) is a technique that helps SVMs handle complex, nonlinear patterns in data. Instead of directly working in the original feature space, the kernel trick maps the data into a higher-dimensional space. This mapping allows SVMs to find a linear decision boundary in the transformed space, even when dealing with non-linear relationships in the original data. In simpler terms, the kernel trick is a smart way for SVMs to effectively handle and classify data that may not be easily separable in its original form.

Introduce the kernel trick in SVM for nonlinear classification problems. Implement SVM with a radial basis function (RBF) kernel using scikit-learn.

# Python code for SVM with RBF kernel
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score

X, y = iris.data, iris.target

# Split data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Apply SVM with RBF kernel
svm_rbf = SVC(kernel='rbf', C=1)
svm_rbf.fit(X_train, y_train)

# Predict and evaluate accuracy
y_pred = svm_rbf.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)

print("Accuracy:", accuracy)

## Conclusion:

By exploring these advanced linear algebra concepts and their applications in machine learning, we’ve taken another significant step in enhancing our Python Foundations for Machine Learning. These concepts not only deepen our understanding of linear algebra but also empower us to address complex machine learning challenges. Stay tuned for more insights in the next installment of our series!