how to copy numpy array value into higher dimensions
<p>I have a (w,h) np array in 2d. I want to make a 3d dimension that has a value greater than 1 and copy its value over along the 3rd dimensions. I was hoping broadcast would do it but it can't. This is how i'm doing it</p>

<pre><code>arr = np.expand_dims(arr, axis=2)
arr = np.concatenate((arr,arr,arr), axis=2)
</code></pre>

<p>is there a a faster way to do so?</p>

<p>You can <em>push</em> all dims forward, introducing a singleton dim/new axis as the last dim to create a <code>3D</code> array and then repeat three times along that one with <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.repeat.html" rel="nofollow"><code>np.repeat</code></a>, like so -</p>

<pre><code>arr3D = np.repeat(arr[...,None],3,axis=2)
</code></pre>

<p>Here's another approach using <a href="http://docs.scipy.org/doc/numpy/reference/generated/numpy.tile.html" rel="nofollow"><code>np.tile</code></a> -</p>

<pre><code>arr3D = np.tile(arr[...,None],3)
</code></pre>

python numpy