Hello! Welcome to the first lesson of our module on Calculus for Machine Learning.
In the previous module, we established how linear algebra provides the language for representing data and transformations in machine learning. We ended by using NumPy to calculate eigenvalues and eigenvectors, the essential vectors that define the behavior of linear transformations.
Now, we shift our focus from representing data to learning from it. At its heart, machine learning is a process of optimization: finding the best model parameters to minimize error or maximize reward. To navigate the complex "landscape" of a model's error function and find its lowest point, we need the tools of calculus.
This lesson kicks off our journey into calculus. Our goal is to compute derivatives of single-variable functions. We'll cover both the "why"—the conceptual and geometric meaning of a derivative—and the "how"—the rules and Python tools for calculating them.
1. The "Why": What is a Derivative?
Before we start calculating, let's build an intuition for what a derivative is and why it's so fundamental to machine learning.
The Core Idea: Instantaneous Rate of Change
Imagine your machine learning model has a single parameter (let's call it ) and a loss function that measures how poorly the model is performing. To improve the model, you need to adjust . But in which direction should you adjust it, and by how much? You want to move in the direction where the loss decreases most rapidly.
The derivative gives us this information. It tells us the instantaneous rate of change of a function at a specific point. Geometrically, this is the slope of the line tangent to the function's curve at that point.

To understand this better, let's watch a short video that introduces the derivative as the slope of a tangent line on a curve.
Intro to Derivatives | Calculus for Machine Learning
This video from Kylie Ying's 'Calculus for Machine Learning' series provides a great visual introduction to the concept of a derivative.
Please watch the segment from 08:44 to 10:49. It explains how the slope, which is constant for a straight line, changes at every point on a curve. The derivative is the function that describes this changing slope.
The Formal Definition: The Limit
So, how do we calculate this "slope at a single point"? We can approximate it by taking two points on the curve that are very, very close together and calculating the slope of the line connecting them (a secant line). The derivative is what we get when we take the limit of this slope as the distance between the two points approaches zero.
This gives us the formal definition of the derivative of a function , denoted as or :
Let's break this down:
- is a very small change in the input .
- is the resulting change in the function's output (the "rise").
- The denominator, , is the change in the input (the "run").
- The part means we are looking at what happens to this ratio as the change becomes infinitesimally small.
The following video provides a clear explanation of this formula and its components.
Intro to Derivatives | Calculus for Machine Learning
Let's return to the Kylie Ying video to see a breakdown of the formal limit definition of a derivative.
Watch the segment from 10:49 to 16:51. The video connects the formula back to the idea of taking a tiny step 'h' and measuring the resulting change in the function.
Given your background in computer science, you might appreciate a computational perspective on this. The text below demonstrates this limit process with Python code, showing how the calculated ratio gets closer and closer to the true derivative as the step size (epsilon, or our ) gets smaller.
22.3. Single Variable Calculus
The following text from 'Dive into Deep Learning' frames the derivative in the context of neural network loss functions and demonstrates the limit definition with a code example.
Read the section '22.3.1. Differential Calculus'. Focus on how the idea of a function's behavior in a tiny range leads to the limit definition. Pay special attention to the Python code block that calculates (L(4 + epsilon) - L(4)) / epsilon for decreasing values of epsilon.
2. The "How": Rules for Differentiation
Using the limit definition every time is time-consuming. Fortunately, mathematicians have developed a set of rules that make finding derivatives much faster. For today's lesson, we'll focus on the rules needed to differentiate any polynomial function.
These are the essential rules:
-
The Constant Rule: The derivative of a constant is zero.
- If , then .
- This makes sense: a constant function is a horizontal line, and its slope is always zero.
-
The Power Rule: To find the derivative of , you bring the exponent down as a multiplier and then subtract one from the exponent.
- If , then .
- This is the most important rule for polynomials. For example, the derivative of is .
-
The Constant Multiple Rule: The derivative of a constant multiplied by a function is the constant times the derivative of the function.
- If , then .
- Example: The derivative of is .
-
The Sum/Difference Rule: The derivative of a sum (or difference) of functions is the sum (or difference) of their derivatives.
- If , then .
- This allows us to differentiate a polynomial term by term.
Let's watch a video that demonstrates these rules with clear examples.
The Organic Chemistry Tutor channel provides excellent, example-driven tutorials. This video will walk you through applying the basic differentiation rules to find the derivative of polynomials.
Watch the following segments: What is a Derivative & Constant Rule: (0:01 - 01:10) Power Rule: (01:10 - 03:41) Constant Multiple Rule: (03:41 - 05:45, you can stop when he begins the next example) Derivatives of Polynomials: (20:23 - 23:25) This will give you a solid foundation for applying these rules together.
Test your understanding!
Using the rules you just learned, find the derivative of the following function:
Show answer
Let's apply the rules term by term:
- Derivative of is .
- Derivative of is .
- Derivative of (which is ) is .
- Derivative of (a constant) is .
Combining them, we get:
3. Extending the Power Rule
The power rule is even more versatile than it first appears. It also works for negative and fractional exponents, which allows us to easily differentiate rational functions (like ) and radical functions (like ).
The key is to first rewrite the function in the form :
- can be rewritten as .
- can be rewritten as .
Once rewritten, you can apply the power rule just as before. Let's see this in action.
We'll return to the Organic Chemistry Tutor video to see how to apply the power rule to negative and fractional exponents.
Watch the segments on: Rational Functions (Negative Exponents): (26:29 - 29:00, stop before the 8/x^4 example) Radical Functions (Fractional Exponents): (30:47 - 33:20, stop before the cube root example) Focus on the strategy of rewriting the function first, then applying the power rule.
Test your understanding!
Find the derivative of the function:
Hint: First, rewrite each term using exponents.
Show answer
First, rewrite the function:
Now, apply the power rule to each term:
- Derivative of is .
- Derivative of is .
So, the derivative is:
Which can also be written as:
4. Practical Application: Derivatives in Python
While manual calculation is crucial for understanding, in practice you'll use libraries to compute derivatives. For symbolic computation (manipulating mathematical expressions directly), Python's sympy library is the perfect tool.
The image below shows how to use sympy to define a function and find its derivative automatically.

This workflow is incredibly powerful. You can define complex functions and get their exact derivatives without manual calculation, which is essential as we move towards more complex models in machine learning.
Conclusion
In this lesson, we laid the foundational groundwork for calculus in machine learning. You've learned not just how to compute derivatives, but also what they represent.
Key Takeaways:
- A derivative measures the instantaneous rate of change of a function, which is geometrically represented as the slope of the tangent line.
- The formal limit definition, , is the principle from which all differentiation is derived.
- We can efficiently compute derivatives using a set of rules, most notably the power rule, constant multiple rule, and sum rule.
- The power rule applies to integer, negative, and fractional exponents, making it widely applicable.
- In Python, the
sympylibrary is the standard tool for performing symbolic differentiation.
Preview of the Next Lesson:
We've now mastered finding the derivative of functions with a single input variable, like . However, a real machine learning loss function depends on many parameters (weights and biases), making it a function of thousands or even millions of variables. How do we find the "slope" in such a high-dimensional space?
To answer this, we need to extend our concept of the derivative. In the next lesson, we will learn how to calculate partial derivatives of multivariable functions, which allows us to see how the function changes when we alter just one variable at a time.