Skip to main content
Documents
basicsArray CreationArray OperationsArray Computation & AnalysisLinear AlgebraRandom ProbabilityData Input/Output & Conversion

NumPy ndarrays

NumPy's `ndarray` is a powerful N-dimensional array object that forms the core of the NumPy library, enabling efficient storage and manipulation of large datasets. It is essential for numerical computations, providing functionality for operations like element-wise calculations, reshaping, and slicing.

Usage

The `ndarray` is used whenever you need to perform efficient mathematical operations on large datasets, particularly when handling multi-dimensional data for scientific computing. Its syntax involves creating arrays using functions like `numpy.array()`, with the optional `dtype` parameter to specify data types for optimized performance and storage.

import numpy as np

array = np.array([1, 2, 3, 4], dtype=np.int32)

Here, `np.array()` is used to create a one-dimensional `ndarray` from a list, specifying the data type with `dtype`.

Examples

1. Basic Array Creation

import numpy as np

array = np.array([1, 2, 3, 4, 5])
print(array)

This example demonstrates creating a simple one-dimensional array containing integers from a Python list. The `dtype`, `shape`, and `size` attributes of the array provide additional information, such as data type, dimensions, and total number of elements.

2. Multi-dimensional Array

import numpy as np

array_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(array_2d)

Here, a two-dimensional array (matrix) is created, showcasing how lists of lists can form multi-dimensional structures.

3. Array Operations

import numpy as np

array_a = np.array([1, 2, 3])
array_b = np.array([4, 5, 6])
sum_array = array_a + array_b
print(sum_array)

In this example, two arrays are added element-wise, demonstrating NumPy's ability to handle vectorized operations efficiently. Vectorized operations allow entire arrays to be processed in a single step, enhancing performance over traditional loops.

4. Array Reshaping

import numpy as np

array = np.array([1, 2, 3, 4, 5, 6])
reshaped_array = array.reshape(2, 3)
print(reshaped_array)

This example shows how to reshape a one-dimensional array into a two-dimensional array, a common operation in data manipulation.

Tips and Best Practices

  • Use vectorized operations. Leverage NumPy's built-in functions for optimal performance instead of looping through arrays. Vectorized operations perform computations on entire arrays, not element-by-element.
  • Be mindful of array shapes and data types. Ensure that shapes are compatible for operations like addition or multiplication, and use the `dtype` parameter to maintain consistency and improve efficiency.
  • Utilize slicing for subarrays. Use slicing to access subarrays for improved readability and performance. For example, `array[:2]` accesses the first two elements.
  • Explore broadcasting. Understand broadcasting rules for operations between arrays of different shapes to maximize NumPy's capabilities. For instance, adding a vector to each row of a matrix.
  • Consider memory efficiency. When dealing with large datasets, use `numpy.memmap` to handle data that doesn't fit into memory.
  • Compare NumPy arrays with Python lists. NumPy arrays offer significant performance benefits over lists for numerical operations and should be preferred in computation-heavy tasks.