Skip to content
Learn Netverks

Lesson

Step 15/36 42% through track

curve-fitting

Curve fitting

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Curve fitting: SciPy scientific routines on NumPy arrays—statistics, optimization, linear algebra, and numerical methods.

Teams apply Curve fitting in every serious SciPy project—skipping it leaves blind spots in analysis and reviews.

You will apply Curve fitting in contexts like: Calibration, hyperparameter search, and engineering design optimization.

Read the narrative, run NumPy + SciPy snippets in the playground (install scipy and numpy with pip if needed), inspect outputs and convergence, and complete MCQs.

When you can explain the previous lesson's ideas in your own words.

optimize.curve_fit fits nonlinear model parameters to data by least squares—common in labs, finance decay curves, and sensor calibration.

Model function signature

Define def model(x, a, b, ...): returning predictions. curve_fit returns optimal parameters and covariance estimate.

Workflow

  1. Plot raw (x, y) data
  2. Choose model with physical meaning
  3. Provide initial guess p0
  4. Inspect fitted curve and parameter uncertainties

Exponential decay fit

import numpy as np
from scipy import optimize

def exp_decay(t, a, b):
    return a * np.exp(-b * t)

t = np.linspace(0, 4, 20)
y = 2.5 * np.exp(-0.8 * t) + 0.05 * np.random.default_rng(0).normal(size=20)
popt, pcov = optimize.curve_fit(exp_decay, t, y, p0=[2, 1])
print('fitted a, b:', popt)

Important interview questions and answers

  1. Q: p0 importance?
    A: Poor initial guesses can converge to wrong local minima—plot and try multiple starts.
  2. Q: pcov diagonal?
    A: Variance estimates for parameters—sqrt gives approximate standard errors.

Self-check

  1. What does curve_fit return?
  2. Why plot data before trusting fitted parameters?

Pitfall: Bad p0 guesses send curve_fit to local minima—try multiple starts.

Interview prep

curve_fit returns?

popt optimal parameters, pcov covariance estimate.

Validation?

Plot residuals; try multiple p0; check parameter uncertainties.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • curve_fit use?
  • Outliers effect?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump