使用 fmax() 比较两个 NumPy 数组并返回元素级最大值
要比较两个数组并返回元素级最大值,请在 Python NumPy 中使用 **numpy.fmax()** 方法。返回值为 True 或 False。
比较两个数组并返回一个包含元素级最大值的新数组。如果正在比较的元素之一是 NaN,则返回非 NaN 元素。如果两个元素都是 NaN,则返回第一个。后一种区别对于复数 NaN 非常重要,复数 NaN 定义为实部或虚部至少有一个是 NaN。其最终效果是在可能的情况下忽略 NaN。
步骤
首先,导入所需的库 -
import numpy as np
使用 array() 方法创建两个 2D NumPy 数组。我们插入了元素 -
arr1 = np.array([[6, 9, 13],[25, 11, 21]]) arr2 = np.array([[8, 12, 18],[22, 19, 26]])
显示数组 -
print("Array 1...
", arr1)
print("
Array 2...
", arr2)获取数组的类型 -
print("
Our Array 1 type...
", arr1.dtype)
print("
Our Array 2 type...
", arr2.dtype)获取数组的维度 -
print("
Our Array 1 Dimensions...
",arr1.ndim)
print("
Our Array 2 Dimensions...
",arr2.ndim)获取数组的形状 -
print("
Our Array 1 Shape...
",arr1.shape)
print("
Our Array 2 Shape...
",arr2.shape)要比较两个数组并返回元素级最大值,请在 Python NumPy 中使用 numpy.fmax() 方法。返回值为 True 或 False -
print("
Result (maximum)...
",np.fmax(arr1, arr2))
示例
import numpy as np
# Creating two 2D numpy array using the array() method
# We have inserted elements
arr1 = np.array([[6, 9, 13],[25, 11, 21]])
arr2 = np.array([[8, 12, 18],[22, 19, 26]])
# Display the arrays
print("Array 1...
", arr1)
print("
Array 2...
", arr2)
# Get the type of the arrays
print("
Our Array 1 type...
", arr1.dtype)
print("
Our Array 2 type...
", arr2.dtype)
# Get the dimensions of the Arrays
print("
Our Array 1 Dimensions...
",arr1.ndim)
print("
Our Array 2 Dimensions...
",arr2.ndim)
# Get the shape of the Arrays
print("
Our Array 1 Shape...
",arr1.shape)
print("
Our Array 2 Shape...
",arr2.shape)
# To compare two arrays and return the element-wise maximum, use the numpy.fmax() method in Python Numpy
# Return value is either True or False
print("
Result (maximum)...
",np.fmax(arr1, arr2))输出
Array 1... [[ 6 9 13] [25 11 21]] Array 2... [[ 8 12 18] [22 19 26]] Our Array 1 type... int64 Our Array 2 type... int64 Our Array 1 Dimensions... 2 Our Array 2 Dimensions... 2 Our Array 1 Shape... (2, 3) Our Array 2 Shape... (2, 3) Result (maximum)... [[ 8 12 18] [25 19 26]]
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP