user2954167 - 5 months ago 25

Python Question

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?

Answer

**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.