Python Math.erf()方法
我们使用 Python 的math
模块来进行数学计算和标准工作。这个模块为我们提供了许多不同的方法,比如math.erf()
方法,它接收一个数字并返回所提供参数的错误函数。
这个方法是在Python 3.2版本中新引入的。这些误差方法通常与计算问题和机器学习技术有关。
该方法接受的数值范围为-Infinity到+Infinity。我们用这个方法来解决各种数学问题,如高斯和数字的积分。
Pythonmath.erf()
方法的语法
math.erf(number)
参数
number |
这是用来寻找误差函数的数字,范围是-Infinity到+Infinity。 |
返回值
该方法返回浮动值并显示所提供参数的误差值。
示例代码:在Python中使用math.erf()
方法
在Python 3.2及以后的版本中,我们使用方法math.erf()
来检查所提供数字的错误函数。
这个方法是用来计算标准正态分布的分布函数的。它实际上是正态分布的积分。
这个方法接受一个从-无限到+无限的数字,并返回一个从-1到+1的值。下面的例子显示了所提供的不同参数的输出变化。
# import to use the mathematics functions
import math
def find_error_function_value(number):
return math.erf(number)
# shows the error function for different value arguments
print('Error function of 65 =',find_error_function_value(65))
# method accepts the negative value as well
print('Error function of -3 =',find_error_function_value(-3))
print('Error function of -0.016 =',find_error_function_value(-0.016))
print('Error function of 19 =',find_error_function_value(19))
print('Error function of 1.98 =',find_error_function_value(1.98))
print('Error function of 908 =',find_error_function_value(908))
输出:
Error function of 65 = 1.0
# negative value has negative floating value result
Error function of -3 = -0.9999779095030014
Error function of -0.016 = -0.01805252617815065
Error function of 19 = 1.0
Error function of 1.98 = 0.9948920003868136
# maximum value is 1.0
Error function of 908 = 1.0
示例代码:对不同符号的相同数字使用math.erf()
方法
在下面的例子中,我们将向math.erf()
方法提供带有正负符号的相同数字。输出结果显示,如果提供的数字有一个+
的符号,结果是正的,反之亦然。
这两个例子表明,math.erf()
方法的结果总是返回一个在-1和+1之间的值。
import math
def test_postive_negative_number(number):
return math.erf(number)
# same parameter value with + and - sign
print('Error function of a number with a positive sign =', test_postive_negative_number(9.28))
print('Error function of a number with a negative sign =', test_postive_negative_number(-9.28))
输出:
# returns the same result with the same sign
Error function of a number with a positive sign = 1.0
Error function of a number with a negative sign = -1.0
示例代码:使用math.erf()
方法来返回高斯概率密度函数
在下面的例子中,math.erf()
方法有助于返回高斯概率密度函数下的面积。
该方法从-Infinity到所提供的数字进行积分。参数number
可以是一个简单的实数或复数。
ndtr()
方法返回所提供数字的标量值。该方法比较所提供数值的绝对值和平方根,并返回结果。
import math
def ndtr(number):
# square root method of the math module
square_root = math.sqrt(2) / 2
initial_result = float(number) * square_root
# absolute method always returns a positive value
absolute = abs(initial_result)
if absolute < square_root:
result = 0.5 + 0.5 * math.erf(initial_result)
else:
result = 0.5 * math.erfc(absolute)
if initial_result > 0:
result = 1 - result
return result
print("The result of the area under the Gaussian probability density function is" , ndtr(5.33))
输出:
The result of the area under the Gaussian probability density function is 0.9999999508936167
示例代码:使用math.erf()
方法来解决高斯积分的问题
在 Python 3.2 及以上版本中,math.erf()
方法也可以用来求解高斯积分。为了这个目的,我们用四个参数编写一个称为gaussian_integral()
的方法:x
,y
,mean
, 和sigma
。
该方法包含两个语句,分别是负/正无穷大的积分到x
,以及负/正无穷大的积分到y
。该方法返回天花板到地板的替换语句。
import math
# import for using the square root function
from math import sqrt
# method returns the integral of the gaussian center
def gaussian_integral(x, y, m, sig):
floor = 0.5 * (1 + math.erf((x - m) / (sig * math.sqrt(2.0))))
ceil = 0.5 * (1 + math.erf((y - m) / (sig * sqrt(2.0))))
return ceil - floor
print("The integral of a gaussian center is",gaussian_integral(-3.111, 6.22, 8, 1))
输出:
The integral of a gaussian center is 0.0375379803485168
示例代码:使用math.erf()
方法来寻找积分
方法math.erf()
,也用于查找两个给定(浮点数)参数的积分,并返回计算出的积分的浮点数。下面的例子将两个浮动值作为default_integral()
方法的参数,并返回积分的浮动值。
import math
# method returns the integral of two floating values
def default_integral(a, b):
return 0.5 * (math.erf(b) - math.erf(a))
print("The value of the integral is",default_integral(9.188, 3.56))
输出:
The value of the integral is -2.394234760449976e-07