Skip to Content
ModulesNumpy TutorialMultidimensional Arrays

Working with Multidimensional Arrays

NumPy makes it easy to work with multidimensional arrays, including creating, manipulating, stacking, and splitting them. Multidimensional arrays are widely used in data processing, image analysis, and numerical computations.


Creating 2D and 3D Arrays

Creating 2D Arrays

A 2D array can be created using a list of lists.

import numpy as np # Create a 2D array array_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print("2D Array:\n", array_2d)

Output:

2D Array: [[1 2 3] [4 5 6] [7 8 9]]

Creating 3D Arrays

A 3D array can be created using a list of lists of lists.

# Create a 3D array array_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print("3D Array:\n", array_3d)

Output:

3D Array: [[[1 2] [3 4]] [[5 6] [7 8]]]

Manipulating Multidimensional Arrays

Reshaping Arrays

Reshape an array to change its dimensions without altering data.

# Reshape a 1D array into a 2D array array = np.arange(1, 10) reshaped = array.reshape(3, 3) print("Reshaped Array:\n", reshaped)

Output:

Reshaped Array: [[1 2 3] [4 5 6] [7 8 9]]

Transposing Arrays

Transpose a 2D array to swap rows and columns.

# Transpose a 2D array transposed = array_2d.T print("Transposed Array:\n", transposed)

Output:

Transposed Array: [[1 4 7] [2 5 8] [3 6 9]]

Stacking Arrays

Vertical Stacking

Combine arrays vertically using vstack().

# Vertical stacking array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) stacked = np.vstack((array1, array2)) print("Vertically Stacked Array:\n", stacked)

Output:

Vertically Stacked Array: [[1 2 3] [4 5 6]]

Horizontal Stacking

Combine arrays horizontally using hstack().

# Horizontal stacking stacked_h = np.hstack((array1, array2)) print("Horizontally Stacked Array:", stacked_h)

Output:

Horizontally Stacked Array: [1 2 3 4 5 6]

Splitting Arrays

Splitting Arrays into Smaller Arrays

Split arrays into smaller arrays using split().

# Split an array into three parts array = np.array([1, 2, 3, 4, 5, 6]) split_arrays = np.split(array, 3) print("Split Arrays:", split_arrays)

Output:

Split Arrays: [array([1, 2]), array([3, 4]), array([5, 6])]

Splitting 2D Arrays

# Split a 2D array along rows split_rows = np.vsplit(array_2d, 3) print("Row-wise Split:", split_rows) # Split a 2D array along columns split_cols = np.hsplit(array_2d, 3) print("Column-wise Split:", split_cols)

Output:

Row-wise Split: [array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]])] Column-wise Split: [array([[1], [4], [7]]), array([[2], [5], [8]]), array([[3], [6], [9]])]

Try It Yourself

Problem 1: Create and Manipulate a 2D Array

Create a 2D array with shape (4, 4). Reshape it into a (2, 8) array and then transpose it.

Show Code

import numpy as np # Create a 2D array array = np.arange(1, 17).reshape(4, 4) # Reshape and transpose reshaped = array.reshape(2, 8) transposed = reshaped.T print("Original Array:\n", array) print("Reshaped Array:\n", reshaped) print("Transposed Array:\n", transposed)

Problem 2: Stack and Split Arrays

Create two 1D arrays with values from 1 to 6 and stack them vertically. Then, split the resulting array into two smaller arrays.

Show Code

import numpy as np # Create arrays array1 = np.array([1, 2, 3]) array2 = np.array([4, 5, 6]) # Stack vertically stacked = np.vstack((array1, array2)) # Split the stacked array split_arrays = np.vsplit(stacked, 2) print("Stacked Array:\n", stacked) print("Split Arrays:", split_arrays)

Pyground

Play with Python!

Output:

Last updated on