我有一个名为population
的数组,其中包含66个项目,我想在每个元素上执行log10,并将答案显示为数组。以下是我的想法:
import math
import numpy as np
population_magnitudes = math.log10(population.item(np.arange(0,66,1)))
population_magnitudes
我收到以下错误:
incorrect number of indices for array
有人可以帮忙吗?
答案 0 :(得分:1)
我不确定我是否理解正确,但这是否回答了你的问题?
import numpy as np
population = np.arange(0,66,1)
population_magnitudes = np.log10(population)
print(population_magnitudes)
输出:
[ -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]
答案 1 :(得分:1)
地图功能
您可以使用map函数将函数应用于数组中的所有元素,如下所示:
>>> 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]
列表综合
您可以使用此方法使用其他列表迭代创建列表
>>> 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]
如果您特别需要numpy数组,请使用np.log10
代替math.log10
进行直接实施。否则,您可以按照上述任何方法,然后使用np.array(list_obtained)
将获取的列表转换为numpy数组。
>>> 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])
答案 2 :(得分:0)
示例:
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]