首先自定义三种类型(如下代码1-3行),第一行使用scalar type,第2,3行使用Structured type。
提出问题:第5,7行同为创建数组,为什么第5行能work,而第7行会raise一个exception:expected an object with a buffer interface呢?
问题解答:原因在于创建numpy数组时,如果指定dtype是Structured type时,List(本例中[1,2])中的元素必须是元组类型的。但是第7行是一般的int型。所以出错。如果指定dtype是scalar type则没有此限制。
后续问题:根据以上分析结果,自然就有了第12行代码。但是错误依然:expected an object with a buffer interface。 但是如果自定义类型有两个或以上的字段(代码3,10),则毫无问题。为什么呢?
问题解答:问题出在元组这边,但定义的元组中只含有一个元素时,该元组就会退化为一般的数据类型而不会被作为元组对待。
解决办法就是如代码14行这样定义List.
另外一种解决方案就是:使用数组的view方法:a=np.array([1,2]).view(dt2)。 效果和代码14行是一样的。
1 dt1=np.dtype(np.int32) 2 dt2=np.dtype([('f1', np.int32)]) 3 dt3=np.dtype([('f1', np.int32), ('f2', np.int32)]) 4 5 a=np.array([1,2],dtype=dt1) 6 print a 7 a=np.array([1,2],dtype=dt2) 8 #error: expected an object with a buffer interface 9 10 a=np.array([(1,2),(3,4)],dtype=dt3) 11 print a 12 a=np.array([(1),(2)],dtype=dt2) 13 #error: expected an object with a buffer interface 14 a=np.array([(1,),(2,)],dtype=dt2)
其他dtype的使用范例:
1 import numpy as np 2 import sys 3 4 def dTypeTest(): 5 #Using dictionaries. Two fields named ‘gender’ and ‘age’: 6 student=np.dtype({'names':['name', 'age', 'weight'],'formats':['S32', 'i','f']}, align=True) 7 a=np.array([('zhang',65,123.5),('wang',23,122.5)],dtype=student) 8 print a 9 #Using array-scalar type: 10 a=np.array([1,2],dtype=np.dtype(np.int16)) 11 print a 12 13 #Structured type, one field name 'f1', containing int16: 14 #a=np.array([1,2],dtype=np.dtype([('f1', np.int16)])) 15 a=np.array([1,2]).view(np.dtype([('f1', np.int16)])) 16 print a 17 a=np.array([1,2]).view(np.dtype([('f1', np.int32)])) 18 print a 19 a=np.array([(1,),(2,)],dtype=np.dtype([('f1', np.int16)])) 20 print a 21 #below two lines of code is not working as [1,2] is not a list of tuple 22 #a=np.array([1,2],dtype=np.dtype([('f1', np.int16)])) 23 #a=np.array([(1),(2,)],dtype=np.dtype([('f1', np.int16)])) 24 25 #Structured type, one field named ‘f1’, in itself containing a structured type with one field: 26 dt=np.dtype([('f1', [('f1', np.int32)])]) 27 a=np.array([1,2]).view(dt) 28 print a 29 a=np.array([((1,),),((2,),)],dtype=dt) 30 print a 31 #below two lines of code is not working as (1,) is not same as the structure of dtype declared 32 #a=np.array([(1,),(2,)],dtype=dt) 33 34 #Structured type, two fields: the first field contains an unsigned int, the second an int32 35 dt=np.dtype([('f1', np.uint), ('f2', np.int32)]) 36 a=np.array([(1,2),(3,4)],dtype=dt) 37 print a 38 39 #Using array-protocol type strings: 40 dt=np.dtype([('a','f8'),('b','S10')]) 41 a=np.array([(3.14,'pi'),(2.17,'e')],dtype=dt) 42 print a 43 44 #Using comma-separated field formats. The shape is (2,3): 45 dt=np.dtype("i4, (2,3)f8") 46 a=np.array([(1,[[1,2,3],[4,5,6]]),(2,[[4,5,6],[1,2,3]])],dtype=dt) 47 print a 48 49 #Using tuples. int is a fixed type, 3 the field’s shape. void is a flexible type, here of size 10 50 dt=np.dtype([('hello',(np.int,3)),('world',np.void,10)]) 51 a=np.array([([1,2,3],'this is a')],dtype=dt) 52 print a 53 54 def main(): 55 dTypeTest() 56 57 if __name__ == "__main__": 58 main()