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


Python Numpy recarray.all()用法及代码示例


在numpy中,数组可能具有包含字段的数据类型,类似于电子表格中的列。一个例子是[(a, int), (b, float)],其中数组中的每个条目都是一对(int,float)。通常,这些属性是使用字典查找(例如,arr['a'] and arr['b']
记录数组允许使用以下方式将字段作为数组的成员进行访问arr.a and arr.b。如果记录数组中的所有元素均评估为True,则numpy.recarray.all()函数将返回True。

用法: numpy.recarray.all(axis=None, out=None, keepdims=False)

参数:
axis:[无或int或int的元组,可选]执行逻辑AND归约的一个或多个轴。
out :[ndarray,可选]将结果存储到的位置。
->如果提供,则必须具有广播输入的形状。
->如果未提供或没有,则返回新分配的数组。
keepdims :[bool,可选]如果将其设置为True,则缩小的轴将保留为尺寸为1的尺寸。使用此选项,结果将针对输入数组正确广播。
如果传递了默认值,则keepdims不会传递给ndarray的所有子类方法,但是任何非缺省值都将传递。如果子类sum方法未实现keepdims,则将引发任何异常。


返回:[ndarray,bool]如果所有元素的评估结果为True,则返回True。

代码1:

# Python program explaining 
# numpy.recarray.all() method  
  
# importing numpy as geek 
import numpy as geek 
  
# creating input array with 2 different field  
in_arr = geek.array([(5.0, 2), (3.0, 4)], 
         dtype =[('a', float), ('b', int)]) 
print ("Input array:", in_arr) 
   
# convert it to a record array, using arr.view(np.recarray) 
rec_arr = in_arr.view(geek.recarray) 
print("Record array of float:", rec_arr.a) 
print("Record array of int:", rec_arr.b) 
  
# applying recarray.all methods to float record array 
out_arr = geek.recarray.all(rec_arr.a) 
print ("Output array:", out_arr)  
  
# applying recarray.all methods to int record array 
out_arr = geek.recarray.all(rec_arr.b) 
print ("Output array:", out_arr) 
输出:
Input array: [(5.0, 2) (3.0, 4)]
Record array of float: [ 5.  3.]
Record array of int: [2 4]
Output array: True
Output array: True

代码2:

如果我们申请numpy.recarray.all()到整个记录数组,那么它将给Type error因为数组是灵活类型或混合类型。

# Python program explaining 
# numpy.recarray.all() method  
  
# importing numpy as geek 
import numpy as geek 
  
# creating input array with 2 different field  
in_arr = geek.array([(5.0, 2), (3.0, 4)], 
         dtype =[('a', float), ('b', int)]) 
print ("Input array:", in_arr)  
  
# convert it to a record array, using arr.view(np.recarray) 
rec_arr = in_arr.view(geek.recarray) 
print("Record array ", rec_arr) 
  
# applying recarray.all methods to  record array 
out_arr = geek.recarray.all(rec_arr) 
print ("Output array:", out_arr)  
输出:
TypeError:cannot perform reduce with flexible type


相关用法


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