Introduction to NumPy: The Foundation for Scientific Computing in Python
Introduction to NumPy: The Foundation for Scientific Computing in Python
NumPy is a fundamental Python library for scientific computing. It provides powerful support for multidimensional arrays and matrices, as well as a wide range of functions for working with them. NumPy is an indispensable tool for data analysts, machine learning specialists, and scientific researchers.
NumPy allows you to efficiently process large volumes of data and perform mathematical operations, replacing loops with compact and vectorized code. The library includes built-in functions for linear algebra and statistics.
Installing and Importing the NumPy Library
To start working with NumPy, you need to install it and import it into your project.
Installing NumPy
Installing the library is done using
Handling Missing Values (NaN)
NumPy uses np.nan to work with missing values.
arr = np.array([1, 2, np.nan, 4, 5])
print(np.isnan(arr)) # [False False True False False]
Special functions such as nanmean(), nanstd(), nansum() are used to handle NaN, which ignore missing values.
NumPy Performance vs. Pure Python
NumPy is written in C and runs significantly faster than pure Python, especially when performing operations on large arrays. This is due to efficient vectorization and optimization.
import time
import numpy as np
size = 100000
# Python list
lista = list(range(size))
listb = list(range(size))
starttime = time.time()
resultpython = [a + b for a, b in zip(lista, listb)]
pythontime = time.time() - starttime
# NumPy array
anp = np.arange(size)
bnp = np.arange(size)
starttime = time.time()
resultnumpy = anp + bnp
numpytime = time.time() - starttime
print(f"Python time: {pythontime:.4f} seconds")
print(f"NumPy time: {numpytime:.4f} seconds")
The performance difference can reach tens or hundreds of times on large datasets.
Real-World Use Cases of NumPy
NumPy is used in various fields:
- Science: processing experimental data
- Finance: time series analysis
- Machine Learning: data preparation and normalization, modeling
- Computer Graphics: working with images
- Education: visualization of mathematical functions and algorithms
Frequently Asked Questions (FAQ)
How is a NumPy array different from a Python list?
A NumPy array is a data structure that stores numbers in the form of tables (tensors), has a fixed data type, and supports vectorized operations. A Python list is more flexible but less efficient for numerical computations.
How to create a zero matrix of size 3x3?
matrix = np.zeros((3, 3))
How to transpose a matrix?
transposedmatrix = matrix.T
How to multiply matrices?
result = matrix1 @ matrix2
How to find the inverse of a matrix?
inversedmatrix = np.linalg.inv(matrix)
Main Categories of NumPy Functions
NumPy provides a wide range of functions for various tasks:
- Array Creation: NumPy offers many functions for creating and initializing arrays with different initial values (zeros, ones, random numbers, etc.).
- Mathematical Operations: The library supports many mathematical operations on arrays, including arithmetic operations, statistical functions, and linear algebra functions.
- Linear Algebra Functions: NumPy provides functions for performing linear algebra operations such as matrix multiplication, eigenvalue computation, etc.
- Trigonometric and Logarithmic Functions: The library offers functions