Mathematics for Machine Learning: Rebuilding the Foundations from Scratch¶
Part 1: Algebra through Logarithms
Before we dive into machine learning, neural networks, or deep learning, we need to build a solid mathematical foundation.
When people first approach machine learning, they often jump straight into algorithms, libraries, and code. They learn how to train a model using Python, call a function from scikit-learn, or build a neural network with PyTorch. They might even get impressive results.
But there's a problem: using a model is not the same as understanding how it works.
To understand machine learning deeply, we need to understand the mathematics behind it. That means starting with the fundamentals, even if some of these concepts seem familiar.
In this article, we'll rebuild our mathematical foundation step by step, starting with algebra and building through exponents and logarithms.
1. Variables, constants, and coefficients¶
Let's begin with a simple expression:
What does this mean?
- \(x\) is a variable. Its value can change.
- \(y\) is another variable whose value depends on \(x\).
- \(3\) is a coefficient. It multiplies \(x\).
- \(2\) is a constant in this expression.
Suppose \(x=4\). We substitute 4 wherever we see \(x\):
Therefore, \(y=14\).
If \(x=5\), then:
Notice something interesting: when \(x\) increases from 4 to 5, \(y\) increases from 14 to 17.
The input increases by 1, but the output increases by 3.
Why? Because every increase of 1 in \(x\) adds another 3 to the expression \(3x+2\).
This simple relationship will become important when we study linear models in machine learning.
2. Expressions and equations are not the same thing¶
Consider these two mathematical statements:
The first is an expression. It represents a calculation, but it doesn't claim that the result equals a particular value.
The second is an equation. It states that the expression on the left equals 14.
This distinction matters because we use expressions to calculate values and equations to find unknown values.
For example, given:
We want to find \(x\).
First, subtract 2 from both sides:
Then divide both sides by 3:
The answer is \(x=4\).
The key principle is that an equation remains balanced when we perform the same valid operation on both sides.
Think of an equation as a balanced scale. If you remove the same weight from both sides, the scale remains balanced. If you divide both sides by the same nonzero number, equality is preserved.
This idea is fundamental to solving equations, rearranging formulas, and understanding many mathematical derivations.
3. The order of operations¶
Consider the expression:
Should we add first or multiply first?
According to the standard order of operations, multiplication comes before addition:
If we calculate from left to right without considering the rules, we might get:
That is a different expression because the parentheses change the order of calculation.
The usual order is:
- Parentheses and other grouping symbols.
- Exponents and roots.
- Multiplication and division, from left to right.
- Addition and subtraction, from left to right.
Let's try another example:
Calculate the exponent first:
Then multiplication:
The answer is 16.
Why does this matter for machine learning? Mathematical formulas can contain several operations, and evaluating them in the wrong order can produce incorrect results. Computers follow explicit rules, and so must we.
4. Negative numbers and fractions¶
Negative numbers and fractions are not separate mathematical worlds. They follow the same basic arithmetic rules, with a few details worth understanding carefully.
Negative numbers¶
When multiplying two numbers with the same sign, the result is positive:
When multiplying numbers with different signs, the result is negative:
The same sign rules apply to division.
These rules will matter later when we calculate changes in model parameters, work with slopes, and interpret derivatives.
Fractions¶
A fraction represents division:
To multiply fractions, multiply the numerators and denominators:
To divide by a fraction, multiply by its reciprocal:
A reciprocal is simply what we get when we swap the numerator and denominator of a nonzero fraction.
Fractions will become especially important when we study ratios, probabilities, averages, and the formulas used to calculate gradients.
5. Exponents: a compact way to express multiplication¶
Consider:
We can write this more compactly as:
The number 2 is the base, and 4 is the exponent. The exponent tells us how many times the base appears as a factor.
Let's explore the main exponent rules.
Rule 1: Multiplying powers with the same base¶
Why do we add the exponents?
Consider:
Expanding the powers gives:
There are five factors of \(x\), so:
We add the exponents because we're counting how many times the same base appears in the product.
Rule 2: Dividing powers with the same base¶
For a nonzero base:
For example:
We can see why by expanding the fraction:
Two factors cancel, leaving three factors of \(x\).
Rule 3: Raising a power to another power¶
For example:
This means multiplying \(x^2\) by itself three times:
We multiply the exponents because each of the three groups contains two factors of \(x\).
Rule 4: Zero exponents¶
For any nonzero \(x\):
Why?
Using the division rule:
But any nonzero number divided by itself equals 1. Therefore:
This rule applies to every nonzero base. The expression \(0^0\) requires separate treatment and is not covered by this rule.
Rule 5: Negative exponents¶
A negative exponent represents a reciprocal:
For example:
We can derive this using the division rule:
Canceling the common factors in the fraction gives:
Therefore:
A negative exponent does not mean the value itself must be negative. It tells us to take the reciprocal.
Rule 6: Fractional exponents¶
A fractional exponent can represent a root:
For example:
Similarly:
So:
Fractional exponents let us express roots and powers within the same mathematical framework.
Why exponents matter in machine learning¶
Exponent rules appear throughout machine learning and deep learning. We encounter them when working with exponential functions, probability distributions, loss functions, and numerical calculations.
For example, the exponential function \(e^x\) plays an important role in the sigmoid function:
We'll return to this expression when we study activation functions in neural networks.
6. Logarithms: answering the opposite question¶
Exponents ask us to calculate a power. Logarithms ask us to determine the exponent.
Suppose:
We know the base is 2, the exponent is 3, and the result is 8.
A logarithm reverses this relationship:
Read this as: "The logarithm of 8 to base 2 is 3."
The formal definition is:
In plain English, the logarithm tells us what exponent we need to raise the base to in order to obtain a particular number.
Example 1: Converting a logarithm into exponential form¶
Suppose:
We ask: to what power must we raise 3 to get 81?
Since:
We have:
Example 2: Converting exponential form into logarithmic form¶
Suppose:
The equivalent logarithmic expression is:
Both statements describe the same relationship.
Rule 1: The logarithm of a product¶
For positive \(M\) and \(N\), with a valid logarithm base:
For example:
Since \(\log_2(8)=3\) and \(\log_2(4)=2\):
The rule works because multiplying powers with the same base means adding their exponents.
Rule 2: The logarithm of a quotient¶
For example:
And indeed:
Rule 3: The logarithm of a power¶
For example:
This is correct because \(8^2=64\) and \(2^6=64\).
Two useful identities¶
For a valid base \(b>0\), \(b\neq1\):
This is because:
Also:
Because:
One important warning: logarithms do not distribute over addition.
In general:
For example:
But:
The two results differ.
What is the natural logarithm?¶
The natural logarithm uses the mathematical constant \(e\), approximately equal to 2.71828.
It is written as:
For example, if:
We convert to exponential form:
Therefore:
Natural logarithms appear in many machine learning formulas, including likelihood calculations, cross-entropy loss, and statistical models.
Back to the series overview.