使用numpy.polynomial.legendre时,如何获取将输入转换为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 matplotlib.pyplot as plt
import numpy as np
我在做什么
受此
< hr />
有什么问题
print(legendrefit_curve1)
返回:
leg([36823.85778316 96929.13731379 123557.55165344 112110.13559758
75345.0434688 32377.19460001 -182.38440131 -15562.47475287
-16142.22533582 -8379.06875482 -744.73929814])
b $ c>
但是,我使用的是Jupyter笔记本,所以如果我只写 legendrefit_curve1
,而没有
(什么区别 print()
使Jupyter的输出与此问题有关。)
很显然, print(legendrefit_curve1)
仅给出了每个勒让德多项式的系数(相同与 legendrefit_curve1.coef
)。
如何获取将x转换为每个Legendre多项式的参数的值?
即如何从ex获取值压缩: -1.0512820512820513 + 0.05128205128205128x
: -1.0512820512820513
和 0.05128205128205128
(不只是手动复制它们)?
什么不起作用
依靠此线程我运行:
用于目录中的attr(legendrefit_curve1):
print('###'+ attr +'###')
print(getattr(legendrefit_curve1,attr))
此文本输出很长,但是我没有在其中找到 -1.05
( ctrl-f
),因此表明未返回 -1.0512820512512820513
值,因此该方法不会
通过查看这些数字,我意识到我可以用数学来构造它们。
1 /(len(curve1)-1)* 2
,即 1/39 * 2
返回: 0.05128205128205128
1 + 1 /(len(curve1)-1 )* 2
即 1 + 1/39 * 2
返回:`1.05
其中是我们要寻找的数字。
我仍然不知道执行 legendrefit_curve1时如何显示它
在Jupyter Notebook单元中,但这没什么意义。
我不知道为什么上面的公式有效,可能是在 math.stackexchange.com 上的问题。
# import packages we need later
import matplotlib.pyplot as plt
import numpy as np
What I am doing
Inspired by this question & answer, 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])
The time values:
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])
Using a numpy's function:
degree=10
legendrefit_curve1 = np.polynomial.legendre.Legendre.fit(tvals, curve1, deg=degree)
The fit seems pretty good:
# 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')
What's the question
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])
However, I am using a Jupyter notebook, so if I just write legendrefit_curve1
, without print()
, I get an output:
(What difference print()
makes to Jupyter's output is related to this question.)
Clearly, print(legendrefit_curve1)
only gave the coefficients of each Legendre polynomial (same with legendrefit_curve1.coef
).
How do I get the values which transform x to be the argument of each Legendre polynomial?
ie how to obtain the values from the expression: -1.0512820512820513+0.05128205128205128x
: -1.0512820512820513
and 0.05128205128205128
(without just copying them manually)?
What didn't work
Relying on this thread I run:
for attr in dir(legendrefit_curve1):
print('###'+attr+'###')
print(getattr(legendrefit_curve1, attr))
This had a long text output, but I did not find -1.05
in it (ctrl-f
), so that suggest that the -1.0512820512820513
value did not get returned, so this method doesn't work.
By looking at those numbers I realized I can construct them from math.
1/(len(curve1)-1)*2
, ie 1/39*2
returns: 0.05128205128205128
1+1/(len(curve1)-1)*2
ie 1+1/39*2
returns: `1.05
Which are the numbers we were looking for.
I still don't know how it is displayed when executing legendrefit_curve1
in a Jupyter Notebook cell, but that is less of the point.
I don't know why the formula above works, it'll probably be a question on math.stackexchange.com.
这篇关于使用numpy.polynomial.legendre时,如何获取将输入转换为Legendre多项式的参数的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!