- Provides support for large, multidimensional arrays and matrices.
- Large collection of
mathematical functions
to operate onarrays
- NumPy is foundation of other libraries like
Pandas
,SciPy
andScikit Learn
DataFrames
are build on NumPy arrays and can leverage NumPy functions.- NumPy's core data structure is the
ndarray
(n dimensional array) - Arrays are
homogeneous
collection of elements of same data types. Array
: Fixed size containers of items that are more efficient than PythonLists
orTuples
for data processing.- Only store a single data type (Mixed data type is stored as
object
|string
) - They can be one dimensional or multi dimensional.
- Array elements can be modified, but the array size cannot change.
- Arithmetic, statistcal, mathematical, bitwise operations and linear algebra.
- Stacking, searching, sorting, counting, broadcasting and matrix operations.
Elementwise
arithmetic operations,scientific
andfinancial
calculations.N dimensional array
processing, fastvector
andmatrix
operations.Broadcasting
: Arithmetic operations between array of different shapes and size.NumPy
is used as a base library for Pandas, SciPy, Statsmodel, Matplotlib, Scikit Learn.- Essential for
OpenCV
computer vision applications ( Images pixels are converted into array ) - The datasets which are accepted by models for
fit
( training | learning ) are also in the form of array. - NumPy has a very rich set of number generator, attributes and methods
import numpy as np
marks = [50, 55, 90, 99, 100]
marks_array = np.array(marks)
Properties | Description |
---|---|
ndim |
Number of dimensions (axes) in an array. |
shape |
Size of an array for each dimension. |
size |
Total number of elements in the array. |
dtype |
Data type of elements in the array. |
![]() |
|||
0 Dimension | 1 Dimension | 2 Dimensions | 3 Dimensions |
Point | Line | Square | Cube |
Scalar | Vector ( Row or Column ) | Matrix ( Table or Data Frame ) | Tensor |
Rank 0 Tensor | Rank 1 Tensor | Rank 2 Tensor | Rank 3 Tensor |
Array | Dimensions |
---|---|
np.array([1, 2, 3]) |
1D Array |
np.array([(1, 2, 3), (4, 5, 6)]) |
2D Array |
np.arange(start, stop, step) |
Range Array |
Attribute | Description |
---|---|
ndarray.shape |
Tuple of array dimensions |
ndarray.size |
Number of elements present in the array |
ndarray.ndim |
Number of array dimensions ( 1, 2, 3 ) |
ndarray.dtype |
Data type of Numpy array |
ndarray.itemsize |
Number of bytes required by each array element |
ndarray.nbytes |
Total number of bytes occupied by array |
ndarray.T |
NumPy.Transpose ( Transpose of a matrix ) |
Method | Description |
---|---|
np.array() |
Creates an array |
array.arange() |
Creates an array in a range with a specified increment |
array.unique() |
Number of unique elements in an array |
array.repeat() |
Creates an array by repeating the number of elements |
array.argmax() |
Indices of maximum values along an axis |
array.argmin() |
Indices of minimum values along an axis |
array.reshape() |
Changes the shape of an array |
array.count_nonzero() |
Count of non-zero elements in an array |
array.hsplit() |
Split arrays horizontally ( Row wise ) |
array.vsplit() |
Split arrays vertically ( Column wise ) |
array.hstack() |
Stack arrays horizontally ( Row wise ) |
array.vstack() |
Stack arrays vertically ( Column wise ) |
array.flatten() |
Transform a multi-dimensional Numpy array to 1D Numpy array |
array.ravel() |
View a multi-dimensional Numpy array as 1D Numpy array |
array.transpose() |
Transposes an array |
array.absolute() |
Absolute values of elements in an array |
array.round() |
Round up the floats to a specified number of decimal points |
Operator | Description |
---|---|
np.linspace(0,2,9) |
Add evenly spaced values between interval to array of length. |
np.zeros((1,2)) |
Creates an array filled with zeros |
np.ones((1,2)) |
Creates an array filled with ones |
np.random.random((5,5)) |
Creates an array with random numbers. |
np.empty((2,2)) |
Creates an empty array. |
- Both
lists
andarrays
have similar syntax. - Elements in both are
ordered
,mutable
and acceptduplicates
- Both allows
indexing
,slicing
anditerating
# Python List
marks = [50, 45, 34]
# NumPy Array
marks = np.array([50, 45, 34])
Python List | NumPy Array |
---|---|
No support for vectorized operations | Support vectorized operations ( addition, multiplication ) |
Hold Heterogeneous / mixed data types | Hold Homogeneous data type |
For loops are not efficient | For loops are fast and efficient |
List is a default structure | Array needs import of NumPy library |
Lists are flexible | Once the dimensions are defined cannot be expanded |
Consumes more memory | Consumes 6x less memory |
Slow ( Data type of each element is checked ) | Faster ( All the elements are of same data type ) |
Python List ( Ordered, Subscriptable, Mutable, Heterogeneous, Duplicate, Indexing, Slicing, Iterable )
List = [1, 2, 3]
NumPy Array
import numpy as np
Array = np.array([1, 2, 3])
- NumPy array elements only need memory to store element values.
List
stores much more information as compared to NumPy array.List
storesobject type
,reference count
,object value
andsize
of value.- Mathematical operations performed by Arrays are faster than Python's list.
# Python List: Uses a for loop for just multiplying the elements with 5.
[i * 5 for i in list]
# NumPy Array: Simply multiplies each elements of an array with 5.
array * 5