N-Dimensional Arrays in NumPy
NumPy supports powerful and efficient multi-dimensional arrays, often referred to as n-dimensional arrays or simply ndarrays. These arrays can have one, two, or many dimensions, making them ideal for numerical and scientific computing.
- 1D Arrays: Simple sequences, like a list of numbers.
- 2D Arrays: Matrices or grids, such as tables or spreadsheets.
- 3D+ Arrays: Multi-layered data like images, videos, or volumetric datasets.
With NumPy's ndarray structure, you can easily create and manipulate data structures of any dimensionality, using a consistent and intuitive syntax.
Quick Links
Creating 1D Arrays
A 1D array in NumPy is simply a linear sequence of elements (like a list). It’s the most basic form of a NumPy array and is created using the numpy.array() function.
To get started, import the NumPy library and create a one-dimensional array.
import numpy as np
# Create a 1D array
arr = np.array([10, 20, 30, 40, 50]) # converts a regular Python list into a NumPy array
# Print the array
print(arr)
# or
num = [60, 70, 80, 90, 100]
arr2 = np.array(num)
# Print the array
print(arr2)
import numpy as np
# Create a 1D array
arr = np.array([10, 20, 30, 40, 50]) # converts a regular Python list into a NumPy array
# Print the array
print(arr)
# or
num = [60, 70, 80, 90, 100]
arr2 = np.array(num)
# Print the array
print(arr2)
How It Works:
- import numpy as np: This imports the NumPy library with the common alias np.
- np.array([...]): This function converts a regular Python list into a NumPy array.
- arr: The resulting array is stored in the variable arr.
Output
[10 20 30 40 50]
[60 70 80 90 100]
[10 20 30 40 50]
[60 70 80 90 100]
Notice that the output uses space instead of commas. That’s a common format for NumPy arrays.
You can create NumPy arrays from different Python sequence types like lists and tuples. Just pass the sequence to the np.array() function, and NumPy will convert it into an array. This makes it easy to build arrays from existing data structures in your code.
For example, both np.array([1, 2, 3]) and np.array((1, 2, 3)) will create the same 1D array.
You can also check the type and shape of the array:
print(type(arr)) # <class 'numpy.ndarray'>
print(arr.shape) # (5,)
print(type(arr)) # <class 'numpy.ndarray'>
print(arr.shape) # (5,)
- type(arr): Confirms the variable is a NumPy array.
- arr.shape: Returns a tuple representing the array’s shape. For a 1D array, it's just the number of elements.
💡 Tip: You can also create arrays using functions like np.arange() or np.linspace(). We’ll explore those next!
Creating 2D Arrays
A 2D array (two-dimensional array) is like a table with rows and columns. It’s commonly used to represent matrices, grids, spreadsheets, or image data. In NumPy, a 2D array is simply an array of arrays.
Just like with 1D arrays, you can create a 2D array using the np.array() function by passing a list of lists (or tuples of tuples).
import numpy as np
# Create a 2D array (2 rows, 3 columns)
arr_2d = np.array([
[1, 2, 3],
[4, 5, 6]
])
# Print the array
print(arr_2d)
import numpy as np
# Create a 2D array (2 rows, 3 columns)
arr_2d = np.array([
[1, 2, 3],
[4, 5, 6]
])
# Print the array
print(arr_2d)
How It Works:
- Each inner list represents a row.
- All rows must have the same number of elements (columns).
- NumPy automatically treats this structure as a 2D array.
Output
[[1 2 3]
[4 5 6]]
[[1 2 3]
[4 5 6]]
The result is a 2-row, 3-column array. You can check its shape using arr_2d.shape:
print(arr_2d.shape) # (2, 3)
print(arr_2d.shape) # (2, 3)
This tells you the array has 2 rows and 3 columns. You can also create 2D arrays using other functions like np.zeros() or np.ones()—we’ll explore those soon!
N-Dimensional Arrays
NumPy can handle arrays with any number of dimensions — not just 1D or 2D. These are called n-dimensional arrays or ndarrays. They allow you to work with complex data structures like 3D matrices, tensors, or higher-dimensional datasets.
You create n-dimensional arrays the same way as 1D or 2D arrays, by passing nested lists (or tuples) to np.array(). The deeper the nesting, the higher the number of dimensions.
import numpy as np
# Create a 3D array (2 blocks, 2 rows, 3 columns)
arr_3d = np.array([
[
[1, 2, 3],
[4, 5, 6]
],
[
[7, 8, 9],
[10, 11, 12]
]
])
print(arr_3d)
print("Shape:", arr_3d.shape) # (2, 2, 3)
import numpy as np
# Create a 3D array (2 blocks, 2 rows, 3 columns)
arr_3d = np.array([
[
[1, 2, 3],
[4, 5, 6]
],
[
[7, 8, 9],
[10, 11, 12]
]
])
print(arr_3d)
print("Shape:", arr_3d.shape) # (2, 2, 3)
How It Works:
- The outermost list contains 2 blocks, representing the first dimension.
- Each block contains 2 lists (rows), representing the second dimension.
- Each row contains 3 elements (columns), representing the third dimension.
- The arr_3d.shape attribute shows the size of each dimension as a tuple: (2, 2, 3).
Output
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
Shape: (2, 2, 3)
[[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
Shape: (2, 2, 3)
You can also check how many dimensions an array has using the ndim attribute. For example, arr_3d.ndim will return 3 for a 3-dimensional array.
print(arr_3d.ndim) # output 3
print(arr_3d.ndim) # output 3
- arr_3d.ndim: Returns an integer representing the number of dimensions of the array.
💡 Tip: Understanding n-dimensional arrays is essential for fields like data science, machine learning, and image processing.
Frequently Asked Questions
What is a 1D array in NumPy?
What is a 1D array in NumPy?
A 1D array in NumPy is a simple linear sequence of elements, similar to a Python list. It is created using np.array([1, 2, 3]) and has a shape like (3,).
How do I create a 2D array in NumPy?
How do I create a 2D array in NumPy?
You can create a 2D array in NumPy by passing a list of lists to np.array(), such as np.array([[1, 2], [3, 4]]). This creates a matrix with rows and columns.
What is the shape of an n-dimensional array?
What is the shape of an n-dimensional array?
The shape of an ndarray is a tuple that represents the size of the array along each dimension. For example, a 3D array with shape (2, 2, 3) has 2 blocks, each with 2 rows and 3 columns.
How can I check the number of dimensions in a NumPy array?
How can I check the number of dimensions in a NumPy array?
Use the ndim attribute. For example, arr.ndim will return 1 for a 1D array, 2 for a 2D array, and so on.
Can NumPy arrays be used for image or tensor data?
Can NumPy arrays be used for image or tensor data?
Yes, NumPy arrays are commonly used for image data (as 3D arrays) and tensors (as n-dimensional arrays), making them essential in fields like machine learning and computer vision.
What's Next?
In the next section, we’ll explore NumPy array attributes — the useful details that describe an array’s shape, size, data type.