What does (n,) mean in the context of numpy and vectors?
<p>I've tried searching StackOverflow, googling, and even using symbolhound to do character searches, but was unable to find an answer. Specifically, I'm confused about Ch. 1 of Nielsen's <em>Neural Networks and Deep Learning</em>, where he says "It is assumed that the input <code>a</code> is an <code>(n, 1) Numpy ndarray</code>, not a <code>(n,) vector</code>."</p>

<p>At first I thought <code>(n,)</code> referred to the orientation of the array - so it might refer to a one-column vector as opposed to a vector with only one row. But then I don't see why we need <code>(n,)</code> and <code>(n, 1)</code> both - they seem to say the same thing. I know I'm misunderstanding something but am unsure.</p>

<p>For reference <code>a</code> refers to a vector of activations that will be input to a given layer of a neural network, before being transformed by the weights and biases to produce the output vector of activations for the next layer.</p>

<p>EDIT: This question equivocates between a "one-column vector" (there's no such thing) and a "one-column matrix" (does actually exist). Same for "one-row vector" and "one-row matrix". </p>

<p>A vector is only a list of numbers, or (equivalently) a list of scalar transformations on the basis vectors of a vector space. A vector might <em>look</em> like a matrix when we write it out, if it only has one row (or one column). Confusingly, we will sometimes refer to a "vector of activations" but actually mean "a single-row matrix of activation values transposed so that it is a single-column."</p>

<p>Be aware that in neither case are we discussing a one-dimensional vector, which would be a vector defined by only one number (unless, trivially, n==1, in which case the concept of a "column" or "row" distinction would be meaningless).</p>

<p>In <code>numpy</code> an array can have a number of different dimensions, 0, 1, 2 etc.</p>

<p>The typical 2d array has dimension <code>(n,m)</code> (this is a Python tuple).  We tend to describe this as having n rows, m columns.  So a <code>(n,1)</code> array has just 1 column, and a <code>(1,m)</code> has 1 row.</p>

<p>But because an array may have just 1 dimension, it is possible to have a shape <code>(n,)</code> (Python notation for a 1 element tuple: see <a href="https://wiki.python.org/moin/TupleSyntax" rel="nofollow">here</a> for more).</p>

<p>For many purposes <code>(n,)</code>, <code>(1,n)</code>, <code>(n,1)</code> arrays are equivalent (also <code>(1,n,1,1)</code> (4d)).  They all have <code>n</code> terms, and can be reshaped to each other.</p>

<p>But sometimes that extra <code>1</code> dimension matters.  A (1,m) array can multiply a (n,1) array to produce a (n,m) array.  A (n,1) array can be indexed like a (n,m), with 2 indices, <code>x[:,0]</code> where as a (n,) only accepts <code>x[0]</code>.</p>

<p>MATLAB matrices are always 2d (or higher).  So people transfering ideas from MATLAB tend to expect 2 dimensions.  There is a <code>np.matrix</code> subclass that supposed to imitate that.</p>

<p>For numpy programmers the distinctions between vector, row vector, column vector, matrix are loose and relatively unimportant.  Or the use is derived from the application rather than from <code>numpy</code> itself.  I think that's what's happening with this network book - the notation and expectations come from outside of <code>numpy</code>.</p>

<p>See as well this answer for how to interpret the shapes with respect to the data stored in <code>ndarrays</code>. It also provides insight on how to use <code>.reshape</code>: <a href="http://stackoverflow.com/a/22074424/3277902">http://stackoverflow.com/a/22074424/3277902</a> </p>

python numpy machine-learning neural-network