计算对数的 Python log() 函数

Logarithms用于描述和表示大数字。日志是指数的反面。本文将深入了解Python log()函数。


在 Python 中理解 log() 函数

为了使用日志函数的功能,我们需要使用下面的陈述来 ** 导入 ** 的数学模块。

1import math

我们都需要注意到,Python 日志函数无法直接访问,我们需要使用数学模块来访问代码中的日志函数。

合成:**

1math.log(x)

math.log(x)函数用于计算 自然逻辑值,即 ** log 到基数 e** (欧勒数) 即大约 2.71828 的参数值(** 数字表达式**),传递给它。

** 例子:**

1import math   
2
3print("Log value: ", math.log(2))

在上面的代码片段中,我们正在请求2的逻辑值。

出发点:**

1Log value:  0.6931471805599453

Python log() 函数的变体

以下是 Python 中的基本日志函数的变体:

  • log2(x)
  • log(x, Base)
  • log10(x)
  • log1p(x)

1. log2(x) - 日志基础 2

「math.log2(x)」函数用于计算 base 2 的数值的 **logarithmic 值。

合成:**

1math.log2(numeric expression)

** 例子:**

1import math 
2
3print ("Log value for base 2: ") 
4print (math.log2(20))

出发点:**

1Log value for base 2: 
24.321928094887363

log(n, Base) - log base n

math.log(x,Base)函数计算了 x 的逻辑值,即对 ** 特定的 (所需) 基数值** 的数值表达式。

合成:**

1math.log(numeric_expression,base_value)

这个函数接受两个论点:

  • 数字表达式**
  • 基数值**

** 注意**:如果函数没有提供 ** 基数值**,则 math.log(x(Base))作为 ** 基数日志函数**,并计算数字表达式的日志到 ** 基数 e**。

** 例子:**

1import math 
2
3print ("Log value for base 4 : ") 
4print (math.log(20,4))

出发点:**

1Log value for base 4 : 
22.1609640474436813

log10(x) - 日志基础 10

math.log10(x)函数计算数字表达式的逻辑值到 base 10

合成:**

1math.log10(numeric_expression)

** 例子:**

1import math 
2
3print ("Log value for base 10: ") 
4print (math.log10(15))

在上面的代码片段中,计算了 15base 10的逻辑值。

出发点:**

1Log value for base 10 : 
21.1760912590556813

标签1p(x)

math.log1p(x)函数计算特定输入值的 log(1+x),即 x

注意: math.log1p(1+x) 等同于 math.log(x)

合成:**

1math.log1p(numeric_expression)

** 例子:**

1import math 
2
3print ("Log value(1+15) for x = 15 is: ") 
4print (math.log1p(15))

在上面的代码片段中,计算输入表达式 15 的日志值 (1+15)。

因此,‘math.log1p(15)’相当于‘math.log(16)’。

出发点:**

1Log value(1+15) for x = 15 is: 
22.772588722239781

理解 Python NumPy 中的日志

Python NumPy使我们能够同时计算输入的NumPy数组元素的自然逻辑值。

为了使用 numpy.log() 方法,我们需要 ** 导入 NumPy 模块** 使用下面的语句。

1import numpy

合成:**

1numpy.log(input_array)

函数 numpy.log() 接受 ** input array** 作为参数,并返回数组中元素中的 **logarithmic 值。

** 例子:**

1import numpy as np 
2
3inp_arr = [10, 20, 30, 40, 50] 
4print ("Array input elements:\n", inp_arr) 
5
6res_arr = np.log(inp_arr) 
7print ("Resultant array elements:\n", res_arr)

出发点:**

1Array input elements:
2 [10, 20, 30, 40, 50]
3Resultant array elements:
4 [ 2.30258509 2.99573227 3.40119738 3.68887945 3.91202301]

结论

在本文中,我们了解了Python日志函数的运作方式,并揭示了Python中的逻辑函数的变体。


参考

Published At
Categories with 技术
Tagged with
comments powered by Disqus