using newaxis in Python
<pre><code>A = np.array([[ 1, 2, 3], 
             [ 4, 5, 6], 
             [ 7, 8, 9],
             [10,11,12]])
B = A[:, np.newaxis]
print(B)
print(B.shape)
</code></pre>

<p>the output is</p>

<pre><code>[[[ 1  2  3]]

 [[ 4  5  6]]

 [[ 7  8  9]]

 [[10 11 12]]]

(4L, 1L, 3L)
</code></pre>

<p>I have two questions:</p>

<ol>
<li>Why are there extra brackets outside brackets, for example, <code>[[ 1  2  3]]</code>, so why it is not <code>[ 1  2  3]</code>?  </li>
<li>what does <code>1L</code> mean? It looks like to me that <code>B</code> is a 4X3 matrix, so why is it not <code>(4L, 3L)</code>?</li>
</ol>

<p>The "extra" bracket and the 1L are  there for the same reason: your use of <code>np.newaxis</code> inserted a new dimension of extent 1 into the array shapeâin other words, you transformed a two-dimensional 4x3 array into a three-dimensional 4x1x3 array. </p>

<p>Each of the 4 "rows" of <code>B</code>contains a two-dimensional 1x3 array, such as</p>

<pre><code>[[4,5,6]]
</code></pre>

<p>whereas each corresponding row of <code>A</code> was just a one-dimensional array of length 3:</p>

<pre><code>[4,5,6]
</code></pre>

<p>Hence the extra brackets. </p>

python arrays numpy matrix