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?
None is an alias for NP. newaxis. It creates an axis with length 1.
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 .
No, it is an array of Nones. It is an array of exactly one None.
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)
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]])
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]]
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.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With