Introduction to 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.
Key Features of NumPy
1. N-dimensional array object (ndarray): Central to NumPy is the ndarray, a powerful N-
dimensional array object that facilitates efficient storage and manipulation of large datasets.
2. Vectorization: NumPy’s operations are optimized to run faster and more efficiently by
avoiding the use of loops through vectorized operations.
3. Broadcasting: Allows arithmetic operations on arrays of different shapes, expanding smaller
arrays automatically to match the shape of larger arrays.
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.
Basic Operations in NumPy
Importing NumPy
python
Copy code
import numpy as np
Creating Arrays
1. From Lists
python
Copy code
arr = np.array([1, 2, 3, 4, 5]) print(arr)
2. Using Built-in Functions
arange
python
Copy code
arr = np.arange(0, 10, 2) print(arr)
zeros
python
Copy code
arr = np.zeros((2, 3)) print(arr)
ones
python
Copy code
arr = np.ones((3, 3)) print(arr)
linspace
python
Copy code
arr = np.linspace(0, 1, 5) print(arr)
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
matrix = np.array([[1, 2], [3, 4]]) print(matrix.T) # Output: # [[1 3] # [2 4]]
2. Matrix Inversion
python
Copy code
matrix = np.array([[1, 2], [3, 4]]) inv_matrix = np.linalg.inv(matrix) print(inv_matrix)
3. Eigenvalues and Eigenvectors
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
rand_arr = np.random.rand(3, 3) # Uniform distribution print(rand_arr)
2. Normal Distribution
python
Copy code
normal_arr = np.random.randn(3, 3) # Standard normal distribution print(normal_arr)
3. Random Integers
python
Copy code
rand_int = np.random.randint(1, 10, size=(2, 3)) print(rand_int)
Integration with Other Libraries
1. Pandas
python
Copy code
import pandas as pd data = np.random.rand(5, 3) df = pd.DataFrame(data, columns=['A', 'B', 'C'])
print(df)
2. Matplotlib
python
Copy code
import matplotlib.pyplot as plt data = np.random.randn(1000) plt.hist(data, bins=30) plt.show()
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.