Skip to content

Step 2: Essential Math for AI

Step 2: Essential Math for AI

You don’t need a PhD in math, but you must understand Linear Algebra (how data is represented) and Calculus (how models learn).


πŸ—οΈ Linear Algebra: Vectors and Matrices

In AI, everything is a number. An image is a matrix of pixel values. A sentence is a vector of embeddings.

import numpy as np

# A 2x3 Matrix (like 2 data points with 3 features each)
X = np.array([
    [1, 2, 3],
    [4, 5, 6]
])

# Transpose the matrix
print(X.T)

# Matrix Multiplication (Dot Product)
# Critical for how Neural Networks process inputs
w = np.array([0.1, 0.2, 0.3])
output = np.dot(X, w)
print(f"Model Output: {output}")

πŸ“ˆ Calculus: Gradients

Neural networks learn by calculating the Derivative (slope) of an error function and moving in the opposite direction to minimize error. This is called Gradient Descent.

# Conceptual Gradient Descent in Python
def gradient_descent(x, learning_rate, steps):
    # f(x) = x^2 (The error function)
    # df/dx = 2x (The derivative/gradient)
    for i in range(steps):
        gradient = 2 * x
        x = x - (learning_rate * gradient)
        print(f"Step {i}: x = {x:.4f}, f(x) = {x**2:.4f}")

gradient_descent(x=10, learning_rate=0.1, steps=5)

πŸ₯… Your Goal

  • Understand that Dot Product is just weighted summing.
  • Understand that Gradient tells you which way to change weights to reduce error.