python polyfit,numpy的PolyFit和PolyVal的多维?

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)

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

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?

This is along the lines of this SO question.

解决方案

np.polynomial.polynomial.polyval takes multidimensional coefficient arrays:

>>> 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)

And of course:

>>> np.all([np.allclose(np.polynomial.polynomial.polyval(xx, fit[:, j]),

... interpol[j]) for j in range(25)])

True

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值