[NumPy-Tickets] [NumPy] #1518: numpy.concat does not appear to work across broadcast axes
NumPy Trac
numpy-tickets@scipy....
Thu Jun 24 03:48:00 CDT 2010
#1518: numpy.concat does not appear to work across broadcast axes
--------------------+-------------------------------------------------------
Reporter: eob | Owner: somebody
Type: defect | Status: new
Priority: normal | Milestone: 2.0.0
Component: Other | Version: 1.4.0
Keywords: |
--------------------+-------------------------------------------------------
Comment(by pv):
If you mean this,
{{{
>>> x = np.array([1,2,3,4])
>>> y = np.array([[1,2],[3,4],[5,6],[7,8]])
>>> np.concatenate((y, np.tile(x, (4,1))), axis=1)
array([[1, 2, 1, 2, 3, 4],
[3, 4, 1, 2, 3, 4],
[5, 6, 1, 2, 3, 4],
[7, 8, 1, 2, 3, 4]])
}}}
you can do it without copies:
{{{
def broadcast_view(x, ref):
"""Broadcast unit dimensions in `x` to match those in `ref` without
copies"""
strides = [0 if x.shape[j] == 1 else x.strides[j] for j in
range(x.ndim)]
shape = [ref.shape[j] if x.shape[j] == 1 else x.shape[j] for j in
range(x.ndim)]
from numpy.lib.stride_tricks import as_strided
return as_strided(x, shape=shape, strides=strides)
>>> x = np.array([1,2,3,4])
>>> y = np.array([[1,2],[3,4],[5,6],[7,8]])
>>> np.concatenate((y, broadcast_view(x[None,:], y)), axis=1)
array([[1, 2, 1, 2, 3, 4],
[3, 4, 1, 2, 3, 4],
[5, 6, 1, 2, 3, 4],
[7, 8, 1, 2, 3, 4]])
>>> broadcast_view(x[None,:], y).base.base.base is x
True
}}}
But yes, I guess `np.concatenate` should do this automatically.
--
Ticket URL: <http://projects.scipy.org/numpy/ticket/1518#comment:3>
NumPy <http://projects.scipy.org/numpy>
My example project
More information about the NumPy-Tickets
mailing list