返回 NumPy 中两个掩码数组的点积


要返回两个掩码数组的点积,请在 Python NumPy 中使用 **ma.dot()** 方法。此函数等效于 numpy.dot,它考虑了掩码值。strict 和 out 的位置与方法版本不同。为了保持与相应方法的兼容性,建议将可选参数视为仅限关键字参数。在某些时候,这可能是强制性的。

strict 参数设置是否传播掩码数据(True)或将其设置为 0(False)以进行计算。默认为 False。传播掩码意味着如果掩码值出现在行或列中,则整个行或列都被视为掩码。

output 参数表明它必须具有与不使用它时返回的完全相同的类型。特别是,它必须具有正确的类型,必须是 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)

获取数组的维度 -

print("Array Dimensions...",arr1.ndim)

获取数组的形状 -

print("Our Array Shape...",arr1.shape)

获取数组的元素数量 -

print("Elements in the Array...",arr1.size)

创建掩码数组 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() 方法 -

print("Result of dot product...",np.ma.dot(arr1, arr2))

示例

Open Compiler
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 print("Result of dot product...",np.ma.dot(arr1, arr2))

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

输出

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...
[[12 0 0]
[30 3 6]
[69 34 47]]

更新于: 2022年2月5日

134 次查看

启动您的 职业生涯

通过完成课程获得认证

开始
广告