In this Numpy tutorial we want to learn about Working with Multi-Dimensional Arrays in Numpy, NumPy is one of the fundamental library for numerical computing in Python, it provides powerful data structure called multidimensional arrays or ndarrays. These ndarrays enable efficient storage and manipulation of data in multiple dimensions, and this makes NumPy a powerful tool for handling complex data structures, now let’s practically talk about this concept.
How to Create Multi-Dimensional Arrays with Numpy
NumPy ndarray allows us to create arrays with multiple dimensions effortlessly. Let’s look at some examples of creating multi-dimensional arrays:
1 2 3 4 5 6 7 8 9 10 |
import numpy as np # Create 2D array arr_2d = np.array([[1, 2, 3], [4, 5, 6]]) # Create 3D array arr_3d = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) print(arr_2d) print(arr_3d) |
This will be the result
z
Accessing and Slicing Numpy Multi-Dimensional Arrays
NumPy provides powerful indexing and slicing capabilities to access elements or sub-arrays in the multi-dimensional arrays. These are a few examples:
1 2 3 4 5 6 7 8 9 |
# Accessing individual elements element = arr_2d[0, 1] # Slicing along a single dimension row_slice = arr_2d[1, :] column_slice = arr_2d[:, 2] # Slicing along multiple dimensions sub_array = arr_3d[1, :, 1] |
Reshaping and Resizing Numpy Multi-Dimensional Arrays
NumPy allows us to reshape and resize multi-dimensional arrays easily. This capability is useful when we need to change the shape or size of an array to fit a specific data processing requirement. These are a few examples:
1 2 3 4 5 6 7 |
# Reshaping an array arr = np.array([1, 2, 3, 4, 5, 6]) reshaped_arr = arr.reshape((2, 3)) # Resizing an array arr = np.array([[1, 2], [3, 4]]) resized_arr = np.resize(arr, (3, 3)) |
Numpy Multi-Dimensional Array Operations
NumPy provides different mathematical and logical operations that work seamlessly on multi-dimensional arrays. These operations can be performed element-wise or across specific dimensions. These are a few examples:
1 2 3 4 5 6 7 8 9 |
# Element-wise operations arr = np.array([[1, 2], [3, 4]]) addition = arr + 1 multiplication = arr * 2 # Operations across dimensions arr = np.array([[1, 2], [3, 4]]) sum_across_columns = np.sum(arr, axis=0) product_across_rows = np.prod(arr, axis=1) |
Broadcasting with Numpy Multi-Dimensional Arrays
NumPy broadcasting feature allows you for efficient element-wise operations between arrays of different shapes and sizes. Broadcasting enables NumPy to handle operations easily, even when dimensions do not match exactly.
1 2 3 |
arr = np.array([[1, 2, 3], [4, 5, 6]]) scalar = 2 broadcasted_arr = arr * scalar |
Learn More on Python Numpy
- Python Numpy for Machine Learning
- How to Install Numpy
- Working with Linear Algebra in Numpy
- Numpy vs Pandas
- Advance Python Numpy Techniques
- Numpy Array Indexing and Slicing
- Numpy Mathematical Functions
- Numpy Random Number Generation in Python
Subscribe and Get Free Video Courses & Articles in your Email