29

I have an array

x = [1500, 1049.8, 34, 351, etc]

How can I take log_10() of the entire array?

holdenweb
  • 33,305
  • 7
  • 57
  • 77
Dax Feliz
  • 12,220
  • 8
  • 30
  • 33

5 Answers5

48

numpy will do that for you.

import numpy
numpy.log10(mat)

Note

mat does not have to be a numpy array for this to work, and numpy should be faster than using a list comprehension as other answers suggest.

jmetz
  • 12,144
  • 3
  • 30
  • 41
  • 1
    I have no doubt that NumPy will do this, and do it very quickly, but bear in mind it's a little bit overkill to install NumPy for some simple scripting. – Hank Gay Jul 25 '12 at 19:19
  • 4
    who said it's for some simple scripting? – jmetz Jul 25 '12 at 19:23
  • @mutzmatron Nobody, I suppose, but the question has a beginner feel to it, and when I was starting out, I used tiny, tiny problems to reduce the number of things I could mess up at any one time. Just thought I'd point it out. FTR, I upvoted you since you gave a good answer that wasn't the same as the like 20 of us who all jumped in with a list comp as our answer. – Hank Gay Jul 25 '12 at 20:02
  • @HankGay - hehehe yeah I noticed how the list comp answers poured in. Cheers for the upvote, and I do agree that in a sense the pythonic way would be to use the list comp and I use them a lot. – jmetz Jul 25 '12 at 20:04
  • Using `numpy.log10` worked for my 2 dimensional array, while `math.log10` would not. – mikey Aug 25 '20 at 14:19
21
from math import log
[log(y,10) for y in x]
phimuemue
  • 34,669
  • 9
  • 84
  • 115
  • 8
    Or use `log10` ;) `This is usually more accurate than log(x, 10).` – phant0m Jul 25 '12 at 19:07
  • This works to answer the poster's question, and it is efficient, but I was trying to take the logarithm of a 2 dimensional array. For that I used `numpy.log10` instead. – mikey Aug 25 '20 at 14:18
9

The simpliest way is to use a list comprehension


Example:

>>> x = [1500, 1049.8, 34, 351]
>>> import math
>>> [math.log10(i) for i in x]
[3.1760912590556813, 3.021106568432122, 1.5314789170422551, 2.545307116465824]
>>> 

Another way is to use the map function


Example:

>>> map(math.log10, x)
[3.1760912590556813, 3.021106568432122, 1.5314789170422551, 2.545307116465824]
>>> 
sloth
  • 99,095
  • 21
  • 171
  • 219
9

You could also use the map builtin function:

import math
new_list = map(math.log10, old_list)

This will probably be insignificantly faster than the list comprehension. I add it here mainly to show the similarity between the two.

EDIT (in response to the comment by @HankGay)

To prove that map is slightly faster in this case, I've written a small benchmark:

import timeit

for i in range(10):
    t=timeit.timeit("map(math.log10,a)",setup="import math; a=range(1,100)")
    print "map",t
    t=timeit.timeit("[math.log10(x) for x in a]",setup="import math; a=range(1,100)")
    print "list-comp",t

Here are the results on my laptop (OS-X 10.5.8, CPython 2.6):

map 24.5870189667
list-comp 32.556563139
map 23.2616219521
list-comp 32.0040669441
map 23.9995992184
list-comp 33.2653431892
map 24.1171340942
list-comp 33.0399811268
map 24.3114480972
list-comp 33.5015368462
map 24.296754837
list-comp 33.5107491016
map 24.0294749737
list-comp 33.5332789421
map 23.7013399601
list-comp 33.1543111801
map 24.41685009
list-comp 32.9259850979
map 24.1111209393
list-comp 32.9298729897

It is important to realize that speed isn't everything though. "readability matters". If map creates something that is harder to read, definitely go for a list comprehension.

mgilson
  • 300,191
  • 65
  • 633
  • 696
  • 1
    I would be utterly shocked if `map` were faster than a list comp, at least on CPython. The CPython implementation of list comps is optimized out the wazoo. If I get some downtime, I might go set up a micro-benchmark, which, as we all know, are fabulously useful :-) – Hank Gay Jul 25 '12 at 19:16
  • @HankGay -- Check out my update. For this simple test case, `map` is nearly 50% faster than an equivalent list comp. – mgilson Jul 25 '12 at 19:32
  • @mgilson Weird. I'm on my work machine, so it's Snow Leopard w/ `Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) `, and my numbers are basically showing list comps to be *ever* so slightly faster, but the difference is so small I doubt it is statistically significant: [my code](https://gist.github.com/3178199) yielded `lc: 1.03603506088, map: 1.04137277603`, `lc: 1.16478681564, map: 1.21709990501`, and `lc: 0.909293889999, map: 1.15685892105` – Hank Gay Jul 25 '12 at 19:54
  • @HankGay -- I reran my test giving the list comp a slight "advantage" (`from math import log10`) and `[log10(x) for x in a]` and map still won, but by a much more slim margin (map ~ 23s, list_comp ~ 25s). I wonder if our timings have anything to do with 64bit vs 32bit (I've seen that matter on occasion). I can also try my benchmark on Ubuntu Linux -- I'll give it a shot and get back to you... – mgilson Jul 25 '12 at 20:05
  • @mgilson 64-bit vs. 32-bit is a great point and something I didn't even consider. This just goes to show why I pretty much always tell people to profile before they bother to "optimize". – Hank Gay Jul 25 '12 at 20:09
  • @HankGay -- on my (64-bit) Ubuntu machine (intel i7 FWIW), `map` is still edging out list-comp (but only by ~10%) for both versions (`import math` and `from math import log10`). list size could also be a factor. My list contains 99 elements whereas yours had 5. – mgilson Jul 25 '12 at 20:13
  • 2
    And, perhaps this link ( http://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map ) should serve as the definitive reference on the subject ... – mgilson Jul 25 '12 at 20:23
5
import math
x = [1500, 1049.8, 34, 351]
y = [math.log10(num) for num in x]

This is called a list comprehension. What it is doing is creating a new list whose elements are the results of applying math.log10 to the corresponding element in the original list, which is not an array, btw.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222