简体   繁体   English

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

[英]Python applying function over each column of the array

I am trying to use the function InterpolatedUnivariateSpline as: 我正在尝试将函数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

So, how do I apply the function to all elements of array C 因此,如何将函数应用于数组C的所有元素

Edit: scipy version: 0.13.2 编辑:科学版:0.13.2

If I understand correctly, you could just use list comprehension: 如果我理解正确,则可以使用列表理解:

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

It will run "model" on all elements and return the list 它将在所有元素上运行“模型”并返回列表

Upgrade your scipy to version 0.16.1 and it will work for your multi-dimensional array, ie: 将您的scipy升级到版本0.16.1,它将适用于您的多维数组,即:

C2 = model(C)

works. 作品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM