In numpy, what does selection by [:,None] do?

Question:

I’m taking the Udacity course on deep learning and I came across the following code:

def reformat(dataset, labels):
    dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)
    # Map 0 to [1.0, 0.0, 0.0 ...], 1 to [0.0, 1.0, 0.0 ...]
    labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
    return dataset, labels

What does labels[:,None] actually do here?

Asked By: Huey

||

Answers:

http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html

numpy.newaxis

The newaxis object can be used in all slicing operations to create an axis of length one. :const: newaxis is an alias for ‘None’, and ‘None’ can be used in place of this with the same result.

http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.expand_dims.html

Demonstrating with part of your code

In [154]: labels=np.array([1,3,5])

In [155]: labels[:,None]
Out[155]: 
array([[1],
       [3],
       [5]])
 
In [157]: np.arange(8)==labels[:,None]
Out[157]: 
array([[False,  True, False, False, False, False, False, False],
       [False, False, False,  True, False, False, False, False],
       [False, False, False, False, False,  True, False, False]], dtype=bool)

In [158]: (np.arange(8)==labels[:,None]).astype(int)
Out[158]: 
array([[0, 1, 0, 0, 0, 0, 0, 0],
       [0, 0, 0, 1, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 1, 0, 0]])
Answered By: hpaulj

None is an alias for NP.newaxis. It creates an axis with length 1. This can be useful for matrix multiplcation etc.

>>>> import numpy as NP
>>>> a = NP.arange(1,5)
>>>> print a
[1 2 3 4]
>>>> print a.shape
(4,)
>>>> print a[:,None].shape
(4, 1)
>>>> print a[:,None]
[[1]
 [2]
 [3]
 [4]]    
Answered By: GWW

I came here after having the exact same problem doing the same Udacity course. What I wanted to do is transpose the one dimensional numpy series/array which does not work with numpy.transpose([1, 2, 3]).
So I wanted to add you can transpose like this (source):

numpy.matrix([1, 2, 3]).T

It results in:

matrix([[1],
        [2],
        [3]])

which is pretty much identical (type is different) to:

x=np.array([1, 2, 3])
x[:,None]

But I think it’s easier to remember…

Answered By: CodingYourLife

to explain it in plain english, it allows operations between two arrays of different number of dimensions.

It does this by adding a new, empty dimension which will automagically fit the size of the other array.

So basically if:

Array1 = shape[100]
and
Array2 = shape[10,100]

Array1 * Array2 will normally give an error.

Array1[:,None] * Array2 will work.

Answered By: john ktejik

If you see code from experienced NumPy users, you will often see them use a special slicing syntax instead of calling reshape.

x = v[None, :]

or

x = v[:, None]

Those lines create a slice that looks at all of the items of v but asks NumPy to add a new dimension of size 1 for the associated axis.

(To clarify the answer by @GWW and the comment by @BlueRine S) While working with numpy arrays it is a good idea to clearly treat one dimensional arrays as row or column vectors. This has been pointed out by Andrew Ng also, to avoid bugs in the code.

>>> import numpy as NP
>>> a = NP.arange(1,5)
>>> a
array([1, 2, 3, 4])
>>> a.shape
(4,)
>>> a[:,None].shape
(4, 1)
>>> a[:,None]
array([[1],
       [2],
       [3],
       [4]])
>>> a[None,:].shape
(1, 4)
>>> a[None,:]
array([[1, 2, 3, 4]])
>>> np.reshape(a, (1, -1))
array([[1, 2, 3, 4]])
>>> np.reshape(a, (-1, 1))
array([[1],
       [2],
       [3],
       [4]])
Answered By: Hari
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.