Let's dive into an introductory tutorial for NumPy!
NumPy, which stands for Numerical Python, is a foundational package for numerical computations in Python. It provides support for arrays (including multidimensional arrays), as well as an assortment of mathematical functions to operate on these arrays. With NumPy, mathematical and logical operations on arrays can be performed efficiently and quickly.
Efficiency: NumPy operations are implemented in C, allowing for efficient looping and mathematical operations.
Multidimensional arrays: NumPy arrays are n-dimensional and homogenous, making them more versatile than Python lists.
Mathematical Functions: NumPy offers an extensive list of mathematical functions including statistical, algebraic, and trigonometric functions.
Interoperability: Many other data science packages, including Pandas and Scikit-learn, are built on top of NumPy.
Before you can use NumPy, you need to install it:
pip install numpy
Once installed, you can import it into your Python script:
import numpy as np
The alias np
is conventionally used by most programmers.
Arrays are the main data structure used in NumPy. They come in two flavors: vectors and matrices. Vectors are strictly 1-dimensional (1D) arrays and matrices are 2D (but you should note a matrix can still have only one row or one column).
# From a Python list arr = np.array([1, 2, 3, 4, 5]) print(arr) # Output: [1 2 3 4 5]
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(matrix)
NumPy provides functions to create special arrays:
np.zeros(3)
: Create an array of zeros with length 3.
np.ones((3,3))
: Create a 3x3 matrix of ones.
np.eye(3)
: Create a 3x3 identity matrix.
NumPy arrays are versatile and allow element-wise operations:
arr = np.array([1, 2, 3]) print(arr + 1) # Add 1 to every element print(arr * 2) # Multiply each element by 2
You can also perform operations between arrays:
arr1 = np.array([1, 2, 3]) arr2 = np.array([4, 5, 6]) print(arr1 + arr2) # Element-wise addition
NumPy provides various functions to perform operations on arrays:
Mathematical: np.sum()
, np.mean()
, np.max()
, np.sin()
, ...
Logical: np.any()
, np.all()
.
Shape & Reshaping: np.reshape()
, np.transpose()
, np.ravel()
.
This is just a glimpse of what NumPy offers. With its efficient arrays and vast assortment of mathematical functions, it's a staple in the data science toolkit. Once you're comfortable with the basics, you can explore more advanced topics like broadcasting, advanced indexing, and specialized functions to further harness the power of NumPy.
Numpy Tutorial
Creating NumPy Array
NumPy Array Manipulation
Matrix in NumPy
Operations on NumPy Array
Reshaping NumPy Array
Indexing NumPy Array
Arithmetic operations on NumPy Array
Linear Algebra in NumPy Array
NumPy and Random Data
Sorting and Searching in NumPy Array
Universal Functions
Working With Images
Projects and Applications with NumPy