Как получить мощности значений массива поэлементно в Python-Pandas?

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

Давайте посмотрим, как получить мощности значений массива поэлементно. Dataframe / Series.pow () используется для расчета мощности элементов либо с самим собой, либо с другими предоставленными сериями. Эта функция применима только к действительным числам и не дает результатов для комплексных чисел.
Итак, посмотрим программы:

Example 1: The uni-dimensional arrays are mapped to a pandas series with either default numeric indices or custom indexes Then corresponding elements are raised to its own power. 

Python3

# import required modules
import numpy as np
import pandas as pd 
  
# create an array
sample_array = np.array([1, 2, 3])  
  
# uni dimensional arrays can be
# mapped to pandas series
sr = pd.Series(sample_array) 
  
print ("Original Array :")
print (sr)
  
# calculating element-wise power 
power_array = sr.pow(sr)
  
print ("Element-wise power array")
print (power_array)

Выход:

Example 2: Powers can also be computed for floating-point decimal numbers. 

Python3

# module to work with arrays in python
import array
  
# module required to compute power
import pandas as pd
  
# creating a 1-dimensional floating 
# point array containing three elements
sample_array = array.array("d"
                           [1.1, 2.0, 3.5])  
  
# uni dimensional arrays can 
# be mapped to pandas series
sr = pd.Series(sample_array) 
  
print ("Original Array :")
print (sr)
  
# computing power of each 
# element with itself 
power_array = sr.pow(sr)
  
print ("Element-wise power array")
print (power_array)

Выход:

Example 3: The Multi-dimensional arrays can be mapped to pandas data frames. The data frame then contains each cell comprising a numeric (integer or floating-point numbers) which can be raised to its own individual powers. 

Python3

# module to work with 
# arrays in python
import array
  
# module required to 
# compute power
import pandas as pd
  
# 2-d matrix containing 
# 2 rows and 3 columns
df = pd.DataFrame({"X": [1,2],
                   "Y": [3,4],
                   "Z": [5,6]});
  
print ("Original Array :")
print(df)
  
# power function to calculate
# power of data frame elements
# with itself
power_array = df.pow(df)
  
print ("Element-wise power array")
print (power_array)

Выход:

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

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