Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Implementing Derivatives to Python | Mathematical Analysis
Mathematics for Data Science

bookImplementing Derivatives to Python

Derivatives are fundamental in calculus and widely used in data science, optimization, and machine learning. In Python, we can compute derivatives symbolically using sympy and visualize them using matplotlib.

1. Computing Derivatives Symbolically

# Define symbolic variable x
x = sp.symbols('x')
# Define the functions
f1 = sp.exp(x)  
f2 = 1 / (1 + sp.exp(-x))  
# Compute derivatives symbolically
df1 = sp.diff(f1, x)  
df2 = sp.diff(f2, x)

Explanation:

  • We define x as a symbolic variable using sp.symbols('x');
  • The function sp.diff(f, x) computes the derivative of f with respect to x;
  • This allows us to manipulate derivatives algebraically in Python.

2. Evaluating and Plotting Functions and Their Derivatives

# Convert symbolic functions to numerical functions for plotting
f1_lambda = sp.lambdify(x, f1, 'numpy')
df1_lambda = sp.lambdify(x, df1, 'numpy')
f2_lambda = sp.lambdify(x, f2, 'numpy')
df2_lambda = sp.lambdify(x, df2, 'numpy')

Explanation:

  • sp.lambdify(x, f, 'numpy') converts a symbolic function into a numerical function that can be evaluated using numpy;
  • This is required because matplotlib and numpy operate on numerical arrays, not symbolic expressions.

3. Printing Derivative Evaluations for Key Points

To verify our calculations, we print derivative values at x = [-5, 0, 5].

# Evaluate derivatives at key points
test_points = [-5, 0, 5]
for x_val in test_points:
    print(f"x = {x_val}: e^x = {f2_lambda(x_val):.4f}, e^x' = {df2_lambda(x_val):.4f}")
    print(f"x = {x_val}: sigmoid(x) = {f4_lambda(x_val):.4f}, sigmoid'(x) = {df4_lambda(x_val):.4f}")
    print("-" * 50)

1. Why do we use sp.lambdify(x, f, 'numpy') when plotting derivatives?

2. If we change x_vals = np.linspace(-5, 5, 10) instead of 1000, what will happen?

3. If we compute sp.diff(f4, x) for the sigmoid function, why does the result involve δ(1δ)δ(1 - δ)?

4. When comparing the graphs of f(x)=exf(x) = e^x and its derivative, which of the following is true?

question mark

Why do we use sp.lambdify(x, f, 'numpy') when plotting derivatives?

Select the correct answer

question mark

If we change x_vals = np.linspace(-5, 5, 10) instead of 1000, what will happen?

Select the correct answer

question mark

If we compute sp.diff(f4, x) for the sigmoid function, why does the result involve δ(1δ)δ(1 - δ)?

Select the correct answer

question mark

When comparing the graphs of f(x)=exf(x) = e^x and its derivative, which of the following is true?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 4

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Awesome!

Completion rate improved to 1.89

bookImplementing Derivatives to Python

Swipe um das Menü anzuzeigen

Derivatives are fundamental in calculus and widely used in data science, optimization, and machine learning. In Python, we can compute derivatives symbolically using sympy and visualize them using matplotlib.

1. Computing Derivatives Symbolically

# Define symbolic variable x
x = sp.symbols('x')
# Define the functions
f1 = sp.exp(x)  
f2 = 1 / (1 + sp.exp(-x))  
# Compute derivatives symbolically
df1 = sp.diff(f1, x)  
df2 = sp.diff(f2, x)

Explanation:

  • We define x as a symbolic variable using sp.symbols('x');
  • The function sp.diff(f, x) computes the derivative of f with respect to x;
  • This allows us to manipulate derivatives algebraically in Python.

2. Evaluating and Plotting Functions and Their Derivatives

# Convert symbolic functions to numerical functions for plotting
f1_lambda = sp.lambdify(x, f1, 'numpy')
df1_lambda = sp.lambdify(x, df1, 'numpy')
f2_lambda = sp.lambdify(x, f2, 'numpy')
df2_lambda = sp.lambdify(x, df2, 'numpy')

Explanation:

  • sp.lambdify(x, f, 'numpy') converts a symbolic function into a numerical function that can be evaluated using numpy;
  • This is required because matplotlib and numpy operate on numerical arrays, not symbolic expressions.

3. Printing Derivative Evaluations for Key Points

To verify our calculations, we print derivative values at x = [-5, 0, 5].

# Evaluate derivatives at key points
test_points = [-5, 0, 5]
for x_val in test_points:
    print(f"x = {x_val}: e^x = {f2_lambda(x_val):.4f}, e^x' = {df2_lambda(x_val):.4f}")
    print(f"x = {x_val}: sigmoid(x) = {f4_lambda(x_val):.4f}, sigmoid'(x) = {df4_lambda(x_val):.4f}")
    print("-" * 50)

1. Why do we use sp.lambdify(x, f, 'numpy') when plotting derivatives?

2. If we change x_vals = np.linspace(-5, 5, 10) instead of 1000, what will happen?

3. If we compute sp.diff(f4, x) for the sigmoid function, why does the result involve δ(1δ)δ(1 - δ)?

4. When comparing the graphs of f(x)=exf(x) = e^x and its derivative, which of the following is true?

question mark

Why do we use sp.lambdify(x, f, 'numpy') when plotting derivatives?

Select the correct answer

question mark

If we change x_vals = np.linspace(-5, 5, 10) instead of 1000, what will happen?

Select the correct answer

question mark

If we compute sp.diff(f4, x) for the sigmoid function, why does the result involve δ(1δ)δ(1 - δ)?

Select the correct answer

question mark

When comparing the graphs of f(x)=exf(x) = e^x and its derivative, which of the following is true?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 4
some-alt