在 NumPy 中返回数组与另一个数组元素级不相等的真值
要返回数组与另一个数组元素级不相等的真值,请在 Python NumPy 中使用 **numpy.not_equal()** 方法。返回值为 True 或 False。该函数返回一个输出数组,它是 x1 和 x2 的元素级比较。通常为布尔类型,除非传递 dtype=object。如果 x1 和 x2 都是标量,则为标量。
out 是存储结果的位置。如果提供,则其形状必须与输入广播到的形状一致。如果未提供或为 None,则返回一个新分配的数组。元组(仅可能作为关键字参数)的长度必须等于输出的数量。
条件在输入上广播。在条件为 True 的位置,out 数组将设置为 ufunc 结果。在其他地方,out 数组将保留其原始值。请注意,如果通过默认的 out=None 创建了一个未初始化的 out 数组,则其中条件为 False 的位置将保持未初始化状态。
步骤
首先,导入所需的库 -
import numpy as np
使用 array() 方法创建两个 2D NumPy 数组。我们插入了 int 类型的元素 &minusl;
arr1 = np.array([[98, 34, 61], [82, 69, 29]]) arr2 = np.array([[91, 34, 61], [81, 55, 32]])
显示数组 -
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.not_equal() 方法。返回值为 True 或 False -
print("
Result...
",np.not_equal(arr1, arr2))示例
import numpy as np
# Creating two 2D numpy array using the array() method
# We have inserted elements of int type
arr1 = np.array([[98, 34, 61], [82, 69, 29]])
arr2 = np.array([[91, 34, 61], [81, 55, 32]])
# 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 return the truth value of an array not equal to another elementwise, use the numpy.not_equal() method in Python Numpy
# Return value is either True or False
print("
Result...
",np.not_equal(arr1, arr2))输出
Array 1... [[98 34 61] [82 69 29]] Array 2... [[91 34 61] [81 55 32]] 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... [[ True False False] [ True True True]]
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP