How to reshape a 1-d array into a an array of shape (1,4,5)?
<p>I have these vectors :</p>

<pre><code>a = [1,2,3,4]
b = [1,2,3,5]
</code></pre>

<p>and I could like to have this at the end :</p>

<pre><code>A = [ [1,0,0,0,0]
      [0,1,0,0,0]
      [0,0,1,0,0]
      [0,0,0,1,0] ]

B = [ [1,0,0,0,0]
      [0,1,0,0,0]
      [0,0,1,0,0]
      [0,0,0,0,1] ]
</code></pre>

<p>I have been using np.reshape from python this way:</p>

<pre><code>A = np.reshape(a,(1,4,1))
B = np.reshape(b,(1,4,1))
</code></pre>

<p>And it does just partially the job as I have the following result:</p>

<pre><code>A = [[1]
     [2]
     [3]
     [4]]

B = [[1]
     [2]
     [3]
     [5]]  
</code></pre>

<p>Ideally I would like something like this:</p>

<pre><code>A = np.reshape(a,(1,4,(1,5)) 
</code></pre>

<p>but when reading the docs, this is not possible.</p>

<p>Thanks in advance for your help</p>

<p>Alternatively, numpy can assign value to multiple indexes on rows/columns in one go, example:</p>

<pre><code>In [1]: import numpy as np

In [2]: b = [1,2,3,5]
   ...: 
   ...: 

In [3]: zero = np.zeros([4,5])

In [4]: brow, bcol = range(len(b)), np.array(b) -1  # logical transform

In [5]: zero[brow, bcol] = 1

In [6]: zero
Out[6]: 
array([[ 1.,  0.,  0.,  0.,  0.],
       [ 0.,  1.,  0.,  0.,  0.],
       [ 0.,  0.,  1.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  1.]])
</code></pre>

python numpy reshape encoder