Calculus for DS: An Introduction to Derivatives

Linear and Non-Linear

Deijah
The Startup
6 min readMay 7, 2020

--

You can easily work with a pre-made algorithm or API as a data scientist but knowing the bones of that algorithm will strengthen you on your journey and give you the knowledge necessary to recreate your own. As with all sciences, math is the core foundation of data science. Calculus is used in data science for things like gradient descent, limits, and derivative chain rule. We can start with the very basics and introduce derivatives.

What are derivatives?

A derivative is the instantaneous rate of change of a function. In other words, derivatives allow us to make more accurate predictions of what will come next by providing us insight into how a function changes at any given point.

You may be familiar with the how following formula for calculating the derivative of a straight line:

y = mx + b (rise over run)

Because the rate of change of a straight line never alters, the derivate never changes from point to point.

You can also express a change in y (y2 — y1) and change in x (x2 — x1)as Δy (delta y) and Δx (delta x).

Let's imagine you needed to predict what time a train would arrive at a specific location. Assuming the train maintains a steady speed throughout the duration of its journey, one could make a prediction using the above formula.

Let’s say the train departed at 1 pm and arrived at 4 pm and it traveled from mile 30 of the transit line to mile 270.

The train traveled 240 miles over the duration of 3 hours giving us the rate of change 80 miles per hour.

The derivative of a linear function f(x) read f primed of x or 𝑓′(𝑥) can be expressed in many ways that will all produce the same result.

We can replace y2 and y1 with f(x2) and f(x1) because we are just saying the output of the first and second x values.

  • You express the derivative in terms of the output at second x minus the output at the first x divided by the difference between the two x values
  • You can also express it in terms of Δ𝑥 by adding the output of the first x to the change in x and subtracting the output of the first x. Then divide by change in x.

Working with Curved Lines

What if we needed to calculate the rate of change of something that sped up or slowed down over time? We simply could not use the previous derivative formula because if you were to check the speed at any second point in time, you wouldn’t be accurate.

The rate of change of the blue line is less than the rate of change in the curved line.

For curved lines, we need to be more precise and we can do this by decreasing the change in x (Δx).

Limit

In the below-left image Δx = 1 and in the right graph Δx =0.1. The graph with the line becoming more and more tangent to the curve (meaning the line is just touching it) is the right graph. So we can see that as the change in x approaches 0, the closer we are to the derivative.

The derivative that we reach as the change in x approaches 0 is called limit.

(left ) Δx = 0.01 (right) Δx = .001

When x = 2 the limit is 4. This idea of limit can be expressed as follows with the latter simply replacing delta x with h to now represent the change in x:

Above is the function for derivative for both nonlinear and linear. This creates a more accurate definition of derivate:

Derivative is the rate of change of a function as the change in x approaches zero.

The coolest part is that now we are finding the rate of change in a moment.

Calculating Derivative in Python

To calculate the derivative of an x value in Python, start a console and install the sympy library.

Then import the multiplication operator and create a symbol called “x” using the sympy symbols method.

Let’s say we have the following function:

We can represent this using the code below.

f is now equal to:

The lamdify method takes in the two arguments of an x value and your created function:

With an x value of 2, the derivative output is:

The next part of this journey involves partial derivatives and gradient descent. In machine learning, gradient descent is used to find the derivative of the sum of errors helping to greatly reduce the possibility of error and in turn helping systems to predict with more accuracy.

I hope I helped break down the very basics of calculus for Data Science. If you are curious and want to learn more here are a few resources and online courses:

Calculus for Machine Learning

Coursera Mathematics for Data Science Specialization

Flatiron School Data Science Bootcamp Prep

Meet the Author

Deijah is a cyber risk and compliance analyst, member of Data Visualization Society, and is also a developer. She is a closet screenwriter, cinephile and mom of three fur children with her dear husband. She is on a journey of self-teaching french and living one day at a time.

“Life is like a box of chowc-litts…you never know what you gonna get” — Forrest Gump

--

--

Deijah
The Startup

Cyber Risk and Compliance Analyst. Developer. Data journalism enthusiast. Cinephile.