Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

numpy

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?

like image 884
Huey Avatar asked Jun 16 '16 18:06

Huey


People also ask

What does [: None mean in NumPy?

None is an alias for NP. newaxis. It creates an axis with length 1.

What does array [:] do in python?

basically used in array for slicing , understand bracket accept variable that mean value or key to display, and " : " is used to limit or slice the entire array into packets .

Can NumPy array have none?

No, it is an array of Nones. It is an array of exactly one None.

How do I randomly select from a NumPy array?

choice() function is used to get random elements from a NumPy array. It is a built-in function in the NumPy package of python. Parameters: a: a one-dimensional array/list (random sample will be generated from its elements) or an integer (random samples will be generated in the range of this integer)


4 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]])
like image 144
hpaulj Avatar answered Oct 19 '22 20:10

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]]    
like image 27
GWW Avatar answered Oct 19 '22 21:10

GWW


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.

like image 13
john ktejik Avatar answered Oct 19 '22 21:10

john ktejik


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

like image 6
CodingYourLife Avatar answered Oct 19 '22 21:10

CodingYourLife