[SciPy-User] Multivariate linear (bilinear) fit

Charles R Harris charlesr.harris at gmail.com
Wed Apr 19 20:12:49 EDT 2017


On Wed, Apr 19, 2017 at 4:38 PM, Joe Kington <joferkington at gmail.com> wrote:

> They're relatively recent additions, but numpy.polynomial.polyvander2d and
> numpy.polynomial.polyval2d should also do what you want, unless I'm
> misunderstanding the problem.
>
> https://docs.scipy.org/doc/numpy/reference/generated/
> numpy.polynomial.polynomial.polyvander2d.html#numpy.polynomial.polynomial.
> polyvander2d
> https://docs.scipy.org/doc/numpy/reference/generated/
> numpy.polynomial.polynomial.polyval2d.html#numpy.polynomial.polynomial.
> polyval2d
>
> You can also do things like (you could generalize this to N-dimensions, as
> well):
>
> def polyfit2d(x, y, z, order=3):
>     ncols = (order + 1)**2
>     G = np.zeros((x.size, ncols))
>     ij = itertools.product(range(order+1), range(order+1))
>     for k, (i,j) in enumerate(ij):
>         G[:,k] = x**i * y**j
>     m, _, _, _ = np.linalg.lstsq(G, z)
>     return m
> def polyval2d(x, y, m):
>     order = int(np.sqrt(len(m))) - 1
>     ij = itertools.product(range(order+1), range(order+1))
>     z = np.zeros_like(x)
>     for a, (i,j) in zip(m, ij):
>         z += a * x**i * y**j
>     return z
>
>
>
I think the "bilinear" is a mistake, as bilinear usually means terms of
degree two. AFAICT, this question is just about multivariate linear fits
only

<snip>

Chuck

>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-user/attachments/20170419/9b597d3e/attachment-0001.html>


More information about the SciPy-User mailing list