I read the document about numpy.c_ many times but still confused. It is said -- "Translates slice objects to concatenation along the second axis." in the following document. Could anyone clarify in the example below, what is slice objects, and what is 2nd axis? I see they are all one dimension and confused where the 2nd axis coming from.
Using Python 2.7 on Windows.
http://docs.scipy.org/doc/numpy-1.6.0/reference/generated/numpy.c_.html#numpy.c_
>>> np.c_[np.array([[1,2,3]]), 0, 0, np.array([[4,5,6]])]
array([[1, 2, 3, 0, 0, 4, 5, 6]])
np.c_
is another way of doing array concatenateThe output shape is (1,8) in both cases; the concatenation was on axis=1, the 2nd axis.
c_
took care of expanding the dimensions of the0
tonp.array([[0]])
, the 2d (1,1) needed to concatenate.np.c_
(andnp.r_
) is actually a class object with a__getitem__
method, so it works with the[]
syntax. Thenumpy/lib/index_tricks.py
source file is instructive reading.Note that the
row
version works with the : slice syntax, producing a 1d (8,) array (same numbers, but in 1d)np.c_
is a convenience, but not something you are required to understand. I think being able to work withconcatenate
directly is more useful. It forces you to think explicitly about the dimensions of the inputs.[[1,2,3]]
is actually a list - a list containing one list.np.array([[1,2,3]])
is a 2d array with shape (1,3).np.arange(1,4)
produces a (3,) array with the same numbers.np.arange(1,4)[None,:]
makes it a (1,3) array.slice(1,4)
is a slice object.np.r_
andnp.c_
can turn a slice object into a array - by actually usingnp.arange
.And to get back to the original example (which might not be the best):
==========
For
np.c_
the 1st dimension of both needs to match.In the
learn
example,n_samples
is the 1st dim ofX
(rows), and therandn
also needs to have that many rows.np.concatenate([(X, randn(n_samples...)], axis=1)
should work just as well here. A little wordier, but functionally the same.