Skip to content

Numpy

Table of Contents

Topics 1-10 Topics 11-20
1. Introduction to NumPy 11. Linear Algebra
2. Installation and Setup 12. Random Numbers
3. NumPy Arrays (ndarray) 13. Statistical Functions
4. Array Creation 14. Array Iteration
5. Array Attributes 15. Sorting and Searching
6. Array Indexing and Slicing 16. File I/O
7. Array Operations 17. Performance and Optimization
8. Mathematical Functions 18. Best Practices
9. Array Manipulation 19. Common Use Cases for Data Engineering
10. Broadcasting 20. Resources

1. Introduction to NumPy

What is NumPy?

  1. NumPy (Numerical Python) is the fundamental package for scientific computing in Python. It provides support for large, multi-dimensional arrays and matrices, along with a collection of mathematical functions to operate on these arrays efficiently.

Key Features

  1. N-dimensional array object (ndarray): Fast and versatile

  2. Broadcasting functions: Operations on arrays of different shapes

  3. Linear algebra operations: Matrix operations, decompositions

  4. Random number generation: Various distributions

  5. Fourier transforms: Signal processing

  6. Integration with C/C++/Fortran: Easy to extend

  7. Memory efficiency: Compact storage compared to Python lists

Why Use NumPy?

  1. Performance: 10-100x faster than pure Python loops

  2. Memory efficient: Arrays use less memory than lists

  3. Vectorization: Write clean, readable code without explicit loops

  4. Foundation: Used by Pandas, SciPy, scikit-learn, TensorFlow, etc.

  5. Broadcasting: Elegant handling of operations on different-sized arrays

  6. Rich functionality: Comprehensive mathematical and statistical functions

NumPy vs Python Lists

Code Screenshot

2. Installation and Setup

Installing NumPy

Code Screenshot

Importing NumPy

Code Screenshot

Setting Display Options

Code Screenshot

3. NumPy Arrays (ndarray)

What is an ndarray?

An ndarray (N-dimensional array) is NumPy's core data structure - a grid of values of the same type, indexed by a tuple of non-negative integers.

Creating Basic Arrays

Code Screenshot

Array Dimensions

Code Screenshot

4. Array Creation

Zeros, Ones, and Empty

Code Screenshot

Range and Linspace

Code Screenshot

Identity and Diagonal Matrices

Code Screenshot

Creating Arrays Like Others

Code Screenshot

Random Arrays

Code Screenshot

5. Array Attributes

Shape and Size

Code Screenshot

Data Types

Code Screenshot

6. Array Indexing and Slicing

Basic Indexing

Code Screenshot

Slicing

Code Screenshot

Boolean Indexing

Code Screenshot

Fancy Indexing

Code Screenshot

Modifying Array Values

Code Screenshot

7. Array Operations

Arithmetic Operations

Code Screenshot

Comparison Operations

Code Screenshot

Logical Operations

Code Screenshot

Universal Functions (ufuncs)

Code Screenshot

Aggregation Functions

Code Screenshot

8. Mathematical Functions

Trigonometric Functions

Code Screenshot

Exponential and Logarithmic

Code Screenshot

Rounding and Clipping

Code Screenshot

Complex Numbers

Code Screenshot

Miscellaneous Math Functions

Code Screenshot

9. Array Manipulation

Reshaping Arrays

Code Screenshot

Transposing

Code Screenshot

Concatenating Arrays

Code Screenshot

Splitting Arrays

Code Screenshot

Adding/Removing Elements

Code Screenshot

Repeating and Tiling

Code Screenshot

10. Broadcasting

What is Broadcasting?

Broadcasting allows NumPy to perform operations on arrays of different shapes. The smaller array is "broadcast" across the larger array.

Broadcasting Rules

If arrays have different numbers of dimensions, pad the smaller shape with ones on the left

Arrays are compatible if dimensions are equal or one of them is 1

After broadcasting, each array behaves as if it had shape equal to element-wise maximum

Simple Broadcasting Examples

Code Screenshot

Broadcasting Visualized

Code Screenshot

Practical Broadcasting Examples

Code Screenshot

newaxis for Broadcasting

Code Screenshot

11. Linear Algebra

Matrix Operations

Code Screenshot

Vector Operations

Code Screenshot

Matrix Decompositions

Code Screenshot

Matrix Properties

Code Screenshot

Solving Linear Systems

Code Screenshot

Least Squares

Code Screenshot

12. Random Numbers

Random Number Generation

Code Screenshot

Random Distributions

Code Screenshot

Random Sampling

Code Screenshot

Code Screenshot

13. Statistical Functions

Basic Statistics

Code Screenshot

Statistics Along Axes

Code Screenshot

Correlation and Covariance

Code Screenshot

Histogram

Code Screenshot

Advanced Statistics

Code Screenshot

14. Array Iteration

Basic Iteration

Code Screenshot

Flat Iterator

Code Screenshot

nditer - Advanced Iteration

Code Screenshot

ndenumerate - Iterate with Indices

Code Screenshot

15. Sorting and Searching

Sorting

Code Screenshot

Argsort - Sort Indices

Code Screenshot

Partition

Code Screenshot

Searching

Code Screenshot

Finding Unique and Counts

Code Screenshot

Maximum and Minimum

Code Screenshot

16. File I/O

Saving and Loading Binary Files

Code Screenshot

Text Files

Code Screenshot

Memory-Mapped Files

Code Screenshot

tofile and fromfile

Code Screenshot

17. Performance and Optimization

Vectorization

Code Screenshot

Memory Views and Copies

Code Screenshot

In-Place Operations

Code Screenshot

Data Type Optimization

Code Screenshot

Avoid Array Copies

Code Screenshot

Use Universal Functions (ufuncs)

Code Screenshot

Parallel Operations with numexpr

Code Screenshot

18. Best Practices

Array Creation Best Practices

Code Screenshot

Avoid Unnecessary Copies

Code Screenshot

Proper Reshaping

Code Screenshot

Broadcasting Best Practices

Code Screenshot

Memory Layout (C vs Fortran Order)

Code Screenshot

Error Handling

Code Screenshot

Documentation and Comments

Code Screenshot

19. Common Use Cases for Data Engineering

Data Preprocessing

Code Screenshot

Feature Engineering

Code Screenshot

Data Splitting

Code Screenshot

Distance Calculations

Code Screenshot

Batch Processing

Code Screenshot

Time Series Operations

Code Screenshot

Working with Missing Data

Code Screenshot

20. Resources

Category Resource Link
Official Documentation NumPy Documentation numpy.org/doc/
NumPy User Guide numpy.org/doc/stable/user/index.html
NumPy API Reference numpy.org/doc/stable/reference/index.html
Books Guide to NumPy by Travis E. Oliphant Free PDF (NumPy creator)
Python for Data Analysis by Wes McKinney Includes NumPy
NumPy Cookbook by Ivan Idris Practical recipes
Elegant SciPy by Juan Nunez-Iglesias et al. Scientific Python
Online Courses NumPy Tutorial (Official) numpy.org
DataCamp NumPy Courses www.datacamp.com/
Kaggle NumPy Course www.kaggle.com/learn/
Real Python NumPy Tutorials realpython.com/tutorials/numpy/
Practice Resources 100 NumPy Exercises github.com/rougier/numpy-100
Kaggle Datasets www.kaggle.com/datasets
Video Tutorials Corey Schafer NumPy Tutorial www.youtube.com/watch?v=GB9ByFAIAH4
freeCodeCamp NumPy Course www.youtube.com/watch?v=QUT1VHiLmmI
Keith Galli NumPy Tutorial www.youtube.com/watch?v=8Y0qQEh7dJg
Community NumPy GitHub github.com/numpy/numpy
Stack Overflow stackoverflow.com/questions/tagged/numpy
Related Libraries SciPy scipy.org/
Pandas pandas.pydata.org/
Matplotlib matplotlib.org/
scikit-learn scikit-learn.org/
TensorFlow/PyTorch Deep learning libraries
Cheat Sheets NumPy Cheat Sheet (DataCamp) www.datacamp.com
NumPy Cheat Sheet (Official) numpy.org
Advanced Topics NumPy for MATLAB Users numpy.org
NumPy C-API numpy.org
Writing NumPy Extensions numpy.org/doc/stable/user/c-info.html

Summary

This comprehensive NumPy guide covered:

Introduction - What is NumPy and why use it

Installation and Setup - Installing and configuring NumPy

NumPy Arrays - Understanding ndarray, the core data structure

Array Creation - Various methods to create arrays

Array Attributes - Shape, dtype, size, and other properties

Indexing and Slicing - Accessing and modifying array elements

Array Operations - Arithmetic, comparison, and logical operations

Mathematical Functions - Trigonometric, exponential, logarithmic

Array Manipulation - Reshaping, transposing, concatenating, splitting

Broadcasting - Operations on arrays of different shapes

Linear Algebra - Matrix operations, decompositions, solving systems

Random Numbers - Random generation and distributions

Statistical Functions - Mean, median, std, correlation, histogram

Array Iteration - Different ways to iterate over arrays

Sorting and Searching - Sorting, searching, finding unique values

File I/O - Saving and loading arrays from files

Performance and Optimization - Best practices for speed and memory

Best Practices - Code organization, memory management

Data Engineering Use Cases - Practical examples for data engineering

Resources - Books, courses, documentation, community