Lesson Introduction

Welcome to our lesson on basic derivatives! In machine learning, understanding how things change is crucial. Derivatives help measure these changes, which is key for optimizing models. By the end, you'll understand derivatives, know how to calculate them, and see a practical example using Python.

What is a Derivative?

Let's understand the concept of a derivative. Imagine you're driving a car. Your speed indicates how your position changes over time. Similarly, a derivative measures how a function's output changes as its input changes. It's like the speed for functions. The derivative is calculated for every input of the original function. So, the derivative is also a function of the same input.

Calculating an Approximation for a Derivative: part 1
Calculating an Approximation for a Derivative: part 2
Calculating an Approximation for a Derivative: part 3

Let's demonstrate this with code:

# Define the function f(x) = x^2
def f(x):
    return x**2

delta_x_values = [1, 0.5, 0.1, 0.01, 0.001]
x = 2

for delta_x in delta_x_values:
    approx_derivative = (f(x + delta_x) - f(x)) / delta_x
    print(f'Approx. derivative with delta_x = {delta_x}: {approx_derivative}')

In this code, we make delta_x smaller and smaller and see how the approximation for a derivative changes.. For each delta_x, the approximate derivative is calculated using the difference quotient formula. The result is printed with an f-string, including the current delta_x, and a calculated approximate derivative for it. Here is the output:

Approx. derivative with delta_x = 1: 5.0
Approx. derivative with delta_x = 0.5: 4.5
Approx. derivative with delta_x = 0.1: 4.100000000000001
Approx. derivative with delta_x = 0.01: 4.009999999999891
Approx. derivative with delta_x = 0.001: 4.000999999999699

As you can see, the value gets closer and closer to 4.

Limiting to 0
Implementing a Derivative in Python

Let's see how to calculate it using Python. We'll use the forward difference method to estimate the derivative numerically.

Here’s the function:

# Numerical derivative using forward difference
def derivative(f, x, h=1e-5):
    return (f(x + h) - f(x)) / h

# Sample function: f(x) = x^2
f = lambda x: x**2

# Compute derivative of f at x=3
print("Derivative of f(x) at x=3:", derivative(f, 3))  # Derivative of f(x) at x=3: 6.000009999951316

In this code:

  • derivative takes a function f, a point x, and a small step h.
  • A small h (1e-5) makes the approximation accurate.
  • We define f(x) = x^2 using a lambda function.
  • We compute and print the derivative of f at x = 3.
Lesson Summary

To summarize, today we:

  • Learned what derivatives are and how they measure change.
  • Used pictures and Python code to visualize and approximate derivatives.
  • Calculated derivatives using numerical methods.

Next, you'll move to practice, where you'll calculate derivatives for different functions using Python. Happy coding!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal