Use of None in Array indexing in Python

This question has been asked and answered on the Theano mailing list, but is actually about the basics of numpy indexing.

Here are the question and answer
https://groups.google.com/forum/#!topic/theano-users/jq92vNtkYUI

For completeness, here is another explanation: slicing with None adds an axis to your array, see the relevant numpy documentation, because it behaves the same in both numpy and Theano:

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

Note that np.newaxis is None:

import numpy as np
a = np.arange(30).reshape(5, 6)

print a.shape  # yields (5, 6)
print a[np.newaxis, :, :].shape  # yields (1, 5, 6)
print a[:, np.newaxis, :].shape  # yields (5, 1, 6)
print a[:, :, np.newaxis].shape  # yields (5, 6, 1)

Typically this is used to adjust shapes to be able to broadcast to higher dimensions. E.g. tiling 7 times in the middle axis can be achieved as

b = a[:, np.newaxis] * np.ones((1, 7, 1))

print b.shape  # yields (5, 7, 6), 7 copies of a along the second axis

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)