在Numpy中计算数组与另一个数组逐元素的逻辑或
要在Python Numpy中计算数组与另一个数组逐元素的逻辑或,可以使用**numpy.logical_or()**方法。返回值为True或False。返回值是应用于x1和x2元素的逻辑或运算的布尔结果;形状由广播确定。如果x1和x2都是标量,则这是一个标量。
out是结果存储到的位置。如果提供,它必须具有输入广播到的形状。如果没有提供或为None,则返回一个新分配的数组。元组(只能作为关键字参数)的长度必须等于输出的数量。
步骤
首先,导入所需的库:
import numpy as np
使用array()方法创建两个二维numpy数组。我们已插入元素:
arr1 = np.array([[True, False, False], [False, True, True]]) arr2 = np.array([[True, False, True], [True, True, False]])
显示数组:
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)要计算数组与另一个数组逐元素的逻辑或,可以使用numpy.logical_or()方法。返回值为True或False:
print("
Result (OR)...
",np.logical_or(arr1, arr2))示例
import numpy as np
# Creating two 2D numpy array using the array() method
# We have inserted elements
arr1 = np.array([[True, False, False], [False, True, True]])
arr2 = np.array([[True, False, True], [True, True, False]])
# 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 compute the truth value of an array OR another array elementwise, use the numpy.logical_or() method in Python Numpy
# Return value is either True or False
print("
Result (OR)...
",np.logical_or(arr1, arr2))输出
Array 1... [[ True False False] [False True True]] Array 2... [[ True False True] [ True True False]] Our Array 1 type... bool Our Array 2 type... bool Our Array 1 Dimensions... 2 Our Array 2 Dimensions... 2 Our Array 1 Shape... (2, 3) Our Array 2 Shape... (2, 3) Result (OR)... [[ True False True] [ True True True]]
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP