Creating a NumPy array directly from __array_interface__
<p>Suppose I have an <code>__array_interface__</code> dictionary and I would like to create a numpy view of this data from the dictionary itself. For example:</p>

<pre><code>buff = {'shape': (3, 3), 'data': (140546686381536, False), 'typestr': '&lt;f8'}
view = np.array(buff, copy=False)
</code></pre>

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

<pre><code>class numpy_holder(object):
    pass

holder = numpy_holder()
holder.__array_interface__ = buff
view = np.array(holder, copy=False)
</code></pre>

<p>This seems a bit roundabout. Am I missing a straightforward way to do this?</p>

<p>correction - with the right 'data' value your <code>holder</code> works in <code>np.array</code>:</p>

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

<p>There is a low level constructor, <code>np.ndarray</code> that takes a buffer parameter.  And a <code>np.frombuffer</code>.</p>

<p>But my impression is that <code>x.__array_interface__['data'][0]</code> 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.</p>

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

<p>===========</p>

<p>An example of <code>ndarray</code> with a <code>.data</code> attribute:</p>

<pre><code>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': [('', '&lt;i4')],
 'shape': (3, 4),
 'strides': None,
 'typestr': '&lt;i4',
 'version': 3}
In [305]: res.data
Out[305]: &lt;memory at 0xb13ef72c&gt;
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])
</code></pre>

<p>Both of these arrays are views.</p>

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

<pre><code>In [379]: holder=numpy_holder()
In [380]: buff={'data':res.data, 'shape':(4,3), 'typestr':'&lt;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]])
</code></pre>

python numpy pybinding