← Back to Chapters

NumPy Tutorial

? NumPy Tutorial

? Quick Overview

NumPy (Numerical Python) is a powerful Python library used for numerical and scientific computing. It provides efficient n-dimensional arrays, mathematical functions, and tools for working with large datasets, making it much faster and more convenient than plain Python lists for numeric operations.

? Key Concepts

  • ndarray: The core data structure in NumPy representing n-dimensional arrays.
  • Vectorization: Performing operations on entire arrays without explicit Python loops.
  • Element-wise operations: Arithmetic applied to each element of an array.
  • Aggregate functions: Functions like mean(), sum(), max(), min() that summarize data.
  • Performance: NumPy operations are implemented in optimized C code, making them very fast.

? Syntax and Basics

To use NumPy, you first install the library and then import it in your Python code. The common convention is to import NumPy as np. Arrays can be created from Python lists, and once you have an array, you can perform mathematical operations directly on it.

NumPy arrays:

  • Store elements of the same data type (e.g., all integers or all floats).
  • Support slicing, indexing, and reshaping similar to lists but more powerfully.
  • Enable broadcasting, where operations automatically expand across compatible shapes.

? Code Examples

? Installing NumPy

? Install NumPy using pip
# Install NumPy using pip
pip install numpy

? Creating Arrays

? Create a 1D NumPy array
import numpy as np

arr = np.array([1, 2, 3, 4])
print(arr)  # Output: [1 2 3 4]

? Array Operations

? Perform element-wise operations on arrays
import numpy as np

arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])

print(arr1 + arr2)  # Output: [5 7 9]
print(arr1 * 2)     # Output: [2 4 6]

? Useful Array Functions

? Use common NumPy aggregation functions
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
print(np.mean(arr))   # Output: 3.0
print(np.sum(arr))    # Output: 15
print(np.max(arr))    # Output: 5
print(np.min(arr))    # Output: 1

? Live Output / Explanation

? What the code is doing

  • Creating arrays: np.array([1, 2, 3, 4]) creates a 1D NumPy array.
  • Array addition: arr1 + arr2 adds corresponding elements: [1, 2, 3] + [4, 5, 6] → [5, 7, 9].
  • Scalar multiplication: arr1 * 2 multiplies every element by 2, giving [2, 4, 6].
  • Mean: np.mean(arr) computes the average value (here, 3.0).
  • Sum: np.sum(arr) adds all elements (here, 15).
  • Max and Min: np.max(arr) and np.min(arr) return the largest and smallest values.

? Use Cases / When to Use NumPy

  • When you need fast numeric calculations on large datasets.
  • For data preprocessing before using libraries like Pandas, Matplotlib, or scikit-learn.
  • For matrix operations in machine learning and deep learning.
  • Whenever plain Python lists feel slow or inconvenient for math-heavy tasks.

? Tips & Best Practices

  • Prefer NumPy arrays over Python lists for numerical computations to gain speed and functionality.
  • Use vectorized operations instead of Python for loops whenever possible.
  • Explore built-in functions like mean(), sum(), max(), and min() for quick summaries.
  • Keep array shapes in mind, especially when working with multi-dimensional (2D/3D) arrays.
  • Use np.array() with a specified dtype if you need strict control over data types.

? Try It Yourself

  • Create a 2D array (matrix) using np.array() and perform addition and multiplication on it.
  • Calculate mean, sum, max, and min for a 2D array.
  • Experiment with array slicing and indexing (e.g., first row, first column, sub-matrices).
  • Create two arrays of different shapes and see how NumPy broadcasting behaves in operations.