为什么拟合多项式比改变多项式基础更快?

时间:2017-04-07 10:39:49

标签: performance numpy curve-fitting polynomials

给定区间[-1,1]上的一些数据点和这些点的最佳拟Chebyshev多项式,我想将切比雪夫多项式转换为勒让德多项式。

有两种方法可以做到,如下面的代码所示。直接的方法是在Chebyshev多项式上调用convert(kind = Legendre),这需要19.591秒。另一种方法是在数据点上调用Legendre.fit,这只需要3.356秒。

import numpy as np
from numpy.polynomial import Chebyshev, Legendre
x = np.linspace(-1, 1, 1000)
y = 1.0 / (1 + x ** 2) + 1e-3 * np.random.random(1000)
T = Chebyshev.fit(x, y, 99)

from timeit import timeit
timeit("T.convert(kind = Legendre)", setup = "from __main__ import x, y, T, Legendre",
       number = 200)
timeit("Legendre.fit(x, y, 99)", setup = "from __main__ import x, y, Legendre",
       number = 200)

问题:为什么Legendre.fitconvert(kind = Legendre)快得多?我做错了吗?

0 个答案:

没有答案
相关问题