A NumPy array is the data structure provided by the NumPy library for storing and working with numerical data. It supports efficient element-wise operations, mathematical computations and multi-dimensional data representation. NumPy arrays are widely used in data analysis, scientific computing and machine learning.
Creating NumPy Arrays
Before creating NumPy arrays, import the NumPy library using the standard alias np. Arrays are created using the np.array() function, which can be used to create one-dimensional, two-dimensional and multi-dimensional arrays.
import numpy as np
arr1 = np.array([10, 20, 30])
arr2 = np.array([[1, 2], [3, 4]])
arr3 = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
print(arr1)
print(arr2)
print(arr3)
Output
[10 20 30] [[1 2] [3 4]] [[[1 2] [3 4]] [[5 6] [7 8]]]
Explanation:
- np.array([10, 20, 30]) creates a 1D array containing a single sequence of elements.
- np.array([[1, 2], [3, 4]]) creates a 2D array with rows and columns.
- np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]) creates a 3D array consisting of multiple 2D arrays.
- NumPy automatically determines the number of dimensions based on the nested structure of the data provided.
Attributes
NumPy arrays provide several built-in attributes that help us understand their structure and data type. These attributes are commonly used while analyzing array dimensions, size, and element types.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Shape:", arr.shape)
print("Data Type:", arr.dtype)
print("Dimensions:", arr.ndim)
Output
Shape: (2, 3) Data Type: int64 Dimensions: 2
Explanation:
- arr.shape returns (2, 3), indicating the array has 2 rows and 3 columns.
- arr.dtype returns the data type of the array elements. In this case, all elements are integers (int64).
- arr.ndim returns the number of dimensions in the array. Since the array contains rows and columns, it is a 2-dimensional array.
Operations
NumPy supports a wide range of mathematical operations that can be performed directly on arrays. These operations are applied element-wise by default, making calculations concise and efficient.
import numpy as np
x = np.array([10, 20, 30])
y = np.array([1, 2, 3])
print("Addition:", x + y)
print("Subtraction:", x - y)
print("Multiplication:", x * y)
print("Division:", x / y)
Output
Addition: [11 22 33] Subtraction: [ 9 18 27] Multiplication: [10 40 90] Division: [10. 10. 10.]
Explanation:
- x + y adds corresponding elements from both arrays.
- x - y subtracts elements at the same positions.
- x * y multiplies corresponding elements.
- x / y divides corresponding elements and returns floating-point results.
- These operations are performed element-wise without using loops.
Matrix Multiplication
NumPy also supports matrix multiplication for multi-dimensional arrays. This operation combines rows from the first matrix with columns from the second matrix.
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
res = np.dot(a, b)
print(res)
Output
[[19 22] [43 50]]
Explanation:
- np.dot(a, b) performs matrix multiplication between the two matrices.
- Each value in the result is calculated using the dot product of a row from the first matrix and a column from the second matrix.
Dimensions in NumPy Arrays
NumPy arrays can have multiple dimensions, allowing users to store data in multilayered structures.
| Name | Example |
|---|---|
| 0D (zero-dimensional) | Scalar - A single element |
| 1D (one-dimensional) | Vector- A list of integers. |
| 2D (two-dimensional) | Matrix- A spreadsheet of data |
| 3D (three-dimensional) | Tensor- Storing a color image |
NumPy Arrays vs Lists
Both NumPy arrays and Python lists can store collections of values, but NumPy arrays are specifically designed for numerical computing and large-scale data processing.
import numpy as np
arr = np.array([1, 2, 3])
lst = [1, 2, 3]
print(arr + 10)
Output
[11 12 13]
Explanation:
- NumPy automatically applies the operation to every element in the array.
- Similar operations on a Python list require additional code such as loops or list comprehensions.
- This vectorized behavior is one of the main reasons NumPy is widely used for numerical computing.
To know more about it refer to: Python Lists VS Numpy Arrays