首页 > 解决方案 > 将 math.erf 应用于数组

问题描述

我正在使用 math.erf 来查找数组中每个元素的误差函数。

# Import the erf function from the math library
from math import erf
# Import the numpy library to represent the input data
import numpy as np

# Create a dummy np.array
dummy_array = np.arange(20)

# Apply the erf function to the array to calculate the error function of each element
erf_array = erf(dummy_array)

我收到一个错误,因为我无法将整个函数应用于数组。有没有一种方法可以将误差函数应用于整个数组(矢量化方法),而无需遍历每个元素并应用它?(循环会花费很多时间,因为表格会很大)

标签: pythonpandasfunctionnumpyvectorization

解决方案


from scipy import special
import numpy as np

dummy_array = np.arange(20)
erf_array = special.erf(dummy_array)

将子包导入specialfrom scipy import special. 正如此处此处所解释的那样,导入就像scipy然后import scipy调用scipy.special.erf()将不起作用。


推荐阅读