0

I have an array called population that contains 66 items and I want to perform the log10 on each element and display the answers as an array as well. Here is what I came up with already :

import math
import numpy as np

population_magnitudes = math.log10(population.item(np.arange(0,66,1)))
population_magnitudes

I get the following error :

incorrect number of indices for array

Could anyone help ?

3 Answers 3

1

I'm not sure that I understand correctly, but does this answer your question ?

import numpy as np

population = np.arange(0,66,1)
population_magnitudes = np.log10(population)
print(population_magnitudes)

Output:

[       -inf  0.          0.30103     0.47712125  0.60205999  0.69897
  0.77815125  0.84509804  0.90308999  0.95424251  1.          1.04139269
  1.07918125  1.11394335  1.14612804  1.17609126  1.20411998  1.23044892
  1.25527251  1.2787536   1.30103     1.32221929  1.34242268  1.36172784
  1.38021124  1.39794001  1.41497335  1.43136376  1.44715803  1.462398
  1.47712125  1.49136169  1.50514998  1.51851394  1.53147892  1.54406804
  1.5563025   1.56820172  1.5797836   1.59106461  1.60205999  1.61278386
  1.62324929  1.63346846  1.64345268  1.65321251  1.66275783  1.67209786
  1.68124124  1.69019608  1.69897     1.70757018  1.71600334  1.72427587
  1.73239376  1.74036269  1.74818803  1.75587486  1.76342799  1.77085201
  1.77815125  1.78532984  1.79239169  1.79934055  1.80617997  1.81291336]
Sign up to request clarification or add additional context in comments.

1 Comment

@Ankireddy: why did you change the range? I used the range defined in the OP (i.e. arange(0,66,1))
1

MAP FUNCTION

You can use the map function to apply a function to all elements in an array, it looks like this :

>>> import math
>>> arr = [10**x for x in range(10)]
>>> arr
[1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000]
>>> ans = list(map(math.log10,arr))
>>> ans
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

LIST COMPREHENSION

You can use this method to create a list using another list iteration

>>> licomp = [math.log10(x) for x in arr]
>>> licomp
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]

If you specifically want numpy arrays, use np.log10 instead of math.log10 for direct implementation. Otherwise you can follow any of the above method and then convert the obtained list to numpy array using np.array(list_obtained).

>>> import numpy as np
>>> nparr =  np.arange(66)
>>> nparr
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
       51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65])
>>> np.log10(nparr)
__main__:1: RuntimeWarning: divide by zero encountered in log10
array([      -inf, 0.        , 0.30103   , 0.47712125, 0.60205999,
       0.69897   , 0.77815125, 0.84509804, 0.90308999, 0.95424251,
       1.        , 1.04139269, 1.07918125, 1.11394335, 1.14612804,
       1.17609126, 1.20411998, 1.23044892, 1.25527251, 1.2787536 ,
       1.30103   , 1.32221929, 1.34242268, 1.36172784, 1.38021124,
       1.39794001, 1.41497335, 1.43136376, 1.44715803, 1.462398  ,
       1.47712125, 1.49136169, 1.50514998, 1.51851394, 1.53147892,
       1.54406804, 1.5563025 , 1.56820172, 1.5797836 , 1.59106461,
       1.60205999, 1.61278386, 1.62324929, 1.63346846, 1.64345268,
       1.65321251, 1.66275783, 1.67209786, 1.68124124, 1.69019608,
       1.69897   , 1.70757018, 1.71600334, 1.72427587, 1.73239376,
       1.74036269, 1.74818803, 1.75587486, 1.76342799, 1.77085201,
       1.77815125, 1.78532984, 1.79239169, 1.79934055, 1.80617997,
       1.81291336])

Comments

0

Example:

from math import log
population = [2,4,23,4]
result = [log(val, 10) for val in population]

print result 


#output [0.30102999566398114, 0.6020599913279623, 1.3617278360175928, 0.6020599913279623]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.