无法在Python中打印曲线拟合

发布于 2025-01-31 01:24:07 字数 1380 浏览 2 评论 0原文

嗨,我有以下数据框。

    Years  Count
0    2003     99
1    2004    116
2    2005    101
3    2006    115
4    2007    108
5    2008    122
6    2009    162
7    2010    152
8    2011    221
9    2012    252
10   2013    351
11   2014    480
12   2015    409
13   2016    384
14   2017    581
15   2018    406
16   2019     18

我正在尝试为此绘制曲线拟合。但是我不确定我犯了什么错误。 它不打印曲线拟合。您可以将上面给定的数据框放在标记的代码位置中。 初始化它确实正确绘制了图表,但是之后我什至尝试重新启动Anaconda,但仍然没有打印。这可能是一个很小的错误...

from numpy import arange
from pandas import read_csv
from scipy.optimize import curve_fit
from matplotlib import pyplot
import pandas as pd
import matplotlib.pyplot as plt

##(place above given dataframe here)
dff.plot(x='Years',y='Count', marker = 'o')
print(dff)


x= dff.iloc[:, 0]
y= dff.iloc[:, 1]


def objective(x, a, b, c, d, e, f):
    return (a * x) + (b * x**2) + (c * x**3) + (d * x**4) + (e * x**5) + f

popt, _ = curve_fit(objective, x, y)           
a, b, c, d, e, f = popt                         

pyplot.scatter(x, y)                        
x_line = arange(min(x), max(x), 1)        
y_line = objective(x_line, a, b, c, d, e, f)    
pyplot.plot(x_line, y_line, '--', color='red') 
pyplot.show()

​href =“ https://i.sstatic.net/ubcy0.png” rel =“ nofollow noreferrer”>

Hi i have following dataframe.

    Years  Count
0    2003     99
1    2004    116
2    2005    101
3    2006    115
4    2007    108
5    2008    122
6    2009    162
7    2010    152
8    2011    221
9    2012    252
10   2013    351
11   2014    480
12   2015    409
13   2016    384
14   2017    581
15   2018    406
16   2019     18

I am trying to plot curve fitting for this one. but i am not sure what mistake i m making.
Its not printing curve fitting. You can place the above given dataframe in the code marked location.
initiall it did plot the graph correctly but after that it didn't i have even try to restart anaconda but still its not printing.. It could be some very minor mistake...

from numpy import arange
from pandas import read_csv
from scipy.optimize import curve_fit
from matplotlib import pyplot
import pandas as pd
import matplotlib.pyplot as plt

##(place above given dataframe here)
dff.plot(x='Years',y='Count', marker = 'o')
print(dff)


x= dff.iloc[:, 0]
y= dff.iloc[:, 1]


def objective(x, a, b, c, d, e, f):
    return (a * x) + (b * x**2) + (c * x**3) + (d * x**4) + (e * x**5) + f

popt, _ = curve_fit(objective, x, y)           
a, b, c, d, e, f = popt                         

pyplot.scatter(x, y)                        
x_line = arange(min(x), max(x), 1)        
y_line = objective(x_line, a, b, c, d, e, f)    
pyplot.plot(x_line, y_line, '--', color='red') 
pyplot.show()

enter image description here

Required Plot

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文