Numpy - ndarray
ndarray
is a short form for N-dimensional array which is a important component of NumPy. It’s allows us to store and manipulate large amounts of data efficiently. All elements in an ndarray
must be of same type making it a homogeneous array. This structure supports multiple dimensions which makes it ideal for handling complex datasets like those used in scientific computing or data analysis. With this
we can perform fast and memory efficient operations on data.
Let's understand this with a simple example:
import numpy as np
arr1 = np.array([1, 2, 3, 4, 5])
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr1)
print(arr2)
print(arr3)
Output:

Attributes of ndarray
Understanding the attributes of an ndarray is important while working with NumPy effectively. Here are the key attributes:
- ndarray.shape: Returns a tuple representing the shape (dimensions) of the array.
- ndarray.ndim: Returns the number of dimensions (axes) of the array.
- ndarray.size: Returns the total number of elements in the array.
- ndarray.dtype: Provides the data type of the array elements.
- ndarray.itemsize: Returns the size in bytes of each element
Example:
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Shape:", arr.shape)
print("Dimensions:", arr.ndim)
print("Size:", arr.size)
print("Data type:", arr.dtype)
print("Item size:", arr.itemsize)
Output:

Array Indexing and Slicing
NumPy allows indexing and slicing operations on ndarrays which offers more flexibility compared to standard Python lists. Here's a overview:
1. Basic Indexing
We can access individual elements in an array using square brackets just like Python lists. The indexing starts at 0.
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[2])
Output:
30
2. Slicing
It allows us to extract sub-arrays using a range of indices. The syntax is [start:stop] where start is inclusive and stop is exclusive.
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4])
Output:
[20 30 40]
3. Multi-dimensional Indexing
We can index and slice each dimension separately in multi-dimensional arrays. This allows us to access specific rows, columns or deeper dimensions of the array.
import numpy as np
arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr_2d[1, 2])
print(arr_2d[0:2, 1:3])
Output:

Array Operations
NumPy allows us to perform operations on entire arrays which enables efficient computation without the need for explicit loops. These operations include:
1. Element-wise Operations
These are straightforward and allow us to perform arithmetic operations on each element of the array directly.
import numpy as np
arr = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print(arr + arr2)
print(arr * arr2)
print(arr - arr2)
print(arr / arr2)
Output:

2. Matrix Operations (Dot product)
It allow us to multiply two arrays or matrices and get a single value or another matrix.
import numpy as np
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
print(np.dot(matrix1, matrix2))
Output:

3. Broadcasting
This feature enables us to perform operations on arrays of different shapes. NumPy automatically adjusts the smaller array to match the shape of the larger one for the operation.
import numpy as np
arr = np.array([[1, 2], [3, 4]])
print(arr + 10)
Output:

4. Reshaping and Flattening
NumPy provides functions to reshape or flatten arrays which is useful when working with machine learning or deep learning algorithms.
- Reshaping: Change the shape of an array while keeping the data same.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped_arr = arr.reshape(2, 3)
print(reshaped_arr)
Output:

- Flattening: Convert multi-dimensional arrays into one-dimensional arrays.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
flattened_arr = arr.flatten()
print(flattened_arr)
Output:
[1 2 3 4 5 6]
NumPy provides the tools needed to efficiently handle and manipulate large datasets making it an important library for numerical tasks in Python.