Python Numpy recarray.dot()函数

Python Numpy recarray.dot()函数

在numpy中,数组可以有一个包含字段的数据类型,类似于电子表格中的列。一个例子是[(a, int), (b, float)] ,其中数组中的每个条目是一对(int, float)。通常情况下,这些属性使用字典查询,如arr[‘a’] 和 arr[‘b’] 。记录数组允许字段作为数组的成员被访问,使用arr.a和arr.b。

numpy.recarray.dot()函数返回两个记录数组的乘积。

语法: numpy.recarray.dot(arr, out=None)

参数:
arr :第二个记录数组。
out : [ndarray, optional] 一个储存结果的位置。
-> 如果提供,它必须有一个输入广播到的形状。
-> 如果没有提供或没有,将返回一个新分配的数组。

返回 : 一个容纳结果的新数组被返回,除非指定out,在这种情况下,它被返回。

代码#1:

# Python program explaining
# numpy.recarray.dot() method 
  
# importing numpy as geek
import numpy as geek
  
# creating 2 input array with 2 different field 
in_arr1 = geek.array([[(5.0, 2), (3.0, -4), (6.0, 9)],
                     [(9.0, 1), (5.0, 4), (-12.0, -7)]],
                     dtype =[('a', float), ('b', int)])
  
in_arr2 = geek.array([[(2.0, 1), (4.0, -3)],
                     [(8.0, 3), (6.0, 5)],
                     [(6.0, -5), (-5.0, 4)]],
                     dtype =[('a', float), ('b', int)])
  
print ("1st Input array : ", in_arr1)
print ("2nd Input array : ", in_arr2)
  
# convert it to a record array,
# using arr.view(np.recarray)
rec_arr1 = in_arr1.view(geek.recarray)
print("1st Record array of float: ", rec_arr1.a)
print("1st Record array of int: ", rec_arr1.b)
rec_arr2 = in_arr2.view(geek.recarray)
print("2nd Record array of float: ", rec_arr2.a)
print("2nd Record array of int: ", rec_arr2.b)
  
# applying recarray.dot methods
# between two float record array 
out_arr1 = rec_arr1.a.dot( rec_arr2.a)
print ("Output float array  : ", out_arr1) 
  
# applying recarray.dot methods
# between two int record array 
out_arr1 = rec_arr1.b.dot( rec_arr2.b)
print ("Output int array  : ", out_arr1) 

输出:

1st Input array :  [[(  5.,  2) (  3., -4) (  6.,  9)]
 [(  9.,  1) (  5.,  4) (-12., -7)]]
2nd Input array :  [[( 2.,  1) ( 4., -3)]
 [( 8.,  3) ( 6.,  5)]
 [( 6., -5) (-5.,  4)]]
1st Record array of float:  [[  5.   3.   6.]
 [  9.   5. -12.]]
1st Record array of int:  [[ 2 -4  9]
 [ 1  4 -7]]
2nd Record array of float:  [[ 2.  4.]
 [ 8.  6.]
 [ 6. -5.]]
2nd Record array of int:  [[ 1 -3]
 [ 3  5]
 [-5  4]]
Output float array  :  [[ 70.   8.]
 [-14. 126.]]
Output int array  :  [[-55  10]
 [ 48 -11]]

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

Numpy 数组操作