1.6: Basic Math for ML
Learn Linear Algebra basics, including vectors and matrices, along with Probability and Statistics concepts like mean, median, and standard deviation, essential for Machine Learning.
1.6: Basic Math for Machine Learning
Welcome to this chapter in the AI Zero to Mastery series! In this lesson, we’ll explore foundational math concepts essential for machine learning: Linear Algebra, Probability, and Statistics. Understanding these will strengthen your ability to handle datasets and model computations effectively.
Linear Algebra Basics
Linear Algebra is the study of vectors, matrices, and their transformations. These concepts are at the core of most ML algorithms, where data manipulation and optimization play crucial roles.
1. What is a Vector?
Definition
A vector is an ordered list of numbers that represents a direction and magnitude in space.
Example: The vector [3, 4]
means "move 3 steps right and 4 steps up."
Real-Life Analogy
Imagine you’re using Google Maps to navigate to a location. The instruction "Go 3 km north and 4 km east" can be represented as a vector [3, 4]
.
Key Operations on Vectors
-
Addition:
Combine vectors by adding corresponding components:[3, 4] + [1, -2] = [4, 2]
-
Dot Product:
Measures similarity between two vectors. Formula:[a, b] · [c, d] = (a × c) + (b × d)
Example:
[3, 4] · [1, 2] = (3 × 1) + (4 × 2) = 11
2. What is a Matrix?
Definition
A matrix is a grid of numbers arranged into rows and columns.
Example:
[1, 2, 3]
[4, 5, 6]
Real-Life Analogy
Think of a spreadsheet where each row is a user, and each column is a property (e.g., "Age" or "Score"). The entire spreadsheet is the matrix.
Key Operations with Matrices
-
Matrix Multiplication:
Combines data from two matrices.
Example: Weight matrices in neural networks adjust input features during training. -
Transpose:
Flips rows into columns:Original Matrix: [1, 2] [3, 4] Transpose: [1, 3] [2, 4]
Probability and Statistics
Probability and Statistics help us interpret and make sense of data, enabling ML models to generalize and make predictions.
Key Concepts in Statistics
1. Mean (Average)
The mean is the sum of all values divided by the number of values.
Formula:
Mean = (Sum of all values) / (Number of values)
Example: Test scores [80, 90, 100]:
Mean = (80 + 90 + 100) / 3 = 90
2. Median
The median is the middle value in a sorted dataset.
Example:
Dataset: [10, 20, 30]
Median = 20
Dataset: [10, 20, 30, 40]
Median = (20 + 30) / 2 = 25
3. Standard Deviation (SD)
SD measures how spread out data is from the mean.
Formula:
SD = sqrt((∑(x_i - mean)^2) / n)
Example: For [10, 20, 30]:
Mean = 20
Variance = ((10-20)^2 + (20-20)^2 + (30-20)^2) / 3 = 66.67
SD = sqrt(66.67) ≈ 8.16
Applications in ML
-
Probability:
Used in classification problems. Example: Predicting whether an email is spam using Bayes’ Theorem. -
Statistics:
- Mean: Used in data preprocessing (e.g., filling missing values).
- Median: Handles outliers in skewed datasets.
- Standard Deviation: Scales features for uniformity.
Practice Problems
Linear Algebra
- Add the vectors:
[2, 3] + [5, -1]
- Multiply the matrices:
[1, 2] [3, 4] × [5, 6] [7, 8]
Statistics
- Calculate the mean, median, and standard deviation for the dataset:
[4, 8, 6, 10, 2]
- If the probability of raining today is
70%
, what is the probability it won’t rain?
Solutions
Linear Algebra
Problem 1: Add the vectors
[2, 3] + [5, -1] = [7, 2]
Problem 2: Multiply the matrices
[1, 2] [5, 6] [19, 22]
[3, 4] × [7, 8] = [43, 50]
Statistics
Problem 1: Calculate Mean, Median, and SD
- Mean: 6
- Median: 6
- Standard Deviation: 2.83
Problem 2: Probability of no rain
1 - 0.7 = 0.3 (30%)
Conclusion
Linear Algebra and Probability & Statistics are the foundation of Machine Learning. Master these concepts to handle datasets, understand models, and tackle real-world ML problems effectively.
Next Lesson: Dive deeper into basic math for ML with more real-world applications!