Python中的Arrayfun? [英] Arrayfun in Python?
问题描述
我正在尝试将一些代码从Matlab转换为Python.知道我将如何转换此行吗?我是python的新手,以前从未见过arrayfun
.谢谢.非常感激.
I'm trying to convert some code from Matlab to Python. Any idea how I would go about converting this line? I'm very new to python and I've never seen arrayfun
before. Thanks. Much appreciated.
zj=arrayfun(@sigmoid,aj);
推荐答案
您将要使用数字库 numpy
每当处理数字数据时.
You will want to use the numerical library numpy
whenever you're working with numerical data.
其中,名为arrayfun
的Matlab函数只是该函数的向量化形式.例如
In it, the Matlab function called arrayfun
is simply the vectorized form of that function. E.g.
Matlab:
>> a = 1:4
a =
1 2 3 4
>> arrayfun(@sqrt, a)
ans =
1.0000 1.4142 1.7321 2.0000
>> sqrt(a)
ans =
1.0000 1.4142 1.7321 2.0000
在numpy中,您需要这样做:
Whereas in numpy, you'd do:
>>> import numpy as np
>>> a = np.arange(4)
>>> np.sqrt(a)
array([ 0. , 1. , 1.41421356, 1.73205081])
大多数函数都可以向量化,S形也不例外.例如,如果将sigmoid定义为1./(1 + exp(-x))
,则可以使用Python编写:
Most functions can be vectorized, a sigmoid is no exception to that. For example, if the sigmoid were defined as 1./(1 + exp(-x))
, then you could write in Python:
def sigmoid(x):
return 1./(1 + np.exp(-x))
zj = sigmoid(aj)
这篇关于Python中的Arrayfun?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!