如何使用 Numpy.polyfit 绘制趋势
how to use Numpy.polyfit to plot trend
感谢用户 Eduard Ilyasov 几天前帮助我
现在我得到了一些结果,但我很难理解这些
我正在尝试计算从 1979 年到 2016 年的温度趋势。
#calculate trend
####numpy.ployfit
nmon = nyr * 12
tdum = MA.arange(0,nmon)
ntimes, ny, nx = tempF.shape
#ntimes is time, ny is latitude, nx is longitude
print tempF.shape
trend = MA.zeros((ny,nx), dtype='2f')
#trend = MA.zeros((ny,nx),dtype=float)
print trend.shape
for y in range (0,ny):
for x in range (0,nx):
trend[y,x]= numpy.polyfit(tdum, tempF[:,y,x],1)
print trend.shape
print trend
这些是结果:
(
(456, 241, 480)
(241, 480, 2)
(241, 480, 2)
[[[ 0.00854342 -1.94362879]
[ 0.00854342 -1.94362879]
[ 0.00854342 -1.94362879]
...,
[ 0.00854342 -1.94362879]
[ 0.00854342 -1.94362879]
[ 0.00854342 -1.94362879]]
[[ 0.00824162 -1.87496781]
[ 0.00824792 -1.87640166]
[ 0.00825524 -1.87806702]
...,
[ 0.00822667 -1.87156749]
[ 0.00823172 -1.87271607]
[ 0.0082366 -1.87382615]]
[[ 0.00767854 -1.7468679 ]
[ 0.00769076 -1.74964726]
[ 0.00770384 -1.75262356]
...,
[ 0.00764879 -1.74010038]
[ 0.00765911 -1.74244869]
[ 0.00766829 -1.74453557]]
...,
[[-0.0025295 0.57546186]
[-0.00252633 0.57474071]
[-0.00252274 0.57392275]
...,
[-0.00253488 0.57668549]
[-0.00253269 0.57618785]
[-0.00253125 0.57585901]]
[[-0.00315533 0.71783835]
[-0.00315261 0.71721852]
[-0.00314936 0.71648043]
...,
[-0.00315671 0.71815109]
[-0.00315621 0.71803892]
[-0.00315584 0.71795386]]
[[-0.00309109 0.7032221 ]
[-0.00309109 0.7032221 ]
[-0.00309109 0.7032221 ]
...,
[-0.00309109 0.7032221 ]
[-0.00309109 0.7032221 ]
[-0.00309109 0.7032221 ]]]
我理解每个括号中的第二个值应该是趋势值的系数,但我不了解趋势的形状。每个 [] 中第一个数字的含义是什么?我应该使用什么趋势值来绘制趋势图?
如果你能帮助我,非常感谢
如果您进一步阅读 numpy.polyfit()
的文档,您将看到此函数的定义
The solution minimizes the squared error
E = \sum_{j=0}^k |p(x_j) - y_j|^2
in the equations:
x[0]**n * p[0] + ... + x[0] * p[n-1] + p[n] = y[0]
x[1]**n * p[0] + ... + x[1] * p[n-1] + p[n] = y[1]
...
x[k]**n * p[0] + ... + x[k] * p[n-1] + p[n] = y[k]
对于趋势是线性的情况,这意味着 trend[y,x,0]
是 trend 的值(也称为 slope) 和 trend[y,x,1]
是 截距 .
为了便于说明,请考虑以下示例:
import numpy as np
from matplotlib import pyplot as plt
N = 10
xs = np.random.random(N)
ys = np.random.random(N)
trend = np.polyfit(xs,ys,1)
plt.plot(xs,ys,'o')
trendpoly = np.poly1d(trend)
plt.plot(xs,trendpoly(xs))
感谢用户 Eduard Ilyasov 几天前帮助我
现在我得到了一些结果,但我很难理解这些
我正在尝试计算从 1979 年到 2016 年的温度趋势。
#calculate trend
####numpy.ployfit
nmon = nyr * 12
tdum = MA.arange(0,nmon)
ntimes, ny, nx = tempF.shape
#ntimes is time, ny is latitude, nx is longitude
print tempF.shape
trend = MA.zeros((ny,nx), dtype='2f')
#trend = MA.zeros((ny,nx),dtype=float)
print trend.shape
for y in range (0,ny):
for x in range (0,nx):
trend[y,x]= numpy.polyfit(tdum, tempF[:,y,x],1)
print trend.shape
print trend
这些是结果:
(
(456, 241, 480)
(241, 480, 2)
(241, 480, 2)
[[[ 0.00854342 -1.94362879]
[ 0.00854342 -1.94362879]
[ 0.00854342 -1.94362879]
...,
[ 0.00854342 -1.94362879]
[ 0.00854342 -1.94362879]
[ 0.00854342 -1.94362879]]
[[ 0.00824162 -1.87496781]
[ 0.00824792 -1.87640166]
[ 0.00825524 -1.87806702]
...,
[ 0.00822667 -1.87156749]
[ 0.00823172 -1.87271607]
[ 0.0082366 -1.87382615]]
[[ 0.00767854 -1.7468679 ]
[ 0.00769076 -1.74964726]
[ 0.00770384 -1.75262356]
...,
[ 0.00764879 -1.74010038]
[ 0.00765911 -1.74244869]
[ 0.00766829 -1.74453557]]
...,
[[-0.0025295 0.57546186]
[-0.00252633 0.57474071]
[-0.00252274 0.57392275]
...,
[-0.00253488 0.57668549]
[-0.00253269 0.57618785]
[-0.00253125 0.57585901]]
[[-0.00315533 0.71783835]
[-0.00315261 0.71721852]
[-0.00314936 0.71648043]
...,
[-0.00315671 0.71815109]
[-0.00315621 0.71803892]
[-0.00315584 0.71795386]]
[[-0.00309109 0.7032221 ]
[-0.00309109 0.7032221 ]
[-0.00309109 0.7032221 ]
...,
[-0.00309109 0.7032221 ]
[-0.00309109 0.7032221 ]
[-0.00309109 0.7032221 ]]]
我理解每个括号中的第二个值应该是趋势值的系数,但我不了解趋势的形状。每个 [] 中第一个数字的含义是什么?我应该使用什么趋势值来绘制趋势图?
如果你能帮助我,非常感谢
如果您进一步阅读 numpy.polyfit()
的文档,您将看到此函数的定义
The solution minimizes the squared error
E = \sum_{j=0}^k |p(x_j) - y_j|^2
in the equations:
x[0]**n * p[0] + ... + x[0] * p[n-1] + p[n] = y[0] x[1]**n * p[0] + ... + x[1] * p[n-1] + p[n] = y[1] ... x[k]**n * p[0] + ... + x[k] * p[n-1] + p[n] = y[k]
对于趋势是线性的情况,这意味着 trend[y,x,0]
是 trend 的值(也称为 slope) 和 trend[y,x,1]
是 截距 .
为了便于说明,请考虑以下示例:
import numpy as np
from matplotlib import pyplot as plt
N = 10
xs = np.random.random(N)
ys = np.random.random(N)
trend = np.polyfit(xs,ys,1)
plt.plot(xs,ys,'o')
trendpoly = np.poly1d(trend)
plt.plot(xs,trendpoly(xs))