简体   繁体   English

沿 numpy 数组应用函数

[英]Applying a function along a numpy array

I've the following numpy ndarray.我有以下 numpy ndarray。

[ -0.54761371  17.04850603   4.86054302]

I want to apply this function to all elements of the array我想将此函数应用于数组的所有元素

def sigmoid(x):
  return 1 / (1 + math.exp(-x))

probabilities = np.apply_along_axis(sigmoid, -1, scores)

This is the error that I get.这是我得到的错误。

TypeError: only length-1 arrays can be converted to Python scalars

What am I doing wrong.我究竟做错了什么。

Function numpy.apply_along_axis is not good for this purpose.函数numpy.apply_along_axis不适用于此目的。 Try to use numpy.vectorize to vectorize your function: https://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html This function defines a vectorized function which takes a nested sequence of objects or numpy arrays as inputs and returns an single or tuple of numpy array as output.尝试使用numpy.vectorize对您的函数进行矢量化: https : numpy.vectorize该函数定义了一个矢量化函数,该函数采用嵌套的对象序列或 numpy 数组作为输入并返回 numpy 数组的单个或元组作为输出。

import numpy as np
import math

# custom function
def sigmoid(x):
  return 1 / (1 + math.exp(-x))

# define vectorized sigmoid
sigmoid_v = np.vectorize(sigmoid)

# test
scores = np.array([ -0.54761371,  17.04850603,   4.86054302])
print sigmoid_v(scores)

Output: [ 0.36641822 0.99999996 0.99231327]输出: [ 0.36641822 0.99999996 0.99231327]

Performance test which shows that the scipy.special.expit is the best solution to calculate logistic function and vectorized variant comes to the worst:性能测试表明scipy.special.expit是计算逻辑函数的最佳解决方案,而矢量化变体则最差:

import numpy as np
import math
import timeit

def sigmoid_(x):
  return 1 / (1 + math.exp(-x))
sigmoidv = np.vectorize(sigmoid_)

def sigmoid(x):
   return 1 / (1 + np.exp(x))

print timeit.timeit("sigmoidv(scores)", "from __main__ import sigmoidv, np; scores = np.random.randn(100)", number=25),\
timeit.timeit("sigmoid(scores)", "from __main__ import sigmoid, np; scores = np.random.randn(100)",  number=25),\
timeit.timeit("expit(scores)", "from scipy.special import expit; import numpy as np;   scores = np.random.randn(100)",  number=25)

print timeit.timeit("sigmoidv(scores)", "from __main__ import sigmoidv, np; scores = np.random.randn(1000)", number=25),\
timeit.timeit("sigmoid(scores)", "from __main__ import sigmoid, np; scores = np.random.randn(1000)",  number=25),\
timeit.timeit("expit(scores)", "from scipy.special import expit; import numpy as np;   scores = np.random.randn(1000)",  number=25)

print timeit.timeit("sigmoidv(scores)", "from __main__ import sigmoidv, np; scores = np.random.randn(10000)", number=25),\
timeit.timeit("sigmoid(scores)", "from __main__ import sigmoid, np; scores = np.random.randn(10000)",  number=25),\
timeit.timeit("expit(scores)", "from scipy.special import expit; import numpy as np;   scores = np.random.randn(10000)",  number=25)

Results:结果:

size        vectorized      numpy                 expit
N=100:   0.00179314613342 0.000460863113403 0.000132083892822
N=1000:  0.0122890472412  0.00084114074707  0.000464916229248
N=10000: 0.109477043152   0.00530695915222  0.00424313545227

Use np.exp and that will work on numpy arrays in a vectorized fashion:使用np.exp并将以矢量化方式处理 numpy 数组:

>>> def sigmoid(x):
...     return 1 / (1 + np.exp(-x))
...
>>> sigmoid(scores)
array([  6.33581776e-01,   3.94391811e-08,   7.68673281e-03])
>>>

You will likely not get any faster than this.您可能不会比这更快。 Consider:考虑:

>>> def sigmoid(x):
...     return 1 / (1 + np.exp(-x))
...

And:和:

>>> def sigmoidv(x):
...   return 1 / (1 + math.exp(-x))
...
>>> vsigmoid = np.vectorize(sigmoidv)

Now, to compare the timings.现在,比较时间。 With a small (size 100) array:使用小(大小 100)数组:

>>> t = timeit.timeit("vsigmoid(arr)", "from __main__ import vsigmoid, np; arr = np.random.randn(100)", number=100)
>>> t
0.006894525984534994
>>> t = timeit.timeit("sigmoid(arr)", "from __main__ import sigmoid, np; arr = np.random.randn(100)", number=100)
>>> t
0.0007238480029627681

So, still an order-of-magnitude difference with small arrays.因此,与小阵列仍然存在数量级差异。 This performance differences stays relatively constant, with a 10,000 size array:这种性能差异保持相对恒定,数组大小为 10,000:

>>> t = timeit.timeit("vsigmoid(arr)", "from __main__ import vsigmoid, np; arr = np.random.randn(10000)", number=100)
>>> t
0.3823414359940216
>>> t = timeit.timeit("sigmoid(arr)", "from __main__ import sigmoid, np; arr = np.random.randn(10000)", number=100)
>>> t
0.011259705002885312

And finally with a size 100,000 array:最后是一个大小为 100,000 的数组:

>>> t = timeit.timeit("vsigmoid(arr)", "from __main__ import vsigmoid, np; arr = np.random.randn(100000)", number=100)
>>> t
3.7680041620042175
>>> t = timeit.timeit("sigmoid(arr)", "from __main__ import sigmoid, np; arr = np.random.randn(100000)", number=100)
>>> t
0.09544878199812956

scipy已经实现了这个函数 幸运的是,Python 允许我们在导入时重命名:

 from scipy.special import expit as sigmoid

Just to clarify what apply_along_axis is doing, or not doing.只是为了澄清apply_along_axis正在做什么或不做什么。

def sigmoid(x):
  print(x)    # show the argument
  return 1 / (1 + math.exp(-x))

In [313]: np.apply_along_axis(sigmoid, -1,np.array([ -0.54761371  ,17.04850603 ,4.86054302])) 
[ -0.54761371  17.04850603   4.86054302]   # the whole array
...
TypeError: only length-1 arrays can be converted to Python scalars

The reason you get the error is that apply_along_axis passes a whole 1d array to your function.您收到错误的原因是apply_along_axis将整个一apply_along_axis数组传递给您的函数。 Ie the axis.即轴。 For your 1d array this is the same as对于您的一维数组,这与

sigmoid(np.array([ -0.54761371  ,17.04850603 ,4.86054302]))

The apply_along_axis does nothing for you. apply_along_axis对您没有任何作用。

As others noted,switching to np.exp allows sigmoid to work with the array (with or without the apply_along_axis wrapper).正如其他人指出的那样,切换到np.exp允许sigmoid使用数组(有或没有 apply_along_axis 包装器)。

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

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