Taking Derivatives in Python. Learn how to deal with Calculus part of... | by Dario Radečić | Towards Data Science

The idea behind this post is to revisit some calculus topics needed in data science and machine learning and to take them one step further — calculate them in Python. It’s really simple once you get the gist of it, and you don’t need to worry about memorizing differentiation rules.
This post will be based on a Python library called SymPy, and here’s a short introduction on it (if you haven’t used it before):
SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python.[1]
To install it (should come with Anaconda distribution) fire up the terminal window and execute the following:
pip install sympy
As I said, we’ll use it to revisit some topics from calculus, differential calculus to be more precise. If you are rusty in this area, let’s shortly revisit it:
Differential calculus, Branch of mathematical analysis, devised by Isaac Newton and G.W. Leibniz, and concerned with the problem of finding the rate of change of a function with respect to the variable on which it depends. Thus it involves calculating derivatives and using them to solve problems involving nonconstant rates of change. Typical applications include finding maximum and minimum values of functions in order to solve practical problems in optimization.[2]
And now to be even more specific (this part will be over soon, I promise), we’ll be calculating derivatives. I don’t remember what those are, either. Nothing fancy, actually:
The derivative of a function at some point characterizes the rate of change of the function at this point. We can estimate the rate of change by calculating the ratio of change of the function Δy to the change of the independent variable Δx. In the definition of derivative, this ratio is considered in the limit as Δx→0.[3]
Okay okay, stop torturing me with the theory already!
This is the last paragraph before diving into examples, honest to God! To start, we’ll dive into single variable functions, but later on, we will go through a couple of examples for multivariate functions, ergo we’ll cover how to calculate partial derivatives. This is something you’ll do more often, I haven’t used derivatives of single-variable functions much in the application part.
Without further ado, let’s get started!
This would be something covered in your Calc 1 class or online course, involving only functions that deal with single variables, for example, f(x). The goal is to go through some basic differentiation rules, go through them by hand, and then in Python. Let’s get started.
The power rule states this:
Which is pretty self-explanatory, if you listened to some calc class before. If you haven’t, let’s go through a simple example. Your function f(x) is equal to x to the fifth. Now use the power rule to calculate the derivative. It’s pretty straightforward:
Now let’s take a look at how to calculate it in Python. The first thing is to import the library, and then to declare a variable that you will use as a letter in your functions. Here’s how to do it for a single variable function:
Once those cells are executed, it becomes trivial to take the derivative (same function as above):
Pay attention to this beautiful print formatting — looks just like an equation written in LaTeX!
The product rule states that if f(x) and g(x) are two differentiable functions, then the derivative is calculated as the first function times the derivative of second plus the second times the derivative of first. That might have sounded confusing a bit when expressed with words, so here’s the notation:
Let’s calculate one example by hand. We have the following:
As you can see, x squared plus 1 would be f(x), and cosine of x would be g(x). And here’s how you’d do it in Python:
Also straightforward. Make sure to watch where you put those brackets though. Also, note that you can’t use cosine from math or numpy libraries, you need to use the one from sympy.
If you decide to dive deeper into machine learning algorithms you’ll see chain rule popping up everywhere — gradient descent, backpropagation, you name it. It deals with nested functions, for example, f(g(x)) and states that the derivative is calculated as the derivative of an outer function multiplied with the inner function, then all multiplied by the derivative of the inner function. Here’s the notation:
And here’s a simple example calculated by hand:
Python implementation is once again as simple as it can be:
Here, the same rules apply as when dealing with it’s utterly simple single variable brother — you still use the chain rule, power rule, etc, but you take derivatives with respect to one variable while keeping others constant. Oh, and those are called partial derivatives. Fancy.
To start, let’s take the most basic two-variable function and calculate partial derivatives. The function is simply — x squared multiplied by y, and you would differentiate it as follows:
Cool, but how would I do this in Python? Good question. To start, you’ll need to redefine your symbols. And in a traditional Python style, you can do this with one line of code:
Now call to the .diff() function will require one more argument — the term you’re calculating the derivative for. Let’s take a look:
You can here see how the partial derivatives are calculated with respect to x, and then y. Rewriting the function becomes tedious fast, and there’s a way to avoid it. Let’s explore it in the next example.
Here’s another example of taking partial derivatives with respect to all 3 variables:
This will once again require you to alter the symbols:
And you can be smarter this time and set the function to a variable, instead of rewriting it every time:
Cool. The remaining part is absolutely the same as before:
This is much more readable. You can even read it literally — differentiate function f with respect to x.
This wasn’t an advanced calculus tutorial, heck this even wasn’t a calculus tutorial, and it wasn’t intended to be one. The goal was to point you in the right direction, so you don’t have to take derivatives by hand, or some online calculator.
Try to apply this to linear regression with gradient descent — it would be a good exercise, and I’ll post an article on it in a couple of days.
Want to learn how to take integrals in Python? Let me know.
Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.