python dict到numpy结构化数组 [英] python dict to numpy structured array
问题描述
我有一个字典,需要将其转换为NumPy结构化数组.我正在使用arcpy函数 NumPyArraytoTable
,因此NumPy结构化数组是唯一可以使用的数据格式.
I have a dictionary that I need to convert to a NumPy structured array. I'm using the arcpy function NumPyArraytoTable
, so a NumPy structured array is the only data format that will work.
基于此线程:从字典写入numpy数组,这线程:如何将Python字典对象转换为numpy数组
Based on this thread: Writing to numpy array from dictionary and this thread: How to convert Python dictionary object to numpy array
我已经尝试过了:
result = {0: 1.1181753789488595, 1: 0.5566080288678394, 2: 0.4718269778030734, 3: 0.48716683119447185, 4: 1.0, 5: 0.1395076201641266, 6: 0.20941558441558442}
names = ['id','data']
formats = ['f8','f8']
dtype = dict(names = names, formats=formats)
array=numpy.array([[key,val] for (key,val) in result.iteritems()],dtype)
但是我不断得到expected a readable buffer object
以下方法有效,但很愚蠢,显然不适用于真实数据.我知道有一种更优雅的方法,我只是想不通.
The method below works, but is stupid and obviously won't work for real data. I know there is a more graceful approach, I just can't figure it out.
totable = numpy.array([[key,val] for (key,val) in result.iteritems()])
array=numpy.array([(totable[0,0],totable[0,1]),(totable[1,0],totable[1,1])],dtype)
推荐答案
您可以使用np.array(list(result.items()), dtype=dtype)
:
import numpy as np
result = {0: 1.1181753789488595, 1: 0.5566080288678394, 2: 0.4718269778030734, 3: 0.48716683119447185, 4: 1.0, 5: 0.1395076201641266, 6: 0.20941558441558442}
names = ['id','data']
formats = ['f8','f8']
dtype = dict(names = names, formats=formats)
array = np.array(list(result.items()), dtype=dtype)
print(repr(array))
收益
array([(0.0, 1.1181753789488595), (1.0, 0.5566080288678394),
(2.0, 0.4718269778030734), (3.0, 0.48716683119447185), (4.0, 1.0),
(5.0, 0.1395076201641266), (6.0, 0.20941558441558442)],
dtype=[('id', '<f8'), ('data', '<f8')])
如果您不想创建元组的中间列表list(result.items())
,则可以改用np.fromiter
:
If you don't want to create the intermediate list of tuples, list(result.items())
, then you could instead use np.fromiter
:
在Python2中:
array = np.fromiter(result.iteritems(), dtype=dtype, count=len(result))
在Python3中:
array = np.fromiter(result.items(), dtype=dtype, count=len(result))
为什么使用列表[key,val]
不起作用:
Why using the list [key,val]
does not work:
顺便说一下,您的尝试
numpy.array([[key,val] for (key,val) in result.iteritems()],dtype)
非常接近工作.如果将列表[key, val]
更改为元组(key, val)
,则它将起作用.当然,
was very close to working. If you change the list [key, val]
to the tuple (key, val)
, then it would have worked. Of course,
numpy.array([(key,val) for (key,val) in result.iteritems()], dtype)
与
numpy.array(result.items(), dtype)
在Python2中,或
in Python2, or
numpy.array(list(result.items()), dtype)
在Python3中.
np.array
处理列表与元组的方法不同: Robert Kern解释说:
np.array
treats lists differently than tuples: Robert Kern explains:
通常,元组被视为标量"记录,而列表则被视为 递归.此规则有助于numpy.array()找出哪个 序列是记录,是要重现的其他序列 之上;即哪些序列创建了另一个维度,哪些是 原子元素.
As a rule, tuples are considered "scalar" records and lists are recursed upon. This rule helps numpy.array() figure out which sequences are records and which are other sequences to be recursed upon; i.e. which sequences create another dimension and which are the atomic elements.
由于(0.0, 1.1181753789488595)
被认为是这些原子元素之一,因此它应该是一个元组,而不是列表.
Since (0.0, 1.1181753789488595)
is considered one of those atomic elements, it should be a tuple, not a list.
这篇关于python dict到numpy结构化数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!