簡體   English   中英

如何在python中獲取函數的ln?

[英]How to take the ln of a function in python?

我使用 polyfit 來找到數據集的擬合線,但現在我需要找到該擬合線函數的自然對數並繪制它。 這是我到目前為止所擁有的:

#Fit line for PD
deg = 10
zn = np.polyfit(l_bins, l_hits, deg)
l_pn = np.poly1d(zn)
pylab.plot(l_bins, l_pn(l_bins), '-g')
ln_list = []
for all in l_bins:
    ln_list.append(np.log(l_pn(all)))
pylab.plot(l_bins, ln_list, '-b')

有沒有更好或更正確的方法來做到這一點?

似乎您只想要最初提供的垃圾箱的值。 在這種情況下,這更簡單,速度也會更快。

ln_list = np.log(l_pn(l_bins))

請記住,如果這樣做有意義, numpy函數通常會將自己逐元素應用於數組。

log(x) 是基於 10 的,而 ln(x) 是基於自然對數的。

import math
x = 8
print math.log(x, math.e)

編輯
我建議使用numpy.log作為Roger Fan在下面演示的。 由於您已經在使用 numpy 數組,因此這肯定會優於使用map或列表理解。


原答案
如果您有一個 z 值list ,則可以使用map對每個值執行某些功能,在這種情況下為log (即ln )。

>>> x = range(1,10)
>>> x
[1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> from math import log
>>> map(log, x)
[0.0, 0.6931471805599453, 1.0986122886681098, 1.3862943611198906, 1.6094379124341003, 1.791759469228055, 1.9459101490553132, 2.0794415416798357, 2.1972245773362196]

您可以使用任何函數,因此如果您願意,可以使用numpy.log

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM