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>Refer to numpy reference document (<a href="http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html" rel="nofollow">http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html</a>),
Each newaxis object in the selection tuple serves to expand the dimensions of the resulting selection by one unit-length dimension. The added dimension is the position of the newaxis object in the selection tuple.</p>

<ol>
<li>Here, each bracket represent one dimension, extra bracket is caused by newaxis.</li>
<li>1L means the dimension increased by newaxis.</li>
</ol>

python arrays numpy matrix