Python 🐍 Nonlinear Regression Curve Fit

  Рет қаралды 80,617

APMonitor.com

APMonitor.com

Күн бұрын

The Scipy curve_fit function determines four unknown coefficients to minimize the difference between predicted and measured heart rate. Pandas is used to import and view the data. A Matplotlib plot is used to see how the initial guess values and the final optimized parameter values compare for generating predictions. An R-squared value is calculated to show how well the predictions and measurements align.
Course Material: apmonitor.com/che263/index.ph...
Github Repository (see HW04.ipynb): github.com/APMonitor/learn_py...
Schedule: apmonitor.com/che263/index.ph...
Solution Videos: apmonitor.com/che263/index.ph...

Пікірлер: 98
@alibastami8626
@alibastami8626 4 жыл бұрын
Just the fact that you provide this invaluable content for free, when others are selling low quality courses for a lot of money, proves how a great human being you are. You are a true Master.
@apm
@apm 4 жыл бұрын
Thanks Ali!
@arcesarino
@arcesarino 5 жыл бұрын
Perfect! Thank you so much for the great video.
@SuperCamProductions
@SuperCamProductions 3 жыл бұрын
This was exactly what I needed, thank you!
@arushijain4457
@arushijain4457 4 жыл бұрын
Super! So easily explained, thank you.
@OnlyfansCustomerSupport
@OnlyfansCustomerSupport Жыл бұрын
thanks for the help this was a long long journey it took me like 3-5 hours to translate my german function name in its actual english equivalent since the direct translation is miles miles away :D and finaly i got a model that should help me solve my enzyme activity measures
@DFS370
@DFS370 3 жыл бұрын
thank u X 10^10 . U r a great professional and teacher !
@gomezest
@gomezest 2 жыл бұрын
This is really good content! Thank you very much for sharing your knowledge. Im still blown away by the quality and how you explained the material
@apm
@apm 2 жыл бұрын
Glad it was helpful!
@asifraj321
@asifraj321 2 жыл бұрын
Very helpful and valuable too. Thank you.
@eaglepaul87
@eaglepaul87 3 жыл бұрын
Oh really useful!! Thanks a lot
@datastako156
@datastako156 2 жыл бұрын
thank you for sharing!
@Thahid
@Thahid 3 жыл бұрын
Excellent video
@bastianian2939
@bastianian2939 2 жыл бұрын
cool video!
@badinhbk
@badinhbk 5 жыл бұрын
Thank you very much. Your channel is very helpful for me :)
@rrc
@rrc 5 жыл бұрын
Thanks!
@lukas-santopuglisi668
@lukas-santopuglisi668 4 жыл бұрын
thanks a lot ! :)
@alvaro_gavilan_rojas
@alvaro_gavilan_rojas 3 жыл бұрын
Thank you!
@agustingambaretto2260
@agustingambaretto2260 3 жыл бұрын
Thanks for the demo, very useful for a beginner like me! managed to do something similar following the steps. I wanted to ask something. My formula in stead of having a single variable ( time), y have 3 input variables, i have to compute p1[i], p2[i], p3[i] to be able to calculate the y value. I manage to adapt my code to that and plot it, but i got stucked in the curve_fit. In the curve_fit uses only 1 xdata if i am correct. What variation can i use? I hope you know, thanks !
@apm
@apm 3 жыл бұрын
Check out this tutorial apmonitor.com/pds/notebooks/06_regression.html
@kareemsakr41
@kareemsakr41 4 жыл бұрын
thank you
@NawtieBoy96
@NawtieBoy96 2 жыл бұрын
6:26 whats the formula really that you have used in that bpm fn ? i didnt understand that part. usually what I know is that therea re various non-linear methods that one can choose for non-linear reg types such as polynomial fits , lograthmic fits and so on . correct me if I am wrong pls
@apm
@apm 2 жыл бұрын
Check out 3:58 for the correlation that is used in the bpm function. There are many different types of nonlinear functions that can be used or even machine learning where something like an LSTM can predict time-series data. Here is more information on machine learning approaches: apmonitor.com/pds There is a regression module that may help in particular: apmonitor.com/pds/notebooks/06_regression.html
@margalanis
@margalanis 4 жыл бұрын
Hey, great content and pretty clear explanations! I have a question though. Is it easy to add some constraints to your variables (e.g. 0
@apm
@apm 4 жыл бұрын
No, you can add simple upper and lower bound constraints to variables in Scipy curve fit but not more complicated bounds that involve two or more variables. There is also scipy.optimize.minimize and python gekko that support these types of constraints: apmonitor.com/che263/index.php/Main/PythonOptimization
@margalanis
@margalanis 4 жыл бұрын
@@apm thanks a lot for the feedback!
@obinnaizima9387
@obinnaizima9387 4 жыл бұрын
Hi, this was very helpful. However, if you had a dataset that comprises of multiple exponentially decaying curves, how would this work out? I can relatively do this for a single exponential decay curve but unsure on how to deal with multiple exp. decay curves.
@apm
@apm 4 жыл бұрын
You can add additional variables and equations to the model. It is no problem to have any number of variables and equations. There are additional tutorials on regression here: github.com/APMonitor/data_science/blob/master/06.%20Regression.ipynb
@stevethach3340
@stevethach3340 4 жыл бұрын
Hi! Great video! A couple of questions: 1) What if you had multiple people's heart rate? Is it possible to create a curve that would fit all of them? 2) What if we do not have the BPM equation? Thank you!
@apm
@apm 4 жыл бұрын
Yes, just add the other people to the objective function. You'll likely need a nested loop inside the objective function. The objective function is a single number so all the sum of squared errors are lumped into one value.
@guerreirodaluzgmailcom
@guerreirodaluzgmailcom 4 жыл бұрын
Hi Mr. ,What criteria does it take to choose the values for the initial conjecture of the parameters? I will be grateful for the answer
@apm
@apm 4 жыл бұрын
They are guess values. If it doesn't converge with guess values of 1 or 0 then I sometimes create a plot and adjust the values myself until the predicted gets closer to the data. Vanishing gradients or local minima can be a problem with poor initial guess values.
@certifiedcriticmedia1532
@certifiedcriticmedia1532 4 жыл бұрын
Nice
@shreeniketjoshi
@shreeniketjoshi 4 жыл бұрын
Hi, that was awesome and very helpful! Just one question, when you entered c, cov where was the cov used? Only c was printed out, right? If I enter just c in place of c,cov would there be any difference? If you can attach a link for the same, that would be great. Thanks a bunch
@apm
@apm 4 жыл бұрын
Yes, you can look at the error or R^2 value when you adjust only one of the parameters at a time. Here is another example: apmonitor.com/me575/index.php/Main/NonlinearRegression You can turn the status to off (0) for any of the parameters that you want to hold constant.
@apm
@apm 4 жыл бұрын
Yes, you need to have both output arguments otherwise it includes c,cov as a tuple.
@shreeniketjoshi
@shreeniketjoshi 4 жыл бұрын
@@apm Oh thanks! So basically the curve fit function outputs c, cov and we need to receive it. We may choose not to use it. Thank you so much
@skkumarish
@skkumarish 4 жыл бұрын
I'm a newbie to python. can you please explain me why use choose the particular equation for your curve fitting?
@apm
@apm 4 жыл бұрын
The choice of equation is just an example to show nonlinear regression. Sometimes you have to try different equation forms to get one that fits well.
@tonycardinal413
@tonycardinal413 Жыл бұрын
Thank you so much for posting this. Ques: If there is a high covariance between the parameters why is this bad? thanx!
@apm
@apm Жыл бұрын
The parameters are single values so there is no covariance between them. If you have a high degree of covariance between data (x, y), it means that the two variables have a strong linear relationship. If one goes up, the other also goes up (or down).
@KarthikeyanMmmm
@KarthikeyanMmmm 2 жыл бұрын
hello sir , is there any similar package in python as curve fit , in which we can able to impose some constraints ( just like during regression at a particular point slope should be a specific value for continuity ) during regression, OR is there any posiblity in curve_fit to impose some constraints
@apm
@apm 2 жыл бұрын
Try Python gekko or scipy.optimize.minimize for two constrained options in regression: apmonitor.com/me575/index.php/Main/NonlinearRegression
@KarthikeyanMmmm
@KarthikeyanMmmm 2 жыл бұрын
@APMonitor.com sir , I have X and Y data, in which first ranga of data fits in certain equation and next range of data fits in some other equation, I used scipy.curve fit first set of data to non linear equation, for second set of data to be fitted in second equation , I need the regressor to follow the constraints, 1. at the break point / trasnsitiin point of frist equation to second, the value of y(x) two equation should be same, 2 . and also slope of equation one and two at that transition point should also have to equal , so I have to use this constraint during fitting of second range of data to the second equation , what I understood minimize () is not a regressor ,, a equation solver, and in GEKKO I don't know how to impose those constrains, please give a help or suggession, Hope you aware of maple, in that 'LSSolve ' was there to docl this regression with constraints, I need a similar function or package in phython to do curve fitting with user defined contraints
@apm
@apm 2 жыл бұрын
@@KarthikeyanMmmm you can switch equations with one equation with 0=b*(eqn1)+(1-b)*(eqn2) with b=1 when eqn1 is active and b=0 when eqn2 is active. You can set the derivative equal at the switch point with (s*(eqn1.dt()-eqn2.dt())==0) where s is the switch point that is 1 when there is the switch. Please see apmonitor.com/do for additional examples. If you have questions as you get started, please post a complete and minimal example to StackOverflow with tag [gekko].
@KR-uy6or
@KR-uy6or 4 жыл бұрын
So for non linear regression we have to assume/guess which curve equation will fit the data and unlike linear regression in PYTHON which predicts itself??
@apm
@apm 4 жыл бұрын
Yes, that is correct. Machine learning has fewer assumptions on model form. apmonitor.github.io/data_science
@KR-uy6or
@KR-uy6or 4 жыл бұрын
@@apm thanks for the help🙂
@erenyeager4452
@erenyeager4452 2 жыл бұрын
very good tutorial, however my doubt is what if you have a matrix of parameters? how do you make the objective function. eg: Ax = b. I need to find A. how do you frame the problem?
@apm
@apm 2 жыл бұрын
You may want to use Gekko for that problem: apmonitor.com/wiki/index.php/Main/GekkoPythonOptimization Check out the matrix operations with Gekko: github.com/BYU-PRISM/GEKKO/blob/master/examples/test_arrays.py
@KarthikeyanMmmm
@KarthikeyanMmmm 2 жыл бұрын
is there iany way to solve equations in a python list [eqn1,eqn2,eqn3.........] with adition constainsts in form of equations e.g. a+bX=90, a*X^2+b*X=100, like in regression we have a model, if we substitute data points in it we will get many equations of model parameters along with it we add additional constaraints please help me
@apm
@apm 2 жыл бұрын
Yes, please see apmonitor.com/me575/index.php/Main/NonlinearRegression as an example of this type of problem where you have an objective and constraint with data regression.
@giocanox97
@giocanox97 3 жыл бұрын
Thanks for the very clear video. However, it doesn't seem to work for my specific function: after defining variables x and y using x.values as in the video at 10:56 from a datafile, I try doing curve fit and it tells me that "only size-1 arrays can be converted to python scalars", which I am unable to fix. All the other steps are the same as in the video This is the function I am trying to fit: y = c_1 * x^{c_2}
@apm
@apm 3 жыл бұрын
It is x**c2 instead of x^c2. Does that help? Source code is here: apmonitor.com/che263/index.php/Main/PythonDataRegression
@giocanox97
@giocanox97 3 жыл бұрын
@@apm thanks a lot! I was using math package but without it it works fine
@senolkurt7864
@senolkurt7864 2 ай бұрын
Thanks for the great tutorial. Since in real life we have only the data, how can we find the best non-linear equation that fits to our data?
@apm
@apm 2 ай бұрын
If you don't know the non-linear equation, try using regression with Machine Learning: apmonitor.com/pds/index.php/Main/RegressionOverview The lazypredict package in Python helps evaluate the regression on multiple models. Otherwise, try different functional forms that you design.
@benedictodhiambo4107
@benedictodhiambo4107 2 жыл бұрын
How did you determine the values of the constants
@apm
@apm 2 жыл бұрын
They are initially just guesses. With the solver, they are selected by the optimizer to minimize the difference between the predicted and measured values.
@nutakkipradeep2708
@nutakkipradeep2708 4 жыл бұрын
if i know the predicted curve z_pred = x*y after some matrix transformations and i know the actual value z_actual then if i find MSE(z_actual - z_pred) then how can i reduce this error? Anyone can answer
@apm
@apm 4 жыл бұрын
If x or y are adjustable parameters then you typically let the optimizer modify those values to minimize your MSE or SSE.
@user-kai516
@user-kai516 3 жыл бұрын
Hi, I am trying to fit my data with SIR model. Is there any suggest for fitting the data with ODE equations with two coefficients (reaction rate constant)? Thanks in advance.
@apm
@apm 3 жыл бұрын
Here are related examples: apmonitor.com/do/index.php/Main/EstimatorObjective and apmonitor.com/do/index.php/Main/COVID-19Response
@user-kai516
@user-kai516 3 жыл бұрын
@@apm Thanks a lot! I'll give it a try :)
@vchamilka
@vchamilka 4 жыл бұрын
How to estimate parameters of a differential equation by fitting to experimental data?
@apm
@apm 4 жыл бұрын
Here are some examples: apmonitor.com/do/index.php/Main/DynamicEstimation
@oxydol3456
@oxydol3456 2 жыл бұрын
Does this curve fitting function sometimes return wrong answer?
@apm
@apm 2 жыл бұрын
If the solver reports a successful solution then it is at least a local solution. Try different initial guesses or try a different solver such as Python Gekko: apmonitor.com/me575/index.php/Main/NonlinearRegression
@besttom8823
@besttom8823 2 жыл бұрын
How to actually find that correlation function is there a way?
@apm
@apm 2 жыл бұрын
You can either select a form yourself based on knowledge of the system (linear, nonlinear, exponential, logarithmic, etc) or else use concepts from machine learning to determine the correlation. For ML content, see apmonitor.com/pds (Regression section).
@user-hk2uj7jl4g
@user-hk2uj7jl4g 4 жыл бұрын
Does principle is gauss-newton or grading descent ? And I want to learn step by step , no use modules THX😭
@apm
@apm 4 жыл бұрын
It uses a gradient descent. There is step-by-step code here: apmonitor.com/me575/index.php/Main/QuasiNewton There is also an optimization textbook that details the methods: apmonitor.com/me575/index.php/Main/BookChapters
@KR-uy6or
@KR-uy6or 4 жыл бұрын
@@apm so when using ML libraries , python automatically uses one of the approximation principle to find out the global maximum/ minimum ? I mean we don't need to create a function for example to do gradient descent approximation....
@sobersabin
@sobersabin 4 жыл бұрын
hello sir, I did not understand , how you get initial guess.If you have time can you please explain me.I am master student in France.
@apm
@apm 4 жыл бұрын
In most cases you can just use an initial guess of 0 or 1 for all values. In this case, the problem is very nonlinear so it requires a better initial guess. A good way to come up with the initial guess is to try values until the predicted and measured values are close. An optimizer can then find the best parameters if you start with a good initial guess. Sometimes you also need to add bounds to the parameters to get it to converge. A better solver can also help. Here is a tutorial on Python Gekko: apmonitor.com/che263/index.php/Main/PythonDataRegression
@hibadu251
@hibadu251 4 жыл бұрын
Is this Levenberg-Marquardt algorithm??
@apm
@apm 4 жыл бұрын
No, it is an SQP method with a Nonlinear Programming solver. More details on solver methods are at apmonitor.com/me575
@MaddingAlex
@MaddingAlex 4 жыл бұрын
How to find t-statistics, F-statistics and p-values for this regression? Using Python
@apm
@apm 4 жыл бұрын
Here's some related information apmonitor.com/che263/index.php/Main/PythonRegressionStatistics
@basichack6974
@basichack6974 3 жыл бұрын
Hello please. I have done everything in your video. It works every well. But how can I predict the heart rate for the time out of the range? I mean what is now the prediction function I have to call??? Please help me for this task.
@apm
@apm 3 жыл бұрын
You can use the coefficients to build an equation for the prediction.
@apm
@apm 3 жыл бұрын
Try creating a new function for the prediction. More help is here: apmonitor.github.io/begin_python (see module on functions).
@basichack6974
@basichack6974 3 жыл бұрын
Oh yeah I got it. That is so easy. Thanks a lot❤️❤️❤️
@zartadavid2900
@zartadavid2900 3 жыл бұрын
how can I know the chi square?
@apm
@apm 3 жыл бұрын
Here is a good summary www.google.com/amp/s/data-flair.training/blogs/python-linear-regression-chi-square-test/amp/
@cccloud3256
@cccloud3256 3 жыл бұрын
Sorry, but I can't access the data. Can anybody help?
@apm
@apm 3 жыл бұрын
Here is the source code with the data: apmonitor.com/che263/index.php/Main/PythonDataRegression
@ruslanruslan338
@ruslanruslan338 3 жыл бұрын
Thank you very much for your video! By the way, is anybody know how to solve the same issue, but when you have more than one variable. For example Y = a * x1^b + c*x2^d. We have dataset with x1,x2, Y. And how we can optimize a,b,c,d?
@apm
@apm 3 жыл бұрын
Here is something similar: apmonitor.com/me575/index.php/Main/NonlinearRegression
@ruslanruslan338
@ruslanruslan338 3 жыл бұрын
@@apm Thank you very much!!! Yes, it is what I'm looking for.
@HealthyFoodBae_
@HealthyFoodBae_ 3 жыл бұрын
Is this non parametric regression?
@apm
@apm 3 жыл бұрын
No, it has parameters that we adjust to fit the predicted values to the measurements.
@JJGhostHunters
@JJGhostHunters Жыл бұрын
Hello...Can anyone provide any resources for how to perform "nonparametric quantile regression" in Python?
@apm
@apm Жыл бұрын
Does this help? pypi.org/project/vqr/
@JJGhostHunters
@JJGhostHunters Жыл бұрын
@@apm This seems very promising howerver the documentation is not clear how to get started. Perhaps I am making things overly complicated, however I have yet to see any examples in Python or R that will generate the curves shown in Wikipedia. This seems "simple" but apparently it isnt. en.wikipedia.org/wiki/Quantile_regression
Curve Fitting in Python (2022)
24:50
Mr. P Solver
Рет қаралды 89 М.
Curve fitting in Python with curve_fit
51:26
Brant Carlson
Рет қаралды 66 М.
Я нашел кто меня пранкует!
00:51
Аришнев
Рет қаралды 4,3 МЛН
Final muy increíble 😱
00:46
Juan De Dios Pantoja 2
Рет қаралды 53 МЛН
SciPy Beginner's Guide for Optimization
11:03
APMonitor.com
Рет қаралды 293 М.
Unlock the Secret of Nonlinear Curve Fitting - Python LMFIT
18:18
Linear Least Squares to Solve Nonlinear Problems
12:27
The Math Coffeeshop
Рет қаралды 28 М.
Polynomial Regression in Python
20:18
NeuralNine
Рет қаралды 43 М.
SHA: Secure Hashing Algorithm - Computerphile
10:21
Computerphile
Рет қаралды 1,2 МЛН
Genetic Algorithms in Python - Evolution For Optimization
26:10
NeuralNine
Рет қаралды 12 М.
How to: Import, Plot, Fit, and Integrate Data in Python
24:11
Python Nonlinear Equations with Scipy fsolve
13:03
APMonitor.com
Рет қаралды 63 М.
Non-Linear CURVE FITTING using PYTHON
11:37
Phys Whiz
Рет қаралды 76 М.
OZON РАЗБИЛИ 3 КОМПЬЮТЕРА
0:57
Кинг Комп Shorts
Рет қаралды 1,5 МЛН
Samsung Galaxy Unpacked July 2024: Official Replay
1:8:53
Samsung
Рет қаралды 23 МЛН
КРУТОЙ ТЕЛЕФОН
0:16
KINO KAIF
Рет қаралды 4,8 МЛН
iPhone 15 Pro в реальной жизни
24:07
HUDAKOV
Рет қаралды 179 М.