当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Numpy MaskedArray.dot()用法及代码示例


numpy.MaskedArray.dot()函数用于计算两个蒙版阵列的点积。

用法: numpy.ma.dot(arr1, arr2, strict=False)

参数:
arr1,arr2:[ndarray]输入数组。
strict :[布尔,可选]是传播掩码数据(真)还是将其设置为0(假)以进行计算。默认值为False。


Return :[ndarray] arr1和arr2的点积。

代码1:

# Python program explaining 
# numpy.MaskedArray.dot() method  
    
# importing numpy as geek   
# and numpy.ma module as ma  
import numpy as geek  
import numpy.ma as ma  
    
# creating input arrays    
in_arr1 = geek.array([[1, 2], [ 3, 4]]) 
print ("1st Input array:", in_arr1)  
    
in_arr2 = geek.array([[-1, -2], [ -3, -4]])  
print ("2nd Input array:", in_arr2)  
    
      
# Now we are creating  masked array.   
# by making  entry as invalid    
mask_arr1 = ma.masked_array(in_arr1, mask = [[ 1, 0], [ 0, 1]])   
print ("1st Masked array:", mask_arr1)  
    
mask_arr2 = ma.masked_array(in_arr2, mask =[[ 0, 1], [ 0, 0]])   
print ("2nd Masked array:", mask_arr2)  
    
# applying MaskedArray.dot methods   
# to masked array   
out_arr = ma.dot(mask_arr1, mask_arr2)   
print ("Dot product of two arrays:", out_arr)      
输出:
1st Input array: [[1 2]
 [3 4]]
2nd Input array: [[-1 -2]
 [-3 -4]]
1st Masked array: [[-- 2]
 [3 --]]
2nd Masked array: [[-1 --]
 [-3 -4]]
Dot product of two arrays: [[-6 -8]
 [-3 --]]

代码2:

# Python program explaining 
# numpy.MaskedArray.dot() method  
    
# importing numpy as geek   
# and numpy.ma module as ma  
import numpy as geek  
import numpy.ma as ma  
    
# creating input arrays    
in_arr1 = geek.array([[1, 2], [ 3, -1], [ 5, -3]]) 
print ("1st Input array:", in_arr1)  
    
in_arr2 = geek.array([[1, 0, 3], [ 4, 1, 6]])  
print ("2nd Input array:", in_arr2)  
    
      
# Now we are creating  masked array.   
# by making  entry as invalid    
mask_arr1 = ma.masked_array(in_arr1, mask = [[ 1, 0], [ 0, 1], [ 0, 0]])   
print ("1st Masked array:", mask_arr1)  
    
mask_arr2 = ma.masked_array(in_arr2, mask =[[ 0, 0, 0], [ 0, 0, 1]])   
print ("2nd Masked array:", mask_arr2)  
    
# applying MaskedArray.dot methods   
# to masked array   
out_arr = ma.dot(mask_arr1, mask_arr2)   
print ("Dot product of two arrays:", out_arr)      
输出:
1st Input array: [[ 1  2]
 [ 3 -1]
 [ 5 -3]]
2nd Input array: [[1 0 3]
 [4 1 6]]
1st Masked array: [[-- 2]
 [3 --]
 [5 -3]]
2nd Masked array: [[1 0 3]
 [4 1 --]]
Dot product of two arrays: [[8 2 --]
 [3 0 9]
 [-7 -3 15]]



相关用法


注:本文由纯净天空筛选整理自jana_sayantan大神的英文原创作品 Numpy MaskedArray.dot() function | Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。