使用 numpy.polynomial.legendre 时,如何获得将输入转换为勒让德多项式参数的函数?
How do I get the function which transforms an input to be the argument of a Legendre polynomial when using numpy.polynomial.legendre?
# import packages we need later
import matplotlib.pyplot as plt
import numpy as np
我在做什么
受此启发 & , I am fitting a series of Legendre polynomials to a time series:
curve1 = \
np.asarray([942.153,353.081,53.088,125.110,140.851,188.170,70.536,-122.473,-369.061,-407.945,88.734,484.334,267.762,65.831,74.010,-55.781,-260.024,-466.830,-524.511,-76.833,-36.779,-117.366,218.578,175.662,185.653,299.285,215.276,546.048,1210.132,3087.326,7052.849,13867.824,27156.939,51379.664,91908.266,148874.563,215825.031,290073.219,369567.781,437031.688])
时间值:
tvals = \
np.asarray([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40])
使用 numpy 的函数:
degree=10
legendrefit_curve1 = np.polynomial.legendre.Legendre.fit(tvals, curve1, deg=degree)
看起来很合身:
# generate points of fitted curve
n=100
fitted_vals_curve1 = legendrefit_curve1.linspace(n=n)
# plot data and fitted curve
plt.scatter(tvals, curve1)
plt.plot(fitted_vals_curve1[0],fitted_vals_curve1[1],c='r')
问题是什么
print(legendrefit_curve1)
returns:
leg([ 36823.85778316 96929.13731379 123557.55165344 112110.13559758
75345.0434688 32377.19460001 -182.38440131 -15562.47475287
-16142.22533582 -8379.06875482 -744.73929814])
但是,我使用的是 Jupyter 笔记本,所以如果我只写 legendrefit_curve1
,而不写 print()
,我会得到一个输出:
(print()
对 Jupyter 输出的影响与这个问题有关。)
显然,print(legendrefit_curve1)
只给出了每个勒让德多项式的系数(与legendrefit_curve1.coef
相同)。
如何获取将 x 变换为每个勒让德多项式的参数的值?
即如何从表达式中获取值:-1.0512820512820513+0.05128205128205128x
:-1.0512820512820513
和 0.05128205128205128
(不只是手动复制它们)?
什么没用
靠this thread我运行:
for attr in dir(legendrefit_curve1):
print('###'+attr+'###')
print(getattr(legendrefit_curve1, attr))
这有一个很长的文本输出,但我没有在其中找到 -1.05
(ctrl-f
),所以这表明没有返回 -1.0512820512820513
值,所以这个方法无效。
通过查看这些数字,我意识到我可以从数学中构建它们。
1/(len(curve1)-1)*2
,即1/39*2
returns:0.05128205128205128
1+1/(len(curve1)-1)*2
即1+1/39*2
returns:`1.05
我们要找的是哪些号码。
我仍然不知道在 Jupyter Notebook 单元格中执行 legendrefit_curve1
时它是如何显示的,但这不是重点。
我不知道为什么上面的公式有效,这可能是关于math.stackexchange.com的问题。
# import packages we need later
import matplotlib.pyplot as plt
import numpy as np
我在做什么
受此启发
curve1 = \
np.asarray([942.153,353.081,53.088,125.110,140.851,188.170,70.536,-122.473,-369.061,-407.945,88.734,484.334,267.762,65.831,74.010,-55.781,-260.024,-466.830,-524.511,-76.833,-36.779,-117.366,218.578,175.662,185.653,299.285,215.276,546.048,1210.132,3087.326,7052.849,13867.824,27156.939,51379.664,91908.266,148874.563,215825.031,290073.219,369567.781,437031.688])
时间值:
tvals = \
np.asarray([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40])
使用 numpy 的函数:
degree=10
legendrefit_curve1 = np.polynomial.legendre.Legendre.fit(tvals, curve1, deg=degree)
看起来很合身:
# generate points of fitted curve
n=100
fitted_vals_curve1 = legendrefit_curve1.linspace(n=n)
# plot data and fitted curve
plt.scatter(tvals, curve1)
plt.plot(fitted_vals_curve1[0],fitted_vals_curve1[1],c='r')
问题是什么
print(legendrefit_curve1)
returns:
leg([ 36823.85778316 96929.13731379 123557.55165344 112110.13559758
75345.0434688 32377.19460001 -182.38440131 -15562.47475287
-16142.22533582 -8379.06875482 -744.73929814])
但是,我使用的是 Jupyter 笔记本,所以如果我只写 legendrefit_curve1
,而不写 print()
,我会得到一个输出:
(print()
对 Jupyter 输出的影响与这个问题有关。)
显然,print(legendrefit_curve1)
只给出了每个勒让德多项式的系数(与legendrefit_curve1.coef
相同)。
如何获取将 x 变换为每个勒让德多项式的参数的值?
即如何从表达式中获取值:-1.0512820512820513+0.05128205128205128x
:-1.0512820512820513
和 0.05128205128205128
(不只是手动复制它们)?
什么没用
靠this thread我运行:
for attr in dir(legendrefit_curve1):
print('###'+attr+'###')
print(getattr(legendrefit_curve1, attr))
这有一个很长的文本输出,但我没有在其中找到 -1.05
(ctrl-f
),所以这表明没有返回 -1.0512820512820513
值,所以这个方法无效。
通过查看这些数字,我意识到我可以从数学中构建它们。
1/(len(curve1)-1)*2
,即1/39*2
returns:0.05128205128205128
1+1/(len(curve1)-1)*2
即1+1/39*2
returns:`1.05
我们要找的是哪些号码。
我仍然不知道在 Jupyter Notebook 单元格中执行 legendrefit_curve1
时它是如何显示的,但这不是重点。
我不知道为什么上面的公式有效,这可能是关于math.stackexchange.com的问题。