numpy polyfit 是如何工作的?

How does numpy polyfit work?

我已经在 Bluemix Spark 服务中创建了 "Precipitation Analysis" 示例 Jupyter Notebook。

笔记本Link:https://console.ng.bluemix.net/data/notebooks/3ffc43e2-d639-4895-91a7-8f1599369a86/view?access_token=effff68dbeb5f9fc0d2df20cb51bffa266748f2d177b730d5d096cb54b35e5f0

所以在 In[34] 和 In[35] 中(你必须滚动很多次)他们使用 numpy polyfit 来计算给定温度数据的趋势。但是,我不明白怎么用。

有人可以解释一下吗?

问题已在 Developerworks 上得到解答:- https://developer.ibm.com/answers/questions/282350/how-does-numpy-polyfit-work.html

我会尽力解释每一个:-

index = chile[chile>0.0].index => 此语句给出了智利 python 系列中指数大于 0.0.

的所有年份
 fit = np.polyfit(index.astype('int'), chile[index].values,1)

这是 polyfit 函数调用,它找出通过向量提供的索引(年)处的给定 x(年)和 y(年降水量)值的多项式拟合系数(斜率和截距)。

 print "slope: " + str(fit[0])

下面的代码简单地绘制了参考直线的数据点以显示趋势

 plt.plot(index, chile[index],'.')

特别是在下面的语句中,第二个参数实际上是表示 y 的直线方程 "y = mx + b" 其中 m 是斜率,b 是我们在上面使用 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()

希望对您有所帮助。

谢谢,查尔斯。