您的位置:首页 > 编程语言 > Python开发

Numpy 数组和dtype的一个使用误区

2015-10-28 13:07 721 查看
首先自定义三种类型(如下代码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行是一样的。

dt1=np.dtype(np.int32)
dt2=np.dtype([('f1', np.int32)])
dt3=np.dtype([('f1', np.int32), ('f2', np.int32)])

a=np.array([1,2],dtype=dt1)
print a
a=np.array([1,2],dtype=dt2)
#error: expected an object with a buffer interface

a=np.array([(1,2),(3,4)],dtype=dt3)
print a
a=np.array([(1),(2)],dtype=dt2)
#error: expected an object with a buffer interface
a=np.array([(1,),(2,)],dtype=dt2)


其他dtype的使用范例:

import numpy as np
import sys

def dTypeTest():
#Using dictionaries. Two fields named ‘gender’ and ‘age’:
student=np.dtype({'names':['name', 'age', 'weight'],'formats':['S32', 'i','f']}, align=True)
a=np.array([('zhang',65,123.5),('wang',23,122.5)],dtype=student)
print a
#Using array-scalar type:
a=np.array([1,2],dtype=np.dtype(np.int16))
print a

#Structured type, one field name 'f1', containing int16:
#a=np.array([1,2],dtype=np.dtype([('f1', np.int16)]))
a=np.array([1,2]).view(np.dtype([('f1', np.int16)]))
print a
a=np.array([1,2]).view(np.dtype([('f1', np.int32)]))
print a
a=np.array([(1,),(2,)],dtype=np.dtype([('f1', np.int16)]))
print a
#below two lines of code is not working as [1,2] is not a list of tuple
#a=np.array([1,2],dtype=np.dtype([('f1', np.int16)]))
#a=np.array([(1),(2,)],dtype=np.dtype([('f1', np.int16)]))

#Structured type, one field named ‘f1’, in itself containing a structured type with one field:
dt=np.dtype([('f1', [('f1', np.int32)])])
a=np.array([1,2]).view(dt)
print a
a=np.array([((1,),),((2,),)],dtype=dt)
print a
#below two lines of code is not working as (1,) is not same as the structure of dtype declared
#a=np.array([(1,),(2,)],dtype=dt)

#Structured type, two fields: the first field contains an unsigned int, the second an int32
dt=np.dtype([('f1', np.uint), ('f2', np.int32)])
a=np.array([(1,2),(3,4)],dtype=dt)
print a

#Using array-protocol type strings:
dt=np.dtype([('a','f8'),('b','S10')])
a=np.array([(3.14,'pi'),(2.17,'e')],dtype=dt)
print a

#Using comma-separated field formats. The shape is (2,3):
dt=np.dtype("i4, (2,3)f8")
a=np.array([(1,[[1,2,3],[4,5,6]]),(2,[[4,5,6],[1,2,3]])],dtype=dt)
print a

#Using tuples. int is a fixed type, 3 the field’s shape. void is a flexible type, here of size 10
dt=np.dtype([('hello',(np.int,3)),('world',np.void,10)])
a=np.array([([1,2,3],'this is a')],dtype=dt)
print a

def main():
dTypeTest()

if __name__ == "__main__":
main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: