| Name | curve-fit-py JSON |
| Version |
1.4
JSON |
| download |
| home_page | None |
| Summary | A package for fitting a curve given an array of data points |
| upload_time | 2024-09-04 15:28:52 |
| maintainer | None |
| docs_url | None |
| author | Giovanni Paiela |
| requires_python | >=3.11.4 |
| license | MIT |
| keywords |
|
| VCS |
 |
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
# curve_fit_py
A package designed to Find the coefficients of a function $f(x)$ that best fits an array of data points.
## Table of contents
- [Installation](#installation)
- [Usage](#usage)
- [How it works](#how-it-works)
- [License](#license)
## Installation
1. Open terminal in vs code
2. Input ``pip install curve_fit_py`` and press enter
## Usage
The package can be used in a couple of ways depending on what you want to do.
The function for curve fitting is ``curve_fit_py.curve_fit(data,x,type,degree,model,p0)``. Not all parameters need to be used, it depends.
The package provides 3 built-in function types:
1. **Polynomial**
2. **Exponential**
3. **Natural logarithmic**
which can be used with
``'polynomial'``, ``'exp'`` and ``'ln'``. If provided ``type = 'polynomial'``, setting a degree to some value is required. Otherwise, not. If one of these types is used, the parameters ``model`` and ``p0`` are not to be used. After applying the function, you can equate it to your coefficients, for example $a,b$ and graph it.
Here's a simple example on how to use it:
```
import numpy as np
import matplotlib as plt
from curve_fit_py import curve_fit
sample = np.array([1,2,3,4,5,6,7,8,9,10])
x = np.arange(1,11) # Obviously a 1 degree polynomial, i.e a line.
a,b = cfp(data=sample,x=x, type='polynomial',degree=1)
t = np.linspace(1,10,50)
fig, ax = plt.subplots()
plt.grid()
plt.scatter(x,sample,color='red')
plt.plot(t,a*t + b, color='black')
plt.show()
```
However, not everybody needs one of these types of functions. Maybe somebody requires a sin function of the type $A\sin(bx)$. In that case, we will not be using ``type`` or ``degree``, no. We will be using a model function, in our case $A\sin(bx)$, and an initial guess for the coefficients $A$ and $b$ stored in the array $p_0$. Here's a simple example:"
```
import numpy as np
import matplotlib as plt
from curve_fit_py import curve_fit
def sin_model(x,a,b):
return a*np.sin(b*x)
sample = np.array([0,5,10,5,0,-5,-10,-5,0])
x = np.arange(0,10) # Obviously a sin function.
a,b = cfp(data=sample,x=x,model = sin_model, p0 =[10,0.69])
t = np.linspace(0,10,50)
fig, ax = plt.subplots()
plt.grid()
plt.scatter(x,sample,color='red')
plt.plot(t,sin_model(t,a,b), color='black')
plt.show()
```
## How it works
The package uses multiple techniques for approximating a curve given a data set.
If the user has provided a type of function in the parameters, the initial guess for the coefficients will be done through least squares, i.e won't be really a guess but a first approximation. It works the following way for a 1 degree polynomial:
$$Ax = b$$
$$\begin{bmatrix}x_1 & 1 \\\ x_2 & 1 \\\ \vdots & \vdots \\\ x_i & 1 \end{bmatrix} \begin{bmatrix} a \\\ b \end{bmatrix} = \begin{bmatrix} y_1 \\\ y_2 \\\ \vdots \\\ y_i \end{bmatrix}$$
In which $x_i$ and $y_i$ are entries from the arrays ``data`` and ``x`` provided. Given that we can't possibly find the inverse of $A$ and solve the equation, we must apply the least square method to give us an approximate answer. That would be:
$$x = (A^TA)^{-1}A^Tb$$
However this doesn't always give a good approximation. In cases with an exponential or log function, we have to apply a second technique in order to get a better estimate after we've applied least squares. That technique is called Gauss-Newton's method. It works the following way:
Imagine we have a matrix with already existing initial approximations/guesses called $p_0$. Say we only have one component or a bunch of them under one name - $\theta$. We now define a new variable, called a residual: $r_i = y_i - f(x_i,\theta)$ in which $f$ is the function that is attempting to approximate the data set. After that we define a matrix $J$ in which we have entries:
$$J = \begin{bmatrix} \frac{\partial r_i}{\partial \theta} \\\ \vdots \end{bmatrix}$$
We multiply $J$ by a matrix called $\Delta \theta$ and we equate to the matrix of residuals $r$.
$$ \begin{bmatrix} \frac{\partial r_i}{\partial \theta} \\\ \vdots \end{bmatrix} \begin{bmatrix} \Delta \theta \end{bmatrix} = \begin{bmatrix} r_i \\\ \vdots \end{bmatrix}$$
We solve for $\Delta \theta$ using the least square method and we get a solution which tells us by how much we should multiply the derivative of $r_i$ with respect to $\theta$ to get the currently existing residual or error of $r_i$. If we do that in the opposite direction, we should get 0 error
If we change the already existing parameters of $r_i$ with new ones $\theta_f = \theta_i - \Delta \theta$ However we can't do that change too drastically because its not always possible to have an error of 0. Instead we apply a learning rate $l_r$ such that:
$$\theta_f = \theta_i - l_r \Delta \theta$$
and we iterate it a couple of times until it converges to some final error which is the minimum.
## License
This package is licensed under the [MIT License](LICENSE.txt)
Raw data
{
"_id": null,
"home_page": null,
"name": "curve-fit-py",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.11.4",
"maintainer_email": null,
"keywords": null,
"author": "Giovanni Paiela",
"author_email": "jovpa47@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/81/8c/ea3aa3cc24df082f380720b4869da3ab88442bb634d8fbd25f0af99e54d7/curve_fit_py-1.4.tar.gz",
"platform": null,
"description": "# curve_fit_py\r\nA package designed to Find the coefficients of a function $f(x)$ that best fits an array of data points.\r\n\r\n## Table of contents\r\n- [Installation](#installation)\r\n- [Usage](#usage)\r\n- [How it works](#how-it-works)\r\n- [License](#license)\r\n\r\n## Installation\r\n1. Open terminal in vs code\r\n\r\n2. Input ``pip install curve_fit_py`` and press enter\r\n\r\n## Usage\r\nThe package can be used in a couple of ways depending on what you want to do.\r\nThe function for curve fitting is ``curve_fit_py.curve_fit(data,x,type,degree,model,p0)``. Not all parameters need to be used, it depends. \r\nThe package provides 3 built-in function types:\r\n1. **Polynomial**\r\n2. **Exponential**\r\n3. **Natural logarithmic**\r\n\r\nwhich can be used with\r\n\r\n ``'polynomial'``, ``'exp'`` and ``'ln'``. If provided ``type = 'polynomial'``, setting a degree to some value is required. Otherwise, not. If one of these types is used, the parameters ``model`` and ``p0`` are not to be used. After applying the function, you can equate it to your coefficients, for example $a,b$ and graph it.\r\n\r\n Here's a simple example on how to use it:\r\n\r\n ```\r\n import numpy as np\r\n import matplotlib as plt\r\n from curve_fit_py import curve_fit\r\n\r\n sample = np.array([1,2,3,4,5,6,7,8,9,10]) \r\n x = np.arange(1,11) # Obviously a 1 degree polynomial, i.e a line.\r\n a,b = cfp(data=sample,x=x, type='polynomial',degree=1)\r\n\r\n t = np.linspace(1,10,50)\r\n fig, ax = plt.subplots()\r\n plt.grid()\r\n plt.scatter(x,sample,color='red')\r\n plt.plot(t,a*t + b, color='black')\r\n plt.show()\r\n```\r\n\r\nHowever, not everybody needs one of these types of functions. Maybe somebody requires a sin function of the type $A\\sin(bx)$. In that case, we will not be using ``type`` or ``degree``, no. We will be using a model function, in our case $A\\sin(bx)$, and an initial guess for the coefficients $A$ and $b$ stored in the array $p_0$. Here's a simple example:\"\r\n\r\n```\r\n import numpy as np\r\n import matplotlib as plt\r\n from curve_fit_py import curve_fit\r\n\r\ndef sin_model(x,a,b):\r\n return a*np.sin(b*x)\r\n\r\n sample = np.array([0,5,10,5,0,-5,-10,-5,0]) \r\n x = np.arange(0,10) # Obviously a sin function.\r\n a,b = cfp(data=sample,x=x,model = sin_model, p0 =[10,0.69])\r\n\r\n t = np.linspace(0,10,50)\r\n fig, ax = plt.subplots()\r\n plt.grid()\r\n plt.scatter(x,sample,color='red')\r\n plt.plot(t,sin_model(t,a,b), color='black')\r\n plt.show()\r\n```\r\n\r\n## How it works\r\n\r\nThe package uses multiple techniques for approximating a curve given a data set.\r\n\r\nIf the user has provided a type of function in the parameters, the initial guess for the coefficients will be done through least squares, i.e won't be really a guess but a first approximation. It works the following way for a 1 degree polynomial:\r\n\r\n$$Ax = b$$\r\n\r\n$$\\begin{bmatrix}x_1 & 1 \\\\\\ x_2 & 1 \\\\\\ \\vdots & \\vdots \\\\\\ x_i & 1 \\end{bmatrix} \\begin{bmatrix} a \\\\\\ b \\end{bmatrix} = \\begin{bmatrix} y_1 \\\\\\ y_2 \\\\\\ \\vdots \\\\\\ y_i \\end{bmatrix}$$\r\n\r\nIn which $x_i$ and $y_i$ are entries from the arrays ``data`` and ``x`` provided. Given that we can't possibly find the inverse of $A$ and solve the equation, we must apply the least square method to give us an approximate answer. That would be:\r\n\r\n$$x = (A^TA)^{-1}A^Tb$$\r\n\r\nHowever this doesn't always give a good approximation. In cases with an exponential or log function, we have to apply a second technique in order to get a better estimate after we've applied least squares. That technique is called Gauss-Newton's method. It works the following way:\r\n\r\nImagine we have a matrix with already existing initial approximations/guesses called $p_0$. Say we only have one component or a bunch of them under one name - $\\theta$. We now define a new variable, called a residual: $r_i = y_i - f(x_i,\\theta)$ in which $f$ is the function that is attempting to approximate the data set. After that we define a matrix $J$ in which we have entries:\r\n\r\n$$J = \\begin{bmatrix} \\frac{\\partial r_i}{\\partial \\theta} \\\\\\ \\vdots \\end{bmatrix}$$\r\n\r\nWe multiply $J$ by a matrix called $\\Delta \\theta$ and we equate to the matrix of residuals $r$.\r\n\r\n$$ \\begin{bmatrix} \\frac{\\partial r_i}{\\partial \\theta} \\\\\\ \\vdots \\end{bmatrix} \\begin{bmatrix} \\Delta \\theta \\end{bmatrix} = \\begin{bmatrix} r_i \\\\\\ \\vdots \\end{bmatrix}$$\r\n\r\nWe solve for $\\Delta \\theta$ using the least square method and we get a solution which tells us by how much we should multiply the derivative of $r_i$ with respect to $\\theta$ to get the currently existing residual or error of $r_i$. If we do that in the opposite direction, we should get 0 error\r\n\r\nIf we change the already existing parameters of $r_i$ with new ones $\\theta_f = \\theta_i - \\Delta \\theta$ However we can't do that change too drastically because its not always possible to have an error of 0. Instead we apply a learning rate $l_r$ such that:\r\n\r\n$$\\theta_f = \\theta_i - l_r \\Delta \\theta$$\r\n\r\nand we iterate it a couple of times until it converges to some final error which is the minimum.\r\n\r\n## License\r\n\r\nThis package is licensed under the [MIT License](LICENSE.txt)\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A package for fitting a curve given an array of data points",
"version": "1.4",
"project_urls": {
"Source": "https://github.com/Paiela/curve_fit_py"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "30a5628e7dab98e96afa03a5ec6f2933bbeb2608999011980b65bc521e22b817",
"md5": "3db805fd146def53cd2a91f71f69fdfb",
"sha256": "f8fafbc0614a1d7573b4ecbf59a96e1ca54f61dde47985e384050f2aecd4c7c5"
},
"downloads": -1,
"filename": "curve_fit_py-1.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "3db805fd146def53cd2a91f71f69fdfb",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11.4",
"size": 5911,
"upload_time": "2024-09-04T15:28:51",
"upload_time_iso_8601": "2024-09-04T15:28:51.871927Z",
"url": "https://files.pythonhosted.org/packages/30/a5/628e7dab98e96afa03a5ec6f2933bbeb2608999011980b65bc521e22b817/curve_fit_py-1.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "818cea3aa3cc24df082f380720b4869da3ab88442bb634d8fbd25f0af99e54d7",
"md5": "fa2751721592aa3f294b3d25c2228449",
"sha256": "289d16326ed269cf951d1a4794ea64a38dee11aa8304cd36fefd41f20b7e57d1"
},
"downloads": -1,
"filename": "curve_fit_py-1.4.tar.gz",
"has_sig": false,
"md5_digest": "fa2751721592aa3f294b3d25c2228449",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11.4",
"size": 5661,
"upload_time": "2024-09-04T15:28:52",
"upload_time_iso_8601": "2024-09-04T15:28:52.831479Z",
"url": "https://files.pythonhosted.org/packages/81/8c/ea3aa3cc24df082f380720b4869da3ab88442bb634d8fbd25f0af99e54d7/curve_fit_py-1.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-09-04 15:28:52",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "Paiela",
"github_project": "curve_fit_py",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "curve-fit-py"
}