#machine-learning#mathematics#deep-learning#python

Introduction to Machine Learning: A Mathematical Perspective

Abstract: An in-depth exploration of the mathematical foundations underlying machine learning algorithms, including linear regression, gradient descent, and neural networks.

15 min read

Abstract

This article provides a comprehensive introduction to machine learning from a mathematical perspective. We explore fundamental concepts including linear regression, optimization through gradient descent, and the mathematical foundations of neural networks. Through rigorous mathematical notation and practical Python implementations, we demonstrate how these concepts work together to enable machines to learn from data.

1. Introduction

Machine learning has revolutionized the field of computer science, enabling computers to learn patterns from data without being explicitly programmed. At its core, machine learning relies on mathematical optimization and statistical inference.

1.1 What is Machine Learning?

Machine learning can be formally defined as:

f^:XY\hat{f}: \mathcal{X} \rightarrow \mathcal{Y}

Where f^\hat{f} is our learned function that maps inputs from space X\mathcal{X} to outputs in space Y\mathcal{Y}.

2. Linear Regression

2.1 The Model

In linear regression, we assume a linear relationship between features and the target variable:

y=β0+β1x1+β2x2+...+βnxn+ϵy = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + ... + \beta_n x_n + \epsilon

Or in matrix notation:

y=Xβ+ϵ\mathbf y = \mathbf X\beta + \epsilon

2.2 Cost Function

We use the Mean Squared Error (MSE) as our loss function:

J=12mi=1m(hiyi)2J = \frac{1}{2 m} \sum_{i=1}^m (h_i - y_i)^2

Where:

  • mm is the number of training examples
  • h(x)h(x) is our hypothesis function
  • yiy_i is the actual value

2.3 Python Implementation

import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

class CustomLinearRegression:
    """
    A custom implementation of Linear Regression using gradient descent.
    """
    def __init__(self, learning_rate=0.01, n_iterations=1000):
        self.learning_rate = learning_rate
        self.n_iterations = n_iterations
        self.weights = None
        self.bias = None
        self.losses = []
    
    def fit(self, X, y):
        """
        Fit the linear regression model to the training data.
        
        Parameters:
        -----------
        X : array-like, shape (n_samples, n_features)
            Training data
        y : array-like, shape (n_samples,)
            Target values
        """
        n_samples, n_features = X.shape
        
        # Initialize parameters
        self.weights = np.zeros(n_features)
        self.bias = 0
        
        # Gradient descent
        for i in range(self.n_iterations):
            # Forward pass
            y_pred = np.dot(X, self.weights) + self.bias
            
            # Compute loss
            loss = np.mean((y_pred - y) ** 2)
            self.losses.append(loss)
            
            # Backward pass (compute gradients)
            dw = (1 / n_samples) * np.dot(X.T, (y_pred - y))
            db = (1 / n_samples) * np.sum(y_pred - y)
            
            # Update parameters
            self.weights -= self.learning_rate * dw
            self.bias -= self.learning_rate * db
    
    def predict(self, X):
        """Make predictions on new data."""
        return np.dot(X, self.weights) + self.bias

# Example usage
if __name__ == "__main__":
    # Generate synthetic data
    np.random.seed(42)
    X = 2 * np.random.rand(100, 1)
    y = 4 + 3 * X + np.random.randn(100, 1)
    
    # Train model
    model = CustomLinearRegression(learning_rate=0.01, n_iterations=1000)
    model.fit(X, y.ravel())
    
    # Make predictions
    y_pred = model.predict(X)
    
    print(f"Learned weights: {model.weights}")
    print(f"Learned bias: {model.bias}")

3. Gradient Descent

Gradient descent is an optimization algorithm used to minimize the cost function. The update rule is:

βt+1=βtαJ(βt)\beta_{t+1} = \beta_t - \alpha \nabla J(\beta_t)

Where:

  • α\alpha is the learning rate
  • J(βt)\nabla J(\beta_t) is the gradient of the cost function

3.1 Types of Gradient Descent

Note: There are three main variants of gradient descent:

  • Batch Gradient Descent: Uses all training examples
  • Stochastic Gradient Descent (SGD): Uses one example at a time
  • Mini-batch Gradient Descent: Uses a small batch of examples

4. Neural Networks

4.1 Architecture

A simple neural network can be represented as a series of transformations:

z[1]=W[1]x+b[1]a[1]=σ(z[1])z[2]=W[2]a[1]+b[2]y^=σ(z[2])\begin{aligned} \mathbf z^{[1]} &= \mathbf W^{[1]}\mathbf x + \mathbf b^{[1]} \\ \mathbf a^{[1]} &= \sigma(\mathbf z^{[1]}) \\ \mathbf z^{[2]} &= \mathbf W^{[2]}\mathbf a^{[1]} + \mathbf b^{[2]} \\ \hat{\mathbf y} &= \sigma(\mathbf z^{[2]}) \end{aligned}

4.2 Training Process Flowchart

graph TD
    A[Initialize Weights] --> B[Forward Propagation]
    B --> C[Compute Loss]
    C --> D{Loss < Threshold?}
    D -->|No| E[Backward Propagation]
    E --> F[Update Weights]
    F --> B
    D -->|Yes| G[Return Trained Model]

4.3 Simple Neural Network Implementation

class NeuralNetwork {
  private weights: number[][];
  private biases: number[];
  private learningRate: number;

  constructor(inputSize: number, hiddenSize: number, outputSize: number, learningRate = 0.01) {
    this.learningRate = learningRate;
    
    // Initialize weights randomly
    this.weights = [
      this.randomMatrix(inputSize, hiddenSize),
      this.randomMatrix(hiddenSize, outputSize)
    ];
    
    // Initialize biases to zero
    this.biases = [
      new Array(hiddenSize).fill(0),
      new Array(outputSize).fill(0)
    ];
  }

  private randomMatrix(rows: number, cols: number): number[][] {
    return Array.from({ length: rows }, () =>
      Array.from({ length: cols }, () => Math.random() * 2 - 1)
    );
  }

  private sigmoid(x: number): number {
    return 1 / (1 + Math.exp(-x));
  }

  private sigmoidDerivative(x: number): number {
    const s = this.sigmoid(x);
    return s * (1 - s);
  }

  predict(input: number[]): number[] {
    // Forward propagation
    let activation = input;
    
    for (let i = 0; i < this.weights.length; i++) {
      const z = this.matMul(activation, this.weights[i]).map(
        (val, idx) => val + this.biases[i][idx]
      );
      activation = z.map(this.sigmoid);
    }
    
    return activation;
  }

  private matMul(a: number[], b: number[][]): number[] {
    return b[0].map((_, colIndex) =>
      a.reduce((sum, aVal, rowIndex) => sum + aVal * b[rowIndex][colIndex], 0)
    );
  }
}

5. Results and Analysis

5.1 Performance Metrics

For classification tasks, we use several metrics:

Accuracy:

Accuracy=TP+TNTP+TN+FP+FN\text{Accuracy} = \frac{TP + TN}{TP + TN + FP + FN}

Precision:

Precision=TPTP+FP\text{Precision} = \frac{TP}{TP + FP}

Recall:

Recall=TPTP+FN\text{Recall} = \frac{TP}{TP + FN}

F1 Score:

F1=2PrecisionRecallPrecision+RecallF_1 = 2 \cdot \frac{\text{Precision} \cdot \text{Recall}}{\text{Precision} + \text{Recall}}

Example: For a binary classifier with 90 true positives, 10 false positives, 5 false negatives, and 95 true negatives, we get:

  • Accuracy = 92.5%
  • Precision = 90%
  • Recall = 94.7%
  • F1 Score = 92.3%

6. Conclusion

In this article, we explored the mathematical foundations of machine learning, from simple linear regression to complex neural networks. Understanding these mathematical concepts is crucial for:

  1. Model Selection: Choosing the right algorithm for your problem
  2. Hyperparameter Tuning: Optimizing learning rates and architecture
  3. Debugging: Understanding why models fail or succeed
  4. Innovation: Developing new algorithms and techniques

The journey from mathematical theory to practical implementation bridges the gap between abstract concepts and real-world applications.

Warning: Always validate your models on held-out test data to avoid overfitting and ensure generalization to unseen examples.

References

  1. Bishop, C. M. (2006). Pattern Recognition and Machine Learning. Springer.
  2. Goodfellow, I., Bengio, Y., & Courville, A. (2016). Deep Learning. MIT Press.
  3. Murphy, K. P. (2012). Machine Learning: A Probabilistic Perspective. MIT Press.