Comments and answers for "How does numpy polyfit work?"
https://developer.ibm.com/answers/questions/282350/how-does-numpy-polyfit-work.html
The latest comments and answers for the question "How does numpy polyfit work?"Answer by Charles C Gomes
https://developer.ibm.com/answers/answers/282920/view.html
Hi,
I will try to explain each of this:-
`index = chile[chile>0.0].index` => this statements gives out all the years which are indices in chile python series which are greater than 0.0.
fit = np.polyfit(index.astype('int'), chile[index].values,1)
This is polyfit function call which find out ploynomial fitting coefficient(slope and intercept) for the given x(years) and y(precipitation on year) values at index(years) supplied through the vectors.
print "slope: " + str(fit[0])
The below code simply plots the datapoints referenced to straight line to show the trend
plt.plot(index, chile[index],'.')
Perticularly in the below statement the second argument is actually straight line equation to represent y which is
"y = mx + b" where m is the slope and b is intercept that we found out above using polyfit.
plt.plot(index, fit[0]*index.astype('int') + fit[1], '-', color='red')
plt.title("Precipitation Trend for Chile")
plt.xlabel("Year")
plt.ylabel("Precipitation (million cubic meters)")
plt.show()
I hope that helps.
Thanks,
Charles.Mon, 20 Jun 2016 20:16:31 GMTCharles C Gomes