在 python 中对数组的多个元素执行函数
perform a function on several elements of an array in python
我有一个名为 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
有人能帮忙吗?
我不确定我是否理解正确,但这是否回答了您的问题?
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]
示例:
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]
地图功能
您可以使用 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])
我有一个名为 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
有人能帮忙吗?
我不确定我是否理解正确,但这是否回答了您的问题?
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]
示例:
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]
地图功能
您可以使用 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)
.
>>> 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])