对NumPy数组中的每个元素进行立方运算
要对数组中的每个元素进行立方运算(逐元素运算),请使用Python中的**numpy.power()**方法。这里,第一个参数是底数,第二个参数是指数。由于我们想要立方,因此指数为3。
将x1中的每个底数提升到x2中位置对应的幂。x1和x2必须能够广播到相同的形状。整数类型提升到负整数幂将引发ValueError。负值提升到非整数值将返回nan。要获得复数结果,请将输入转换为复数,或将dtype指定为复数。
步骤
首先,导入所需的库:
import numpy as np
创建一个数组:
arr = np.array([5, 10, 25, 7, 9])
显示数组:
print("Array...
", arr)获取数组的类型:
print("
Our Array type...
", arr.dtype)
获取数组的维度:
print("
Our Array Dimension...
",arr.ndim)获取数组的形状:
print("
Our Array Shape...
",arr.shape)要对数组中的每个元素进行立方运算(逐元素运算),请使用Python中的numpy.power()方法。这里,第一个参数是底数,第二个参数是指数。由于我们想要立方,因此指数为3:
print("
Result...
",np.power(arr, 3))示例
import numpy as np
# Create an array
arr = np.array([5, 10, 25, 7, 9])
# Display the array
print("Array...
", arr)
# Get the type of the array
print("
Our Array type...
", arr.dtype)
# Get the dimensions of the Array
print("
Our Array Dimension...
",arr.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr.shape)
# To cube each element in an array., element-wise, use the numpy.power() method in Python
# Here, the 1st parameter is the base and the 2nd exponents
# Since, we want the cube, the exponent is 3
print("
Result...
",np.power(arr, 3))输出
Array... [ 5 10 25 7 9] Our Array type... int64 Our Array Dimension... 1 Our Array Shape... (5,) Result... [ 125 1000 15625 343 729]
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP