Вычислить exp (x) - 1 для всех элементов в данном массиве NumPy

Опубликовано: 25 Марта, 2022

Экспоненциальная функция (e ^ x) - математическая функция, которая вычисляет e в степени x, где e - иррациональное число, приблизительно 2,71828183.

Его можно рассчитать с помощью метода numy.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.

Если скаляр предоставляется функции в качестве входных данных, тогда функция применяется к скаляру, и возвращается другой скаляр.

Example 1: If 3 was given as input then e^3 will returned as output.

Python

import numpy
  
n = 4
print(numpy.exp(n))
  
n = 5
print(numpy.exp(n))

Выход :

 54.598150033144236
148,4131591025766

Если ввод - это массив, функция применяется поэлементно. ex- np.exp ([1,2,3]) эквивалентно [np.exp (1), np.exp (2), np.exp (3)]

Method 1: Iterating over array 

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]

Method 2: Providing array as input to numpy.exp() function

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 ]

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.