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
NumPy (Numerical Python) is one of the most fundamental packages for numerical computations in Python. It provides support for large multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays.
Before diving into its functionality, ensure you have NumPy installed:
pip install numpy
Then, in your Python script or notebook:
import numpy as np
From a Python list or tuple:
array_from_list = np.array([1, 2, 3, 4])
Using built-in functions:
zeros_array = np.zeros((2, 3)) ones_array = np.ones((3, 3)) identity_matrix = np.eye(3) range_array = np.arange(10)
arr = np.array([[1, 2, 3], [4, 5, 6]]) arr.shape # (2, 3) arr.size # 6 arr.ndim # 2 arr.dtype # dtype('int64')
Arrays can be used in arithmetic operations:
a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) c = a + b # array([5, 7, 9]) d = a * b # array([ 4, 10, 18])
You can also apply mathematical operations element-wise:
squared = np.square(a) # array([1, 4, 9])
NumPy provides powerful ways to work with array subsets:
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) # Get a single element arr[1, 2] # 6 # Get a row arr[1] # array([4, 5, 6]) # Slicing arr[0:2, 1:3]
Easily change the shape of arrays:
arr = np.array([1, 2, 3, 4, 5, 6]) reshaped = arr.reshape(2, 3) # Transpose transposed = reshaped.T
Allows NumPy to work with arrays of different shapes:
a = np.array([1, 2, 3]) b = 2 result = a * b # array([2, 4, 6])
NumPy provides many useful functions:
arr = np.array([1, 2, 3, 4, 5]) np.mean(arr) # 3.0 np.median(arr) # 3.0 np.sum(arr) # 15 np.std(arr) # 1.414
Matrix operations are straightforward:
a = np.array([[1, 2], [3, 4]]) b = np.array([[2, 0], [0, 2]]) dot_product = np.dot(a, b)
This tutorial only scratches the surface of what NumPy can do. Given its vast array of functionalities and the key role it plays in Python's scientific ecosystem (e.g., Pandas, Scikit-learn), having a solid understanding of NumPy's capabilities is crucial for anyone working in data science, machine learning, or related fields.