![]() |
VOOZH | about |
NumPy is a homogeneous data structure (all elements are of the same type). It is significantly faster than Python's built-in lists because it uses optimized C language style storage where actual values are stored at contiguous locations (not object reference). It also supports vectorized computations. It supports vectorized operations (no need for loops).
To start using NumPy, import it as follows:
import numpy as np
NumPy array’s objects allow us to work with arrays in Python. The array object is called ndarray. NumPy arrays are created using the array() function.
[1 2 3] [[1 2] [3 4]] [[[1 2] [3 4]] [[5 6] [7 8]]]
NumPy arrays have attributes that provide information about the array:
shape: Returns the dimensions of the array.dtype: Returns the data type of the elements.ndim: Returns the number of dimensions.(2, 3) int64 2
NumPy supports element-wise and matrix operations, including addition, subtraction, multiplication, and division:
[5 7 9] [[19 22] [43 50]]
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 |