Numpy 數組和dtype的一個使用誤區


首先自定義三種類型(如下代碼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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM