如何使用样条插值(scipy)优化numpy循环

2024-06-25 23:24:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试优化numpy中的一个循环。现在循环以26秒的速度运行。我想知道这次是否可以减少

import numpy as np
from scipy import interpolate
import numpy.matlib as matlib


I=10000
T=10000
ct = np.zeros((I,T))
x = matlib.repmat(np.linspace(0,25,I).reshape(I,1),1,T) 
y = np.random.uniform(0, 10, (I,T))
x1 = matlib.repmat(np.linspace(2,20,I).reshape(I,1),1,T) 

tck0 = interpolate.splrep(x[:,0], y[:,0], s=0)
tck1 = interpolate.splrep(x[:,1], y[:,1], s=0)

for t in range(T):
    ct[:,t] = (y[:,t]<=5)*interpolate.splev(x1[:,t], tck0, der=0, ext=0) + \
              (y[:,t]>5)*interpolate.splev(x1[:,t], tck1, der=0, ext=0)

Tags: importnumpyasnpx1ctinterpolatelinspace