如何在python中获取函数的ln? [英] How to take the ln of a function in python?
问题描述
我使用polyfit查找数据集的拟合线,但是现在我需要查找该fitline函数的自然对数并对其进行绘制.这是我到目前为止的内容:
I used polyfit to find a fitline of a data set, but now I need to find the natural log of that fitline function and plot it. Here's what I have so far:
#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')
是否有更好或更正确的方法来做到这一点?
Is there a better or more correct way to do this?
推荐答案
编辑
我建议使用numpy.log
作为 Roger Fan ,如下所示.由于您已经在使用numpy数组,因此使用map
或列表推导方法肯定会胜过这种情况.
Edit
I would suggest using numpy.log
as Roger Fan demonstrated below. Since you are already using numpy arrays, this will certainly outperform using map
or a list comprehension.
原始答案
如果您具有z值的list
,则可以使用map
对每个值(在本例中为log
(即ln
))执行某些功能.
Original answer
If you have a list
of z-values, you can use map
to perform some function to each value, in this case log
(which is 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
.
You could use any function, so you may use numpy.log
if you prefer.
这篇关于如何在python中获取函数的ln?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!