NumPy ndarrays

Carson West

Libraries like NumPy

NumPy ndarrays

NumPy’s core data structure is the ndarray (N-dimensional array). It’s a powerful tool for numerical computation because it provides efficient storage and manipulation of large arrays of homogeneous data.

Key features:

Example:

import numpy as np

# Creating a 2D array
arr = np.array(1, 2, 3, 4, 5, 6)

# Accessing elements
print(arr[0, 1)  # Output: 2

# Performing vectorized operations
print(arr * 2)  # Output: [ 2](./../-2/)  4  6, [ 8 10 12]]

# Array shape
print(arr.shape)  # Output: (2, 3)

# Array data type
print(arr.dtype)  # Output: int64 (or similar, depending on your system)

Related Notes: