Search code examples
pythonarraysindices

perform a function on several elements of an array in python


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 ?


Solution

  • 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]