Numpy - ndarray
ndarray((short for N-dimensional array)) is a core object in NumPy. It is a homogeneous array which means it can hold elements of the same data type. It is a multi-dimensional data structure that enables fast and efficient manipulation of large dataset
Let's understand with a simple example:
import numpy as np
#1D array
arr1 = np.array([1, 2, 3, 4, 5])
#2D array
arr2 = np.array([[1, 2, 3], [4, 5, 6]])
#3D array
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr1)
print(arr2)
print(arr3)
Output
[1 2 3 4 5] [[1 2 3] [4 5 6]] [[[1 2] [3 4]] [[5 6] [7 8]]]
Table of Content
Attributes of ndarray
Understanding the attributes of an ndarray is essential to 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
Shape: (2, 3) Dimensions: 2 Size: 6 Data type: int64 Item size: 8
Array Indexing and Slicing
NumPy allows powerful indexing and slicing operations on ndarrays, similar to Python lists but with additional functionality.
- Basic Indexing: we can index a single element in an array using square brackets.
Example:
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[2])
- Slicing: You can extract sub-arrays using slicing syntax.
import numpy as np
arr = np.array([10, 20, 30, 40, 50])
print(arr[1:4])
Output
[20 30 40]
- Multi-dimensional Indexing: In multi-dimensional arrays, you can index and slice each dimension separately.
Example:
import numpy as np
arr_2d = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# 6 (element at row 1, column 2)
print(arr_2d[1, 2])
# Sub-matrix from rows 0-1, columns 1-2
print(arr_2d[0:2, 1:3])
Output
6 [[2 3] [5 6]]
Array Operations
These operations allow you to perform element-wise arithmetic or other operations on entire arrays without the need for explicit loops.
- Element-wise Operations:
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
[5 7 9] [ 4 10 18] [-3 -3 -3] [0.25 0.4 0.5 ]
- Matrix Operations (Dot product):
import numpy as np
matrix1 = np.array([[1, 2], [3, 4]])
matrix2 = np.array([[5, 6], [7, 8]])
print(np.dot(matrix1, matrix2))
Output
[[19 22] [43 50]]
Broadcasting
Broadcasting is a powerful feature in NumPy that allows you to perform operations on arrays of different shapes.
Example:
import numpy as np
arr = np.array([[1, 2], [3, 4]])
# Adding 10 to each element of the array
print(arr + 10)
Output
[[11 12] [13 14]]
Reshaping and Flattening
NumPy provides functions to reshape or flatten arrays, which is useful when working with machine learning or deep learning algorithms.
- Reshaping:
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
reshaped_arr = arr.reshape(2, 3) # 2 rows, 3 columns
print(reshaped_arr)
Output
[[1 2 3] [4 5 6]]
- 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]