返回两个掩码数组的点积,并设置NumPy中是否传播掩码数据
要返回两个掩码数组的点积,请在 Python NumPy 中使用 **ma.dot()** 方法。“**strict**” 参数设置是否传播掩码数据 (True) 或将其设置为 0 (False) 以进行计算。
此函数等效于 numpy.dot,它考虑了掩码值。“strict” 和“out” 的位置与方法版本中的位置不同。为了保持与相应方法的兼容性,建议将可选参数视为仅限关键字参数。在某些时候,这可能是强制性的。
strict 参数设置是否传播掩码数据 (True) 或将其设置为 0 (False) 以进行计算。默认为 False。传播掩码意味着如果掩码值出现在行或列中,则整个行或列都被视为掩码。
输出参数表明它必须具有与未使用它时返回的完全相同的类型。特别是,它必须具有正确的类型,必须是 C 连续的,并且其 dtype 必须是 dot(a,b) 返回的 dtype。这是一个性能特性。因此,如果这些条件不满足,则会引发异常,而不是尝试灵活处理。
步骤
首先,导入所需的库 -
import numpy as np import numpy.ma as ma
创建数组 1,一个使用 numpy.arange() 方法的 3x3 数组,其元素为 int -
arr1 = np.arange(9).reshape((3,3))
print("Array1...
", arr1)
print("
Array type...
", arr1.dtype)# 创建掩码数组 1 -
arr1 = ma.array(arr1)
掩码数组 1 -
arr1[0, 1] = ma.masked arr1[1, 1] = ma.masked
显示掩码数组 1 -
print("
Masked Array1...
",arr1)
创建数组 2,另一个使用 numpy.arange() 方法的 3x3 数组,其元素为 int -
arr2 = np.arange(9).reshape((3,3))
print("
Array2...
", arr2)
print("
Array type...
", arr2.dtype)创建掩码数组 2
arr2 = ma.array(arr2)
掩码数组 2 -
arr2[2, 1] = ma.masked arr2[2, 2] = ma.masked
显示掩码数组 2 -
print("
Masked Array2...
",arr2)
要返回两个掩码数组的点积,请在 Python NumPy 中使用 ma.dot() 方法。“strict” 参数设置是否传播掩码数据 (True) 或将其设置为 0 (False) 以进行计算 -
print("
Result of dot product...
",np.ma.dot(arr1, arr2, strict=True))示例
import numpy as np
import numpy.ma as ma
# Array 1
# Creating a 3x3 array with int elements using the numpy.arange() method
arr1 = np.arange(9).reshape((3,3))
print("Array1...
", arr1)
print("
Array type...
", arr1.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr1.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr1.shape)
# Get the number of elements of the Array
print("
Elements in the Array...
",arr1.size)
# Create a masked array
arr1 = ma.array(arr1)
# Mask Array1
arr1[0, 1] = ma.masked
arr1[1, 1] = ma.masked
# Display Masked Array 1
print("
Masked Array1...
",arr1)
# Array 2
# Creating another 3x3 array with int elements using the numpy.arange() method
arr2 = np.arange(9).reshape((3,3))
print("
Array2...
", arr2)
print("
Array type...
", arr2.dtype)
# Get the dimensions of the Array
print("
Array Dimensions...
",arr2.ndim)
# Get the shape of the Array
print("
Our Array Shape...
",arr2.shape)
# Get the number of elements of the Array
print("
Elements in the Array...
",arr2.size)
# Create a masked array
arr2 = ma.array(arr2)
# Mask Array2
arr2[2, 1] = ma.masked
arr2[2, 2] = ma.masked
# Display Masked Array 2
print("
Masked Array2...
",arr2)
# To return the dot product of two masked arrays, use the ma.dot() method in Python Numpy
# The "strict" parameter sets whether masked data is propagated (True) or set to 0 (False) for the computation
print("
Result of dot product...
",np.ma.dot(arr1, arr2, strict=True))输出
Array1... [[0 1 2] [3 4 5] [6 7 8]] Array type... int64 Array Dimensions... 2 Our Array Shape... (3, 3) Elements in the Array... 9 Masked Array1... [[0 -- 2] [3 -- 5] [6 7 8]] Array2... [[0 1 2] [3 4 5] [6 7 8]] Array type... int64 Array Dimensions... 2 Our Array Shape... (3, 3) Elements in the Array... 9 Masked Array2... [[0 1 2] [3 4 5] [6 -- --]] Result of dot product... [[-- -- --] [-- -- --] [69 -- --]]
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP