2

我有两个大的多维数组:Y携带 50 万个对象的三个测量值(例如shape=(500000,3))并且X具有相同的形状,但包含Y测量的位置。

首先,我希望每一行都包含一个对象,以拟合一个多项式方程。我知道迭代数组非常慢,但我目前正在做的是:

fit = array([polyfit(X[i],Y[i],deg) for i in xrange(obs.shape[0])])

我的问题是:是否有可能在不显式迭代它们的情况下拟合两个数组的每一行?

4

2 回答 2

1

可以在不沿第一轴迭代的情况下这样做。但是,您的第二个轴相当短(只有 3 个),您实际上可以拟合不超过 2 个系数。

In [67]:

import numpy as np
import scipy.optimize as so

In [68]:

def MD_ployError(p, x, y):
    '''if x has the shape of (n,m), y must be (n,m), p must be (n*p, ), where p is degree'''
    #d is no. of degree
    p_rshp=p.reshape((x.shape[0], -1))
    f=y*1.
    for i in range(p_rshp.shape[1]):
        f-=p_rshp[:,i][:,np.newaxis]*(x**i)
    return (f**2).sum()

In [69]:

X=np.random.random((100, 6))
Y=4+2*X+3*X*X
P=(np.zeros((100,3))+[1,1,1]).ravel()

In [70]:

MD_ployError(P, X, Y)

Out[70]:
11012.2067606684

In [71]:

R=so.fmin_slsqp(MD_ployError, P, args=(X, Y))
Iteration limit exceeded    (Exit mode 9) #you can increase iteration limit, but the result is already good enough.
            Current function value: 0.00243784856039
            Iterations: 101
            Function evaluations: 30590
            Gradient evaluations: 101

In [72]:

R.reshape((100, -1))

Out[72]:
array([[ 3.94488512,  2.25402422,  2.74773571],
       [ 4.00474864,  1.97966551,  3.02010015],
       [ 3.99919559,  2.0032741 ,  2.99753804],
..............................................)
于 2014-03-19T16:06:49.020 回答
0

是的,如果您使用新的 numpy polyfit np.polynomial而不是旧的np.polyfit

X = np.arange(3)
Y = np.random.rand(10000, 3)

fit = np.array([np.polyfit(X, y, 2) for y in Y])
fits = np.polynomial.polynomial.polyfit(X, Y.T, 2)

assert np.allclose(fit.T[::-1], fits)

定时:

In [692]: timeit fit = np.array([np.polyfit(X, y, 2) for y in Y])
 1 loops, best of 3: 2.22 s per loop

In [693]:  timeit fits = np.polynomial.polynomial.polyfit(X, Y.T, 2)
100 loops, best of 3: 3.63 ms per loop
于 2014-03-19T16:09:50.527 回答