2010-09-15 6 views
73

Wie soll ich Log auf die Basis zwei in Python berechnen. Z.B. Ich habe diese Gleichung, wo ich Logbase bin mit 2Log auf die Basis 2 in Python

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

Was haben Sie sollte funktionieren, wenn Sie nehmen die eckigen Klammern um die ", 2" im 'math.log()' Aufruf. Hast du es versucht? – martineau

+3

nice entropy calculation –

+0

math.log (value, base) –

Antwort

157

Es ist gut verwenden können, zu wissen, dass

alt text

aber auch wissen, dass math.log ein optionales zweites Argument verwendet, mit dem Sie die Basis angeben können:

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 
+4

+1. Änderung der Basisformel FTW –

+2

'base' Argument hinzugefügt in Version 2.3, btw. –

+3

Was ist das? Syntax ? Ich kann keine Referenz dafür finden. – wap26

1

log_base_2 (x) = log (x)/log (2)

2

logbase2 (x) = log (x)/log (2)

5
>>> 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 
>>> 
+0

Dies ist in der Funktion 'math.log' integriert. Siehe unutbus Antwort. – tgray

+0

Sie haben Recht, wusste das nicht - danke;) – puzz

0

vergessen Sie nicht, dass log [base A] x = log [base B] x/log [base B] A.

Also, wenn Sie nur log (für natürliche log) haben und log10 (für base-10 log), Sie

myLog2Answer = log10(myInput)/log10(2) 
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 
+0

Zusätzliche Punkte für einen Algorithmus, der angepasst werden kann, um immer den richtigen ganzzahligen Teil zu geben, im Gegensatz zu int (math.log (x, 2)) – user12861

9

Mit 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 
9

Wenn Sie auf Python 3.4 oder höher, dann hat es bereits eine integrierte Funktion zur Berechnung von log 2 (x)

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

Wenn Sie auf ältere Version von Python sind, dann können Sie dies tun

import math 
'finds log base2 of x' 
answer = math.log(x)/math.log(2) 
27

Schwimmer in - schweben

import math 

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

Schwimmer in - int aus

Wenn alles, was Sie brauchen, der ganzzahlige Teil der Log-Basis 2 eine Gleitkommazahl ist, könnte math.frexp() ziemlich effizient sein:

log2int_slow = int(math.floor(math.log(x, 2.0))) 
log2int_fast = math.frexp(x)[1] - 1 
  • Python frexp() ruft die C function frexp() auf, die nur den Exponenten packt und zwickt.

  • Python frexp() gibt ein Tupel zurück (Mantisse, Exponent). So bekommt [1] den Exponententeil. Für ganzzahlige Potenzen von 2 ist der Exponent um eins höher als erwartet. Zum Beispiel wird 32 als 0,5x2 gespeichert. Dies erklärt die obigen - 1. Funktioniert auch für 1/32, die als 0.5x2⁴⁴ gespeichert wird.


int in - int aus

Wenn beide Eingangs- und Ausgangs ganze Zahlen sind, die Integer-Methode .bit_length() könnte noch effizienter sein:

log2int_faster = x.bit_length() - 1 
  • - 1 weil 2ⁿ erfordert n + 1 Bits. Dies ist die einzige Option, die für sehr große Ganzzahlen, z. 2**10000.

  • Alle int-Ausgabe Versionen Boden das Protokoll in Richtung negativ unendlich, so log₂31 4 nicht 5.

+1

Interessant. Also subtrahierst du 1, weil die Mantisse im Bereich [0.5, 1.0] liegt? Ich würde diesem ein paar mehr Upvotes geben, wenn ich könnte. – LarsH

+1

Genau richtig @LarsH. 32 wird als 0.5x2 gespeichert. Wenn Sie also log₂32 = 5 wollen, müssen Sie ** ** subtrahieren. Gilt auch für 1/32, die als 0.5x2⁴⁴ gespeichert wird. –

0

Try this,

import math 
print(math.log(8,2)) # math.log(number,base)