When I try
numpy.newaxis
the result gives me a 2-d plot frame with x-axis from 0 to 1. However, when I try using numpy.newaxis
to slice a vector,
vector[0:4,]
[ 0.04965172 0.04979645 0.04994022 0.05008303]
vector[:, np.newaxis][0:4,]
[[ 0.04965172]
[ 0.04979645]
[ 0.04994022]
[ 0.05008303]]
Is it the same thing except that it changes a row vector to a column vector?
Generally, what is the use of numpy.newaxis
, and in which circumstances should we use it?
newaxis
object in the selection tuple serves to expand the dimensions of the resulting selection by one unit-length dimension.It is not just conversion of row matrix to column matrix.
Consider the example below:
Now lets add new dimension to our data,
You can see that
newaxis
added the extra dimension here, x1 had dimension (3,3) and X1_new has dimension (3,1,3).How our new dimension enables us to different operations:
Adding x1_new and x2, we get:
Thus,
newaxis
is not just conversion of row to column matrix. It increases the dimension of matrix, thus enabling us to do more operations on it.You started with a one-dimensional list of numbers. Once you used
numpy.newaxis
, you turned it into a two-dimensional matrix, consisting of four rows of one column each.You could then use that matrix for matrix multiplication, or involve it in the construction of a larger 4 x n matrix.
Simply put, the
newaxis
is used to increase the dimension of the existing array by one more dimension, when used once. Thus,1D array will become 2D array
2D array will become 3D array
3D array will become 4D array
and so on.. Here is a visual illustration.
Scenario-1:
np.newaxis
might come in handy when you want to explicitly convert a 1D array to either a row vector or a column vector, as depicted in the above picture.Example:
Scenario-2: When we want to make use of numpy broadcasting as part of some operation, for instance while doing addition of some arrays.
Example:
Let's say you want to add the following two arrays:
If you try to add these just like that, NumPy will raise the following
ValueError
:In this situation, you can use
np.newaxis
to increase the dimension of one of the arrays so that NumPy can broadcast.Now, add:
Alternatively, you can also add new axis to the array
x2
:Now, add:
Note: Observe that we get the same result in both cases (but one being the transpose of the other).
Scenario-3: This is similar to scenario-1. But, you can use
np.newaxis
more than once to promote the array to higher dimensions. Such an operation is sometimes needed for higher order arrays (i.e. Tensors).Example:
More background on np.newaxis vs np.reshape
newaxis
is also called as a pseudo-index that allows the temporary addition of an axis into a multiarray.np.newaxis
uses the slicing operator to recreate the array whilenp.reshape
reshapes the array to the desired layout (assuming that the dimensions match; And this is must for areshape
to happen).Example
In the above example, we inserted a temporary axis between the first and second axes of
B
(to use broadcasting). A missing axis is filled-in here usingnp.newaxis
to make the broadcasting operation work.General Tip: You can also use
None
in place ofnp.newaxis
; These are in fact the same objects.P.S. Also see this great answer: newaxis vs reshape to add dimensions
What is
np.newaxis
?The
np.newaxis
is just an alias for the Python constantNone
, which means that wherever you usenp.newaxis
you could also useNone
:It's just more descriptive if you read code that uses
np.newaxis
instead ofNone
.How to use
np.newaxis
?The
np.newaxis
is generally used with slicing. It indicates that you want to add an additional dimension to the array. The position of thenp.newaxis
represents where I want to add dimensions.In the first example I use all elements from the first dimension and add a second dimension:
The second example adds a dimension as first dimension and then uses all elements from the first dimension of the original array as elements in the second dimension of the result array:
Similarly you can use multiple
np.newaxis
to add multiple dimensions:Are there alternatives to
np.newaxis
?There is another very similar functionality in NumPy:
np.expand_dims
, which can also be used to insert one dimension:But given that it just inserts
1
s in theshape
you could alsoreshape
the array to add these dimensions:Most of the times
np.newaxis
is the easiest way to add dimensions, but it's good to know the alternatives.When to use
np.newaxis
?In several contexts is adding dimensions useful:
If the data should have a specified number of dimensions. For example if you want to use
matplotlib.pyplot.imshow
to display a 1D array.If you want NumPy to broadcast arrays. By adding a dimension you could for example get the difference between all elements of one array:
a - a[:, np.newaxis]
. This works because NumPy operations broadcast starting with the last dimension 1.To add a necessary dimension so that NumPy can broadcast arrays. This works because each length-1 dimension is simply broadcast to the length of the corresponding1 dimension of the other array.
1 If you want to read more about the broadcasting rules the NumPy documentation on that subject is very good. It also includes an example with
np.newaxis
: