- NumPy Tutorial
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy - Array Attributes
- NumPy - Array Creation Routines
- NumPy - Array from Existing Data
- Array From Numerical Ranges
- NumPy - Indexing & Slicing
- NumPy - Advanced Indexing
- NumPy - Broadcasting
- NumPy - Iterating Over Array
- NumPy - Array Manipulation
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Mathematical Functions
- NumPy - Arithmetic Operations
- NumPy - Statistical Functions
- Sort, Search & Counting Functions
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy - I/O with NumPy
- NumPy Useful Resources
- NumPy Compiler
- NumPy - Quick Guide
- NumPy - Useful Resources
- NumPy - Discussion
numpy.expand_dims
This function expands the array by inserting a new axis at the specified position. Two parameters are required by this function.
numpy.expand_dims(arr, axis)
Where,
Sr.No. | Parameter & Description |
---|---|
1 | arr Input array |
2 | axis Position where new axis to be inserted |
Example
import numpy as np x = np.array(([1,2],[3,4])) print 'Array x:' print x print '\n' y = np.expand_dims(x, axis = 0) print 'Array y:' print y print '\n' print 'The shape of X and Y array:' print x.shape, y.shape print '\n' # insert axis at position 1 y = np.expand_dims(x, axis = 1) print 'Array Y after inserting axis at position 1:' print y print '\n' print 'x.ndim and y.ndim:' print x.ndim,y.ndim print '\n' print 'x.shape and y.shape:' print x.shape, y.shape
The output of the above program would be as follows −
Array x: [[1 2] [3 4]] Array y: [[[1 2] [3 4]]] The shape of X and Y array: (2, 2) (1, 2, 2) Array Y after inserting axis at position 1: [[[1 2]] [[3 4]]] x.ndim and y.ndim: 2 3 x.shape and y.shape: (2, 2) (2, 1, 2)
numpy_array_manipulation.htm
Advertisements