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

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.


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]]    

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]])

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...

Tags:

Python

Numpy