numpy的PolyFit和PolyVal的多维? [英] NumPy PolyFit and PolyVal in Multiple Dimensions?

查看:1607
本文介绍了numpy的PolyFit和PolyVal的多维?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设被整形为一个二​​维阵列,每行是一个观测组观察的n维阵列。使用这种方法整形, np.polyfit 可以计算出整个ndarray 2次拟合系数(矢量):

Assume an n-dimensional array of observations that are reshaped to be a 2d-array with each row being one observation set. Using this reshape approach, np.polyfit can compute 2nd order fit coefficients for the entire ndarray (vectorized):

fit = np.polynomial.polynomialpolyfit(X, Y, 2)

,其中Y是形状(304000,21)和X是一个载体。这导致系数的(304000,3)阵列,适合。

where Y is shape (304000, 21) and X is a vector. This results in a (304000,3) array of coefficients, fit.

使用迭代器就可以调用 np.polyval(FIT,X)的每一行。这是低效,当向量化的方法可能存在。可以在配合结果被应用到不反复整个观察阵列?如果是这样,怎么样?

Using an iterator it is possible to call np.polyval(fit, X) for each row. This is inefficient when a vectorized approach may exist. Could the fit result be applied to the entire observation array without iterating? If so, how?

这是一起的<一个行href=\"http://stackoverflow.com/questions/19282429/regression-along-a-dimension-in-a-numpy-array\">this SO 问题。

推荐答案

np.polynomial.polynomial.polyval 需要多维数组系数:

>>> x = np.random.rand(100)
>>> y = np.random.rand(100, 25)
>>> fit = np.polynomial.polynomial.polyfit(x, y, 2)
>>> fit.shape # 25 columns of 3 polynomial coefficients
(3L, 25L)
>>> xx = np.random.rand(50)
>>> interpol = np.polynomial.polynomial.polyval(xx, fit)
>>> interpol.shape # 25 rows, each with 50 evaluations of the polynomial
(25L, 50L)

当然,

>>> np.all([np.allclose(np.polynomial.polynomial.polyval(xx, fit[:, j]),
...                     interpol[j]) for j in range(25)])
True

这篇关于numpy的PolyFit和PolyVal的多维?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆