137

我应该如何在 python 中计算以二为底的日志。例如。我有这个等式,我使用对数基数 2

import math
e = -(t/T)* math.log((t/T)[, 2])
4

9 回答 9

269

很高兴知道

log_b(a) = log(a)/log(b)

但也知道它 math.log需要一个可选的第二个参数,它允许您指定基数:

In [22]: import math

In [23]: math.log?
Type:       builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form:    <built-in function log>
Namespace:  Interactive
Docstring:
    log(x[, base]) -> the logarithm of x to the given base.
    If the base not specified, returns the natural logarithm (base e) of x.


In [25]: math.log(8,2)
Out[25]: 3.0
于 2010-09-15T16:23:24.187 回答
95

取决于输入或输出是int还是float

assert 5.392317422778761 ==   math.log2(42.0)
assert 5.392317422778761 ==    math.log(42.0, 2.0)
assert 5                 ==  math.frexp(42.0)[1] - 1
assert 5                 ==            (42).bit_length() - 1

浮动→浮动math.log2(x)

import math

log2 = math.log(x, 2.0)
log2 = math.log2(x)   # python 3.3 or later

浮点→整数math.frexp(x)

如果您只需要浮点数的对数基数 2 的整数部分,那么提取指数非常有效:

log2int_slow = int(math.floor(math.log(x, 2.0)))    # these give the
log2int_fast = math.frexp(x)[1] - 1                 # same result
  • Python frexp() 调用C 函数 frexp(),它只是抓取和调整指数。

  • Python frexp() 返回一个元组(尾数,指数)。所以[1]得到指数部分。

  • 对于 2 的整数幂,指数比您预期的要多 1。例如 32 存储为 0.5x2⁶。这解释了- 1上述情况。也适用于存储为 0.5x2⁻⁴ 的 1/32。

  • 向负无穷大倾斜,因此以这种方式计算的 log231 是 4 而不是 5。log2(1/17) 是 -5 而不是 -4。


整数 → 整数x.bit_length()

如果输入和输出都是整数,则此原生整数方法可能非常有效:

log2int_faster = x.bit_length() - 1
  • - 1因为 2ⁿ 需要 n+1 位。适用于非常大的整数,例如2**10000.

  • 向负无穷大倾斜,因此以这种方式计算的 log231 是 4 而不是 5。

于 2015-01-19T20:41:19.390 回答
20

如果您使用的是 python 3.3 或更高版本,那么它已经具有用于计算 log2(x) 的内置函数

import math
'finds log base2 of x'
answer = math.log2(x)

如果您使用的是旧版本的python,那么您可以这样做

import math
'finds log base2 of x'
answer = math.log(x)/math.log(2)
于 2014-11-16T09:02:14.070 回答
11

使用 numpy:

In [1]: import numpy as np

In [2]: np.log2?
Type:           function
Base Class:     <type 'function'>
String Form:    <function log2 at 0x03049030>
Namespace:      Interactive
File:           c:\python26\lib\site-packages\numpy\lib\ufunclike.py
Definition:     np.log2(x, y=None)
Docstring:
    Return the base 2 logarithm of the input array, element-wise.

Parameters
----------
x : array_like
  Input array.
y : array_like
  Optional output array with the same shape as `x`.

Returns
-------
y : ndarray
  The logarithm to the base 2 of `x` element-wise.
  NaNs are returned where `x` is negative.

See Also
--------
log, log1p, log10

Examples
--------
>>> np.log2([-1, 2, 4])
array([ NaN,   1.,   2.])

In [3]: np.log2(8)
Out[3]: 3.0
于 2010-09-15T16:37:07.373 回答
7

http://en.wikipedia.org/wiki/Binary_logarithm

def lg(x, tol=1e-13):
  res = 0.0

  # Integer part
  while x<1:
    res -= 1
    x *= 2
  while x>=2:
    res += 1
    x /= 2

  # Fractional part
  fp = 1.0
  while fp>=tol:
    fp /= 2
    x *= x
    if x >= 2:
        x /= 2
        res += fp

  return res
于 2010-09-15T16:24:22.817 回答
6
>>> def log2( x ):
...     return math.log( x ) / math.log( 2 )
... 
>>> log2( 2 )
1.0
>>> log2( 4 )
2.0
>>> log2( 8 )
3.0
>>> log2( 2.4 )
1.2630344058337937
>>> 
于 2010-09-15T16:19:20.827 回答
2

试试这个 ,

import math
print(math.log(8,2))  # math.log(number,base) 
于 2018-02-14T10:12:41.270 回答
2

在 python 3 或更高版本中,数学类具有以下功能

import math

math.log2(x)
math.log10(x)
math.log1p(x)

或者您通常可以math.log(x, base)用于您想要的任何基础。

于 2019-01-09T14:25:59.540 回答
0

不要忘记log[base A] x = log[base B] x / log[base B] A

因此,如果您只有log(for natural log) 和log10(for base-10 log),您可以使用

myLog2Answer = log10(myInput) / log10(2)
于 2010-09-15T16:20:27.843 回答