![]() |
VOOZH | about |
NumPy(Numerical Python) is a library for Python numerical computing. It provides efficient multi-dimensional array objects and various mathematical functions for handling large datasets making it a critical tool for professionals in fields that require heavy computation.
NumPy has various features that make it popular over lists:
ndarray, a N-dimensional array object that supports homogeneous data types.To begin using NumPy, you need to install it first. This can be done using the following pip command:
pip install numpy
Once installed, import the library with the alias np
import numpy as np
1. Using np.array: Use np.array() when you want to convert Python lists into NumPy arrays.
[1 2 3] [[1 2] [3 4]] [[[1 2] [3 4]] [[5 6] [7 8]]]
2. Using Numpy Functions: NumPy provides quick utility functions for creating arrays filled with zeros, ones, or ranges:
[[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] [[1. 1.] [1. 1.]] [0 2 4 6 8]
NumPy arrays support indexing to access individual elements using their positions. Multi-dimensional arrays can be indexed using row and column indices.
30 50 4
Slicing follows the same indexing rules as lists, but extends them to multiple dimensions, allowing to select rows, columns, or sub-arrays efficiently.
[[4 5 6]] [2 5]
Advanced Indexing in NumPy provides more flexible ways to access and manipulate array elements.
[20 40 60] [40 50 60]
Element-wise operations in NumPy allow to perform mathematical operations on each element of an array individually, without the need for explicit loops. We can perform arithmetic operations like addition, subtraction, multiplication and division directly on NumPy arrays.
[5 7 9] [-3 -3 -3] [ 4 10 18] [0.25 0.4 0.5 ]
Unary operations in NumPy apply a single-operand transformation, such as negation, absolute value or trigonometric evaluation, across an entire array.
[3 1 0 1 3]
Binary operations apply element-wise to arrays and return a new array containing the results.. We can use all basic arithmetic operators like +, -, /, etc. Operators like +=, -=, *= modify the existing array in-place and = operator simply assigns a new reference to a variable, it does not modify the original array.
[5 7 9]
NumPy provides familiar mathematical functions such as sin, cos, exp, etc. These functions operate element-wise on arrays and return a new array containing the computed values.
[0.0000000e+00 1.0000000e+00 1.2246468e-16] [ 1. 2.71828183 7.3890561 20.08553692] [0. 1. 1.41421356 1.73205081] [0. 1.25331414 1.77245385]
The np.sort() function sorts NumPy arrays in ascending order and can also sort structured arrays based on specific fields.
Note: Strings with prefix b'' indicate byte strings (fixed-length string in NumPy). S10 means each string can store up to 10 bytes.
[(b'Aakash', 2009, 9. ) (b'Ajay', 2008, 8.7) (b'Hrithik', 2009, 8.5) (b'Pankaj', 2008, 7.9)] [(b'Pankaj', 2008, 7.9) (b'Ajay', 2008, 8.7) (b'Hrithik', 2009, 8.5) (b'Aakash', 2009, 9. )]