将 math.erf 应用于数组
Apply math.erf to an array
我正在使用 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)
我收到错误消息,因为我无法将整个函数应用于数组。有没有一种方法可以将误差函数应用于整个数组(矢量化方法)而无需遍历每个元素并应用它? (由于表格很大,循环会花费很多时间)
from scipy import special
import numpy as np
dummy_array = np.arange(20)
erf_array = special.erf(dummy_array)
必须将 special
子包导入为 from scipy import special
。仅将 scipy
导入为 import scipy
然后调用 scipy.special.erf()
将不起作用,如 here and here.
所述
您可以使用列表理解将函数一次应用于每个元素
erf_array = [erf(element) for element in dummy_array)]
我正在使用 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)
我收到错误消息,因为我无法将整个函数应用于数组。有没有一种方法可以将误差函数应用于整个数组(矢量化方法)而无需遍历每个元素并应用它? (由于表格很大,循环会花费很多时间)
from scipy import special
import numpy as np
dummy_array = np.arange(20)
erf_array = special.erf(dummy_array)
必须将 special
子包导入为 from scipy import special
。仅将 scipy
导入为 import scipy
然后调用 scipy.special.erf()
将不起作用,如 here and here.
您可以使用列表理解将函数一次应用于每个元素
erf_array = [erf(element) for element in dummy_array)]