直接从__array_interface__创建一个NumPy数组 [英] Creating a NumPy array directly from __array_interface__

查看:746
本文介绍了直接从__array_interface__创建一个NumPy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个__array_interface__字典,并且我想从字典本身中创建此数据的numpy视图.例如:

Suppose I have an __array_interface__ dictionary and I would like to create a numpy view of this data from the dictionary itself. For example:

buff = {'shape': (3, 3), 'data': (140546686381536, False), 'typestr': '<f8'}
view = np.array(buff, copy=False)

但是,这不起作用,因为np.array搜索缓冲区或数组接口作为属性.简单的解决方法可能如下:

However, this does not work as np.array searches for either the buffer or array interface as attributes. The simple workaround could be the following:

class numpy_holder(object):
    pass

holder = numpy_holder()
holder.__array_interface__ = buff
view = np.array(holder, copy=False)

这似乎有点回旋处.我想念一个简单的方法吗?

This seems a bit roundabout. Am I missing a straightforward way to do this?

推荐答案

更正-使用正确的数据"值,您的holdernp.array中工作:

correction - with the right 'data' value your holder works in np.array:

np.array绝对不起作用,因为它期望可迭代的东西,例如列表列表,并解析各个值.

np.array is definitely not going to work since it expects an iterable, some things like a list of lists, and parses the individual values.

有一个底层构造器np.ndarray,它带有一个缓冲区参数.还有np.frombuffer.

There is a low level constructor, np.ndarray that takes a buffer parameter. And a np.frombuffer.

但是我的印象是x.__array_interface__['data'][0]是数据缓冲区位置的整数表示,而不是直接指向缓冲区的指针.我只用它来验证视图共享相同的数据缓冲区,而不用它构造任何东西.

But my impression is that x.__array_interface__['data'][0] is a integer representation of the data buffer location, but not directly a pointer to the buffer. I've only used it to verify that a view shares the same databuffer, not to construct anything from it.

np.lib.stride_tricks.as_strided使用__array_interface__作为默认步幅和形状数据,但从数组而不是__array_interface__字典获取数据.

np.lib.stride_tricks.as_strided uses __array_interface__ for default stride and shape data, but gets the data from an array, not the __array_interface__ dictionary.

===========

===========

具有.data属性的ndarray的示例:

In [303]: res
Out[303]: 
array([[ 0, 20, 50, 30],
       [ 0, 50, 50,  0],
       [ 0,  0, 75, 25]])
In [304]: res.__array_interface__
Out[304]: 
{'data': (178919136, False),
 'descr': [('', '<i4')],
 'shape': (3, 4),
 'strides': None,
 'typestr': '<i4',
 'version': 3}
In [305]: res.data
Out[305]: <memory at 0xb13ef72c>
In [306]: np.ndarray(buffer=res.data, shape=(4,3),dtype=int)
Out[306]: 
array([[ 0, 20, 50],
       [30,  0, 50],
       [50,  0,  0],
       [ 0, 75, 25]])
In [324]: np.frombuffer(res.data,dtype=int)
Out[324]: array([ 0, 20, 50, 30,  0, 50, 50,  0,  0,  0, 75, 25])

这两个数组都是视图.

好的,对于您的holder类,我可以使用此res.data作为数据缓冲区来做同样的事情.您的班级创建一个object exposing the array interface.

OK, with your holder class, I can make the same thing, using this res.data as the data buffer. Your class creates an object exposing the array interface.

In [379]: holder=numpy_holder()
In [380]: buff={'data':res.data, 'shape':(4,3), 'typestr':'<i4'}
In [381]: holder.__array_interface__ = buff
In [382]: np.array(holder, copy=False)
Out[382]: 
array([[ 0, 20, 50],
       [30,  0, 50],
       [50,  0,  0],
       [ 0, 75, 25]])

这篇关于直接从__array_interface__创建一个NumPy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆