在NumPy中将底数提升到不同的指数
要将底数提升到不同的指数,请在Python中使用**numpy.power()**方法。这里,第一个参数是底数,第二个参数是指数。
将x1中的每个底数提升到x2中位置对应的幂。x1和x2必须可广播到相同的形状。整数类型提升到负整数幂将引发ValueError。负值提升到非整数值将返回nan。要获得复数结果,请将输入转换为复数,或指定dtype为复数。
该条件在输入上进行广播。在条件为True的位置,out数组将设置为ufunc结果。在其他地方,out数组将保留其原始值。请注意,如果通过默认的out=None创建一个未初始化的out数组,则其中条件为False的位置将保持未初始化状态。
步骤
首先,导入所需的库:
import numpy as np
创建一个数组:
arr = np.array([5, 10, 25, 30, 40, 50])
显示数组:
print("Array...
", arr)获取数组的类型:
print("
Our Array type...
", arr.dtype)
获取数组的维度:
print("
Our Array Dimension...
",arr.ndim)获取数组的形状:
print("
Our Array Shape...
",arr.shape)
设置不同的指数:
p = [2, 3, 2, 3, 2, 3]
要将底数提升到不同的指数,请在Python中使用numpy.power()方法。这里,第一个参数是底数,第二个参数是指数:
print("
Result...
",np.power(arr, p))
示例
import numpy as np
# Create an array
arr = np.array([5, 10, 25, 30, 40, 50])
# 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)
# Set the different exponent
p = [2, 3, 2, 3, 2, 3]
# To raise the bases to different exponents, use the numpy.power() method in Python
# Here, the 1st parameter is the base and the 2nd exponents
print("
Result...
",np.power(arr, p))输出
Array... [ 5 10 25 30 40 50] Our Array type... int64 Our Array Dimension... 1 Our Array Shape... (6,) Result... [ 25 1000 625 27000 1600 125000]
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP