我應該如何計算日誌到Python中的基礎2。例如。我有這樣的方程,其中我使用對數底2在Python中登錄到基地2
import math
e = -(t/T)* math.log((t/T)[, 2])
我應該如何計算日誌到Python中的基礎2。例如。我有這樣的方程,其中我使用對數底2在Python中登錄到基地2
import math
e = -(t/T)* math.log((t/T)[, 2])
這是很好的瞭解,
但也知道 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
+1。更改基本公式FTW – 2010-09-15 17:01:51
在2.3版本中添加了'base'參數,順便說一句。 – 2010-09-15 18:09:03
這是什麼?句法 ?我找不到它的參考。 – wap26 2013-04-30 13:59:27
log_base_2(X)=日誌(X)/日誌(2)
logbase2(X)=日誌(X)/日誌(2)
不要忘記,日誌[基A] X =日誌[底座B]×/日誌[基B]甲。
所以,如果你只有log
(自然數)和log10
(用於基地10日誌),您可以使用
myLog2Answer = log10(myInput)/log10(2)
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
算法的額外點可以適應總是給出正確的整數部分,不像int(math.log(x,2)) – user12861 2012-01-10 13:43:53
使用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
如果您在蟒蛇3.4或以上,然後它已經計算的log 2(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)
import math
log2 = math.log(x, 2.0)
log2 = math.log2(x) # python 3.4 or later
如果你需要的是一個浮點數的日誌基地2的整數部分,math.frexp()
可能是相當有效:
log2int_slow = int(math.floor(math.log(x, 2.0)))
log2int_fast = math.frexp(x)[1] - 1
Python frexp()調用C function frexp(),它只是抓取和調整指數。
Python frexp()返回一個元組(尾數,指數)。所以[1]
得到指數部分。對於2的整數冪,指數比您預期的多一個。例如32存儲爲0.5x2。這解釋了上面的- 1
。也適用於存儲爲0.5x2 - 4的1/32。
如果輸入和輸出是整數,該整數方法.bit_length()
可能更爲有效:
log2int_faster = x.bit_length() - 1
- 1
因爲2ⁿ需要n + 1位。這是適用於非常大的整數的唯一選項,例如2**10000
。
所有INT-輸出版本將地板上的日誌向負無窮大,所以log₂31是4而不是5
有趣。所以你在那裏減去1,因爲尾數在[0.5,1.0)的範圍內?如果可以的話,我會給這一個更多的讚揚。 – LarsH 2015-02-23 11:49:47
正確@LarsH。 32存儲爲0.5x2「,所以如果你想log232 = 5,你需要**減1 **。對於存儲爲0.5x2 -4的1/32也是如此。 – 2015-02-23 14:10:44
試試這個,
import math
print(math.log(8,2)) # math.log(number,base)
你有什麼應該,如果你把工作方括號在'math.log()'調用的「,2」周圍。你試過了嗎? – martineau 2010-09-15 18:44:01
很好的熵計算 – 2010-09-16 01:36:28
math.log(value,base) – 2015-01-07 22:10:01