计算给定 NumPy 数组中所有元素的 exp(x) – 1
指数函数(e^x) 是计算 e 的 x 次幂的数学函数,其中 e 是无理数,大约为 2.71828183。
它可以使用 numpy.exp() 方法计算。这个数学函数帮助用户计算输入数组中所有元素的指数。
Syntax: numpy.exp(arr, out, where)
Parameters:
arr : Input
out : A location into which the result is stored. If provided, it must have a shape that the
inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
shape must be same as input array.
where : Boolean Value.True value means to calculate the universal functions(ufunc) at that position, False value means to leave the value in the output alone.
如果将标量作为输入提供给函数,则将函数应用于标量并返回另一个标量。
示例 1:如果将 3 作为输入给出,则 e^3 将作为输出返回。
Python
import numpy
n = 4
print(numpy.exp(n))
n = 5
print(numpy.exp(n))
Python
# importing numpy
import numpy
arr = [1, 2, 3, 4]
print("Input : ", arr)
for i in range(len(arr)):
arr[i] = numpy.exp(arr[i])-1
print("Output : ", arr)
arr = [3, 0.3, 3.1, 2.2]
print("Input : ", arr)
for i in range(len(arr)):
arr[i] = numpy.exp(arr[i])-1
print("Output : ", arr)
Python
# importing numpy
import numpy
arr = [1, 2, 3, 4]
print("Input : ", arr)
arr = numpy.exp(arr)-1
print("Output : ", arr)
arr = [3, 0.3, 3.1, 2.2]
print("Input : ", arr)
arr = numpy.exp(arr)-1
print("Output : ", arr)
输出 :
54.598150033144236
148.4131591025766
如果输入是一个数组,那么函数是按元素应用的。 ex- np.exp([1,2,3]) 等价于 [np.exp(1),np.exp(2),np.exp(3)]
方法一:遍历数组
Python
# importing numpy
import numpy
arr = [1, 2, 3, 4]
print("Input : ", arr)
for i in range(len(arr)):
arr[i] = numpy.exp(arr[i])-1
print("Output : ", arr)
arr = [3, 0.3, 3.1, 2.2]
print("Input : ", arr)
for i in range(len(arr)):
arr[i] = numpy.exp(arr[i])-1
print("Output : ", arr)
输出:
Input : [1, 2, 3, 4]
Output : [1.718281828459045, 6.38905609893065, 19.085536923187668, 53.598150033144236]
Input : [3, 0.3, 3.1, 2.2]
Output : [19.085536923187668, 0.3498588075760032, 21.197951281441636, 8.025013499434122]
方法 2:提供数组作为 numpy.exp()函数的输入
Python
# importing numpy
import numpy
arr = [1, 2, 3, 4]
print("Input : ", arr)
arr = numpy.exp(arr)-1
print("Output : ", arr)
arr = [3, 0.3, 3.1, 2.2]
print("Input : ", arr)
arr = numpy.exp(arr)-1
print("Output : ", arr)
输出:
Input : [1, 2, 3, 4]
Output : [ 1.71828183 6.3890561 19.08553692 53.59815003]
Input : [3, 0.3, 3.1, 2.2]
Output : [19.08553692 0.34985881 21.19795128 8.0250135 ]