Numpy
Numpy
NumPy, short for Numerical Python, is an essential library for scientific computing in Python. It
provides support for large multidimensional arrays and matrices, along with a rich set of high-level
mathematical functions to operate on these arrays efficiently. NumPy is widely used in data science,
machine learning, engineering, and scientific research due to its performance and ease of use.
2. Vectorization: NumPy’s operations are optimized to run faster and more efficiently by
avoiding the use of loops through vectorized operations.
4. Linear Algebra Functions: Comprehensive set of linear algebra tools including functions for
matrix operations, decompositions, and solving linear systems.
5. Random Sampling: Provides functionalities for generating random numbers and sampling
from various probability distributions.
6. Integration with Other Libraries: Seamlessly integrates with other scientific computing and
data analysis libraries like SciPy, Pandas, and Matplotlib.
Importing NumPy
python
Copy code
import numpy as np
Creating Arrays
1. From Lists
python
Copy code
arange
python
Copy code
zeros
python
Copy code
ones
python
Copy code
linspace
python
Copy code
Array Attributes
python
Copy code
arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.shape) # Output: (2, 3) print(arr.size) # Output: 6
print(arr.dtype) # Output: int64
Array Operations
1. Arithmetic Operations
python
Copy code
arr = np.array([10, 20, 30, 40]) print(arr + 5) # Output: [15 25 35 45] print(arr - 5) # Output: [ 5 15 25
35] print(arr * 2) # Output: [20 40 60 80] print(arr / 2) # Output: [ 5. 10. 15. 20.]
2. Element-wise Operations
python
Copy code
arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) print(arr1 + arr2) # Output: [5 7 9] print(arr1 * arr2)
# Output: [ 4 10 18]
3. Matrix Operations
python
Copy code
matrix1 = np.array([[1, 2], [3, 4]]) matrix2 = np.array([[5, 6], [7, 8]]) print(np.dot(matrix1, matrix2)) #
Output: # [[19 22] # [43 50]]
Slicing and Indexing
1. Basic Indexing
python
Copy code
arr = np.array([10, 20, 30, 40, 50]) print(arr[1]) # Output: 20 print(arr[-1]) # Output: 50
2. Slicing
python
Copy code
arr = np.array([10, 20, 30, 40, 50]) print(arr[1:4]) # Output: [20 30 40] print(arr[:3]) # Output: [10 20
30] print(arr[::2]) # Output: [10 30 50]
3. 2D Arrays
python
Copy code
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(arr[1, 2]) # Output: 6 print(arr[:2, 1:]) # Output: [[2 3]
# [5 6]]
Broadcasting
python
Copy code
arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr + 10) # Output: # [[11 12 13] # [14 15 16]]
Linear Algebra
1. Matrix Transpose
python
Copy code
2. Matrix Inversion
python
Copy code
python
Copy code
matrix = np.array([[1, 2], [3, 4]]) eigenvalues, eigenvectors = np.linalg.eig(matrix) print(eigenvalues)
print(eigenvectors)
Random Sampling
1. Random Numbers
python
Copy code
2. Normal Distribution
python
Copy code
3. Random Integers
python
Copy code
1. Pandas
python
Copy code
2. Matplotlib
python
Copy code
Conclusion
NumPy is an indispensable tool for anyone working with numerical data in Python. Its efficient
handling of large datasets, broad functionality for array operations, and seamless integration with
other scientific libraries make it a cornerstone of the Python scientific stack. Whether you're
performing simple arithmetic or complex linear algebra, NumPy provides the tools needed to
manipulate and analyze data effectively. Understanding and utilizing NumPy's features can
significantly enhance your ability to perform high-performance scientific computing and data
analysis.