curve_fit function not fitting to original data perfectly












1















Hi I have a set of data and I fitted my data with the curve_fit function
but the line does not describe the original dataset good enough.
The curve_fit function is not close to the orginal data.



the x array has following data:



[0. 0.025 0.10333333 0.1175 0.164 0.22 0.27571429 0.27625 0.33333333 0.379 0.40545455 0.43416667 0.47769231 0.52571429 0.528 0.538125 0.56470588 0.5577777 0.59263158 0.6065 0.61190476 0.62545455 ...] 


y array looks like this:



[1. 1.95 2.83 3.73 4.57 5.32 5.97 6.81 7.35 7.86 8.5 9.09 9.4 9.83 10.41 11. 11.34 11.8  ...]


My curve_fit func:



def func(x, a, b, c,):
return a*np.exp(-b*x)+c

popt, pcov = curve_fit(func,x,y, maxfev=10000)

plt.plot(x, y, ls="none", marker='.', color='grey')
plt.plot(x,func(x, *popt),'-')

plt.title("my curve")
plt.legend()
plt.show()


Below is my plot:
enter image description here










share|improve this question























  • One could think about weighting the fit by the inverse of the point density. Also one could fit x(y) instead of y(x).

    – ImportanceOfBeingErnest
    Nov 23 '18 at 20:26


















1















Hi I have a set of data and I fitted my data with the curve_fit function
but the line does not describe the original dataset good enough.
The curve_fit function is not close to the orginal data.



the x array has following data:



[0. 0.025 0.10333333 0.1175 0.164 0.22 0.27571429 0.27625 0.33333333 0.379 0.40545455 0.43416667 0.47769231 0.52571429 0.528 0.538125 0.56470588 0.5577777 0.59263158 0.6065 0.61190476 0.62545455 ...] 


y array looks like this:



[1. 1.95 2.83 3.73 4.57 5.32 5.97 6.81 7.35 7.86 8.5 9.09 9.4 9.83 10.41 11. 11.34 11.8  ...]


My curve_fit func:



def func(x, a, b, c,):
return a*np.exp(-b*x)+c

popt, pcov = curve_fit(func,x,y, maxfev=10000)

plt.plot(x, y, ls="none", marker='.', color='grey')
plt.plot(x,func(x, *popt),'-')

plt.title("my curve")
plt.legend()
plt.show()


Below is my plot:
enter image description here










share|improve this question























  • One could think about weighting the fit by the inverse of the point density. Also one could fit x(y) instead of y(x).

    – ImportanceOfBeingErnest
    Nov 23 '18 at 20:26
















1












1








1








Hi I have a set of data and I fitted my data with the curve_fit function
but the line does not describe the original dataset good enough.
The curve_fit function is not close to the orginal data.



the x array has following data:



[0. 0.025 0.10333333 0.1175 0.164 0.22 0.27571429 0.27625 0.33333333 0.379 0.40545455 0.43416667 0.47769231 0.52571429 0.528 0.538125 0.56470588 0.5577777 0.59263158 0.6065 0.61190476 0.62545455 ...] 


y array looks like this:



[1. 1.95 2.83 3.73 4.57 5.32 5.97 6.81 7.35 7.86 8.5 9.09 9.4 9.83 10.41 11. 11.34 11.8  ...]


My curve_fit func:



def func(x, a, b, c,):
return a*np.exp(-b*x)+c

popt, pcov = curve_fit(func,x,y, maxfev=10000)

plt.plot(x, y, ls="none", marker='.', color='grey')
plt.plot(x,func(x, *popt),'-')

plt.title("my curve")
plt.legend()
plt.show()


Below is my plot:
enter image description here










share|improve this question














Hi I have a set of data and I fitted my data with the curve_fit function
but the line does not describe the original dataset good enough.
The curve_fit function is not close to the orginal data.



the x array has following data:



[0. 0.025 0.10333333 0.1175 0.164 0.22 0.27571429 0.27625 0.33333333 0.379 0.40545455 0.43416667 0.47769231 0.52571429 0.528 0.538125 0.56470588 0.5577777 0.59263158 0.6065 0.61190476 0.62545455 ...] 


y array looks like this:



[1. 1.95 2.83 3.73 4.57 5.32 5.97 6.81 7.35 7.86 8.5 9.09 9.4 9.83 10.41 11. 11.34 11.8  ...]


My curve_fit func:



def func(x, a, b, c,):
return a*np.exp(-b*x)+c

popt, pcov = curve_fit(func,x,y, maxfev=10000)

plt.plot(x, y, ls="none", marker='.', color='grey')
plt.plot(x,func(x, *popt),'-')

plt.title("my curve")
plt.legend()
plt.show()


Below is my plot:
enter image description here







python matplotlib






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 20:01









Zara ArshadZara Arshad

294




294













  • One could think about weighting the fit by the inverse of the point density. Also one could fit x(y) instead of y(x).

    – ImportanceOfBeingErnest
    Nov 23 '18 at 20:26





















  • One could think about weighting the fit by the inverse of the point density. Also one could fit x(y) instead of y(x).

    – ImportanceOfBeingErnest
    Nov 23 '18 at 20:26



















One could think about weighting the fit by the inverse of the point density. Also one could fit x(y) instead of y(x).

– ImportanceOfBeingErnest
Nov 23 '18 at 20:26







One could think about weighting the fit by the inverse of the point density. Also one could fit x(y) instead of y(x).

– ImportanceOfBeingErnest
Nov 23 '18 at 20:26














1 Answer
1






active

oldest

votes


















3














As far as I can see you are trying to fit an exponential curve to your data. Most of your data is concentrated on the upper right and hence the algorithm tries to fit it best-possible to that part.






share|improve this answer
























  • thanks , but how can I fit my curve for the lower left part?

    – Zara Arshad
    Nov 23 '18 at 20:12








  • 1





    If you know that your data will look like the example you've provided, I suggest using a polynomial model and a least-squares fit. This works reasonably well in practice. Edit: If you insist on an exponential model, maybe logarithmize your data and try a linear model then.

    – Thomas Lang
    Nov 23 '18 at 20:14













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452361%2fcurve-fit-function-not-fitting-to-original-data-perfectly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














As far as I can see you are trying to fit an exponential curve to your data. Most of your data is concentrated on the upper right and hence the algorithm tries to fit it best-possible to that part.






share|improve this answer
























  • thanks , but how can I fit my curve for the lower left part?

    – Zara Arshad
    Nov 23 '18 at 20:12








  • 1





    If you know that your data will look like the example you've provided, I suggest using a polynomial model and a least-squares fit. This works reasonably well in practice. Edit: If you insist on an exponential model, maybe logarithmize your data and try a linear model then.

    – Thomas Lang
    Nov 23 '18 at 20:14


















3














As far as I can see you are trying to fit an exponential curve to your data. Most of your data is concentrated on the upper right and hence the algorithm tries to fit it best-possible to that part.






share|improve this answer
























  • thanks , but how can I fit my curve for the lower left part?

    – Zara Arshad
    Nov 23 '18 at 20:12








  • 1





    If you know that your data will look like the example you've provided, I suggest using a polynomial model and a least-squares fit. This works reasonably well in practice. Edit: If you insist on an exponential model, maybe logarithmize your data and try a linear model then.

    – Thomas Lang
    Nov 23 '18 at 20:14
















3












3








3







As far as I can see you are trying to fit an exponential curve to your data. Most of your data is concentrated on the upper right and hence the algorithm tries to fit it best-possible to that part.






share|improve this answer













As far as I can see you are trying to fit an exponential curve to your data. Most of your data is concentrated on the upper right and hence the algorithm tries to fit it best-possible to that part.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 20:04









Thomas LangThomas Lang

44627




44627













  • thanks , but how can I fit my curve for the lower left part?

    – Zara Arshad
    Nov 23 '18 at 20:12








  • 1





    If you know that your data will look like the example you've provided, I suggest using a polynomial model and a least-squares fit. This works reasonably well in practice. Edit: If you insist on an exponential model, maybe logarithmize your data and try a linear model then.

    – Thomas Lang
    Nov 23 '18 at 20:14





















  • thanks , but how can I fit my curve for the lower left part?

    – Zara Arshad
    Nov 23 '18 at 20:12








  • 1





    If you know that your data will look like the example you've provided, I suggest using a polynomial model and a least-squares fit. This works reasonably well in practice. Edit: If you insist on an exponential model, maybe logarithmize your data and try a linear model then.

    – Thomas Lang
    Nov 23 '18 at 20:14



















thanks , but how can I fit my curve for the lower left part?

– Zara Arshad
Nov 23 '18 at 20:12







thanks , but how can I fit my curve for the lower left part?

– Zara Arshad
Nov 23 '18 at 20:12






1




1





If you know that your data will look like the example you've provided, I suggest using a polynomial model and a least-squares fit. This works reasonably well in practice. Edit: If you insist on an exponential model, maybe logarithmize your data and try a linear model then.

– Thomas Lang
Nov 23 '18 at 20:14







If you know that your data will look like the example you've provided, I suggest using a polynomial model and a least-squares fit. This works reasonably well in practice. Edit: If you insist on an exponential model, maybe logarithmize your data and try a linear model then.

– Thomas Lang
Nov 23 '18 at 20:14






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53452361%2fcurve-fit-function-not-fitting-to-original-data-perfectly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin