无法在Python中打印曲线拟合
嗨,我有以下数据框。
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()
Required Plot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论