我一直在尝试优化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)
您可以使用splrep输出构造一个
BSpline
实例,并使用单个向量化调用代替splev循环相关问题 更多 >
编程相关推荐