Python在数组的每一列上应用函数

2024-05-13 17:04:29 发布

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

我尝试使用函数InterpolatedUnivariateSpline作为:

from scipy.interpolate import InterpolatedUnivariateSpline
A = np.array([1,2,3,4,5])
B = np.array([11,34,56,78,19])
C = np.random.normal(0, 1, (500, 30))
model = InterpolatedUnivariateSpline(A, B, k = 1)
C2 = model(C) #fails with error object too deep for desired array
C2 = model(C[:,0]) #works but is not useful as I need inter-/extra-polation for entire C

那么,如何将函数应用于数组C的所有元素呢

编辑:scipy版本:0.13.2


Tags: 函数fromimportformodelwithnprandom
2条回答

如果我理解正确,你可以使用列表理解:

C2 = [model(i) for i in C]

它将在所有元素上运行“model”并返回列表

scipy升级到版本0.16.1,它将适用于多维数组,即:

C2 = model(C)

工作。你知道吗

相关问题 更多 >