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>What you're trying to do is not actually a reshape, as you alter the structure of the data.
Make a new array with the shape you want:</p>

<pre><code>A = np.zeros(myshape)
B = np.zeros(myshape)
</code></pre>

<p>and then index those arrays</p>

<pre><code>n = 0
for i_a, i_b in zip(a, b):
    A[n, i_a - 1] = 1
    B[n, i_b - 1] = 1
    n += 1 
</code></pre>

<p>The <code>i_a/i_b - 1</code> in the assignment is only there to make 1 index the 0th element. This also only works if <code>a</code> and <code>b</code> have the same length. Make this two loops if they are not the same length.
There might be a more elegant solution but this should get the job done :)</p>

python numpy reshape encoder