python怎么识别log函数,如何在python中获取函数的ln?

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?

解决方案

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.

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]

You could use any function, so you may use numpy.log if you prefer.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值