'Unable to print Curve fitting in python
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|


