Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scipy curve fit #5919

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9612ccf
Finished initial description
ca999 Jan 3, 2025
d37462d
syntax
ca999 Jan 4, 2025
0a24e82
complete
ca999 Jan 5, 2025
e53f07a
🤖 update concept of the week
codecademy-gh-api Jan 5, 2025
d424f6f
spellings + code
ca999 Jan 6, 2025
91d3f6f
Update curve-fit.md
mamtawardhani Jan 8, 2025
edc6db9
Check to verify that there are not missing md files (#5553)
jrood Jan 6, 2025
c8466ca
Volatile variables (#5721)
Nil-Morah Jan 7, 2025
878dfe4
feat: [Concept Entry] Git Checkout (#5748) (#5769)
tommaso-berti Jan 7, 2025
ce0fcdd
Update post-merge copy (#5922)
jrood Jan 7, 2025
95ee575
Update json.md (#5891)
vlrnsnk Jan 7, 2025
cb821c7
[Term Entry] Python statsmodels: anova_lm() (#5907)
SaviDahegaonkar Jan 8, 2025
e624ab1
Added docs for javascript spread operator - #5802 (#5827)
Maverick073 Jan 8, 2025
5ba20f4
Update curve-fit.md
mamtawardhani Jan 8, 2025
3a93d85
[Edit] Combine Duplicate Focus Group Entries (#5829)
ebikatsudon Jan 8, 2025
b95eba3
[Concept Entry] Rust: Ownership (#5880)
SaviDahegaonkar Jan 8, 2025
73af5de
[Concept Entry] JavaScript: Nullish Coalescing
hitomipupil Jan 8, 2025
53cb3ed
trying to commit with github email
ca999 Jan 8, 2025
4fc0268
Merge branch 'main' into scipy-curve-fit
ca999 Jan 8, 2025
65534f3
Merge branch 'main' into scipy-curve-fit
ca999 Jan 10, 2025
13e1e55
Merge branch 'main' into scipy-curve-fit
ca999 Jan 10, 2025
5137b6a
addressed reviews
ca999 Jan 15, 2025
f8bbc26
Update content/scipy/concepts/scipy-optimize/terms/curve-fit/curve-fi…
avdhoottt Jan 18, 2025
adefa9b
Update content/scipy/concepts/scipy-optimize/terms/curve-fit/curve-fi…
avdhoottt Jan 18, 2025
e017bc7
Final Changes
avdhoottt Jan 18, 2025
2575bd0
Merge branch 'main' into scipy-curve-fit
avdhoottt Jan 18, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions content/scipy/concepts/scipy-optimize/terms/curve-fit/curve-fit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
Title: 'Curve Fit'
Description: 'Fits a custom function to data by adjusting its parameters to match the data as closely as possible.'
Subjects:
- 'Data Science'
- 'Machine Learning'
Tags:
- 'Python'
- 'Optimization'
- 'Mathematics'
CatalogContent:
- 'learn-python'
- 'paths/data-science'
---

**`curve_fit()`** fits a custom function to data by adjusting its parameters to minimize the difference between the function's predictions and the actual data points, ensuring the best possible match.

## Syntax

```pseudo
popt, pcov = curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=None, bounds=(-inf, inf), method=None, jac=None, *, full_output=False, nan_policy=None, **kwargs)
```

- `f`: This is the function to fit to the data.
- `xdata`: An array-like input representing the independent variable values (e.g., time, x-coordinates, etc.).
- `ydata`: An array-like input representing the dependent variable values (e.g., measured data corresponding to `xdata`).
- `p0`: Initial guess of the parameters.
- `sigma`: Defines the uncertainty in `ydata`.
- `absolute_sigma`: If `True`, `sigma` is interpreted absolutely, and the parameter covariance pcov reflects absolute values. If `False` which is the default, `sigma` is scaled to normalize residual variance. Here, pcov(absolute_sigma=False) = pcov(absolute_sigma=True) \* chisq(popt)/(M-N).
- `check_finite`: Ensures input arrays do not contain `NaN` or `inf`. If `True`, a `ValueError` is raised when such values are found. Defaults to `True` unless `nan_policy` is explicitly specified.
- `bounds`: Specifies parameter bounds. Defaults to no bounds. Options include:
- An instance of the `Bounds` class.
- A 2-tuple of array-like objects or scalars: Scalars apply bounds uniformly, and `np.inf` can disable bounds partially.
- `method` - Optimization method. Choices are:
- `'lm'` (default for unconstrained problems): Levenberg-Marquardt.
- `'trf'` (default if bounds are set): Trust Region Reflective.
- `'dogbox'`: Dogleg.
`'lm'` cannot handle cases where observations < variables. Use `'trf'` or `'dogbox'` instead.
- `jac`: Jacobian matrix computation for `jac(x, ...)`. Defaults to numerical estimation if None. Supports finite difference schemes for `'trf'` and `'dogbox'` methods.
- `full_output`: If `True`, returns additional information such as infodict, mesg, ier.
- `nan_policy`: Decides behavior when `NaN` values exist in input data:
- None (default): No special handling; behavior depends on implementation.
- `'raise'`: Throws an error.
- `'omit'`: Ignores NaN values during computation.
- `**kwargs`: Additional keyword arguments passed to `leastsq` (if method = 'lm') or least_squares otherwise.

It returns:

- `popt`: A 1D array containing the optimal values of the parameters (`a`, `b`, `c`, etc.) that minimize the difference between the function and the data (`ydata`).
- `pcov`: A 2D array representing the covariance matrix of the estimated parameters, which provides an estimate of the uncertainties (or standard errors) associated with the optimized parameters.

## Example

This code uses `curve_fit` to fit a custom exponential decay function to noisy data and estimates the parameters `a`, `b`, and `c`:

```py
import numpy as np
from scipy.optimize import curve_fit

# Define your custom function
def func(x, a, b, c):
return a * np.exp(-b * x) + c

# Define your data
xdata = np.linspace(0, 4, 50)
y = func(xdata, 2.5, 1.3, 0.5)
rng = np.random.default_rng()
y_noise = 0.2 * rng.normal(size=xdata.size)
ydata = y + y_noise

# Fit for the parameters a, b, c of the function func:
popt, pcov = curve_fit(func, xdata, ydata)
print(popt)
```

The above will give the following output:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output differs every time we run the code. We should add a note here saying that.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @avdhoottt I have added in a note saying that the output will differ for each run

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @avdhoottt Is there anything else that I need to do to get this PR merged ?


Note: since we are using np.random() for rng variable the output is expected to change for each run.
avdhoottt marked this conversation as resolved.
Show resolved Hide resolved

```shell
[2.47681145 1.32375703 0.53063146]
```