-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
ca999
wants to merge
26
commits into
Codecademy:main
Choose a base branch
from
ca999:scipy-curve-fit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Scipy curve fit #5919
Changes from 24 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
9612ccf
Finished initial description
ca999 d37462d
syntax
ca999 0a24e82
complete
ca999 e53f07a
🤖 update concept of the week
codecademy-gh-api d424f6f
spellings + code
ca999 91d3f6f
Update curve-fit.md
mamtawardhani edc6db9
Check to verify that there are not missing md files (#5553)
jrood c8466ca
Volatile variables (#5721)
Nil-Morah 878dfe4
feat: [Concept Entry] Git Checkout (#5748) (#5769)
tommaso-berti ce0fcdd
Update post-merge copy (#5922)
jrood 95ee575
Update json.md (#5891)
vlrnsnk cb821c7
[Term Entry] Python statsmodels: anova_lm() (#5907)
SaviDahegaonkar e624ab1
Added docs for javascript spread operator - #5802 (#5827)
Maverick073 5ba20f4
Update curve-fit.md
mamtawardhani 3a93d85
[Edit] Combine Duplicate Focus Group Entries (#5829)
ebikatsudon b95eba3
[Concept Entry] Rust: Ownership (#5880)
SaviDahegaonkar 73af5de
[Concept Entry] JavaScript: Nullish Coalescing
hitomipupil 53cb3ed
trying to commit with github email
ca999 4fc0268
Merge branch 'main' into scipy-curve-fit
ca999 65534f3
Merge branch 'main' into scipy-curve-fit
ca999 13e1e55
Merge branch 'main' into scipy-curve-fit
ca999 5137b6a
addressed reviews
ca999 f8bbc26
Update content/scipy/concepts/scipy-optimize/terms/curve-fit/curve-fi…
avdhoottt adefa9b
Update content/scipy/concepts/scipy-optimize/terms/curve-fit/curve-fi…
avdhoottt e017bc7
Final Changes
avdhoottt 2575bd0
Merge branch 'main' into scipy-curve-fit
avdhoottt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
content/scipy/concepts/scipy-optimize/terms/curve-fit/curve-fit.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
|
||
> **Note:** Since we are using `np.random()` for `rng` variable the output is expected to change for each run. | ||
|
||
```shell | ||
[2.47681145 1.32375703 0.53063146] | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ?