sucheta
sucheta

Reputation: 11

When indexing a one dimensional array, what does None mean, and why does the shape change?

a = np.array([1, 2, 3])
a.shape
(3,)# why not(1,3)?
v1 = np.array([4, 5, 6])[None,:] # what does none over here mean?
v1.shape
(1,3) #why (1,3) here?

Why does shape show different answers for 1-d array and what does none over here mean?

Upvotes: 0

Views: 1499

Answers (2)

fountainhead
fountainhead

Reputation: 3722

In some programming languages or frameworks, a one-dimensional array is considered to be the same as a two-dimensional array in which one of the dimensions has a length of 1.

But not in numpy.

In numpy, a one-dimensional array is something:

  1. Which prints (n,) as its shape (where n is the length of its only dimension)
  2. Whose elements require only one index to be accessed individually.

A numpy two-dimensional array is something:

  1. Which prints (m,n) as its shape
  2. Whose elements require two indices to be accessed individually.

The shape attribute of a numpy ndarray tells you the length of each dimension of the array.

In a = np.array([1, 2, 3]) you are creating a one-dimensional array, and the length of that only dimension is 3.

To create the "nearest" two-dimensional array, you would have to call np.array([[1, 2, 3]]).

Notice the consecutive [[ at the beginning, and the consecutive ]] at the end. These double-brackets will also appear when you print this two-dimensional array.

The number of consecutive brackets at the beginning (or at the end) is usually how you visually figure out the number of dimensions in the ndarray.

The use of None as an index in numpy

Normally, in Python, when you slice a list, or index into a list, you are probably used to getting a result that has either the same number of dimensions or fewer number of dimensions.

But in numpy, the notation of indexing can sometimes be used to build a higher-dimensional ndarray.

This is what you are doing here, when you take the result of np.array([4, 5, 6]) and then index it with [None, :].

In numpy, when None is specified as an index, it is the same as specifying numpy.newaxis. (It is just a more compact alternative to numpy.newaxis)

And, specifying numpy.newaxis, as an index, at a particular position (in your case, position 0 among the indices), is like saying, "I want to add one more dimension, having a length of 1, at this particular position"

(Note that in numpy, the words axis and dimension are used interchangeably in the documentation).

In your case, np.array([4, 5, 6]) first returns a one-dimensional array, and the length of that sole dimension is 3 (since there are 3 elements).

When you then index into this one-dimensional array with [None, :], your first index None is actually saying that you want to prefix a new dimension, having a length of 1, at position 0 (that is, position 0 among the indices). In other words, you are saying that you want to first create an outer array of length 1, and the only element of this outer array would be an inner array, and the shape and content of that inner array is further specified by the indices that follow your None (in this case, just the :).

(Since the length of this newly prefixed dimension is always 1, the only valid index that you can use for this new dimension will be 0)

Your second index (:) is actually specifying what you want to get, when you use the index 0 on this newly prefixed dimension. By specifying : as your second index, you are effectively saying that you want the entire result of np.array([4, 5, 6]) to be present at position 0 of the newly prefixed dimension.

Upvotes: 2

Jim Todd
Jim Todd

Reputation: 1588

The slicing here is done with np.newaxis object. It can take a default value of None.

np.array([1, 2, 3]) basically contains 3 columns and 1 row. Usually tuple with a single value is ended with ',' at last.

np.array([4, 5, 6])[None,:] makes use of np.newaxis object which can be set to None. In turn it displays, each element in each row.

Reference to newaxis object: numpy.newaxis

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

Upvotes: 0

Related Questions