Skip to content

    Introduction

    NumPy Course

    From the first array to the neural network, in three levels, with integrative projects and an executable file per chapter.

    By Alexsander Valente

    Who is this material for

    NumPy is the foundation of almost everything done with data in Python: pandas, matplotlib, scikit-learn, PyTorch, and JAX speak the language of arrays. I organized these notes into six parts, from installation to a neural network written only with NumPy, and each idea appears first as intuition, then as code that you can execute.

    PartFor whomWhat you will be able to do by the end
    EnvironmentWho has never used NumPyInstall, import, and understand why the array is faster than the list
    BasicWho is starting with dataCreate, index, reshape, and summarize arrays, and think in vectors instead of loops
    IntermediateWho already manipulates arrays and wants to go furtherJoin, sort, handle missing data, use linear algebra, and read and write files
    AdvancedWho writes numerical code for productionUnderstand memory, einsum, performance, reproducible randomness, and numerical testing
    EcosystemWho will use NumPy with other librariesPass data between NumPy, pandas, matplotlib, and scikit-learn without losing control
    ProjectsWho wants to prove they have learnedDeliver three complete projects, with tests, one per level

    If you have never programmed in Python, take the Python course, at least up to chapter 25. Here I assume you know what a list, a function, and a import.

    How each chapter works

    Every chapter follows the same layout: a short explanation, a code snippet, and its actual output. Each block shows, at the top, the file and the lines of the repository where it is located.

    • The output is real. I executed each chapter and copied what Python printed. This applies to NumPy 2.x, the version the course assumes. In 1.x versions, some representations change, such as how scalars are printed.
    • The exercises have solutions. Try first. The solution is hidden and ends with assert, so you find out for yourself if you got it right.
    • What depends on your machine is marked. Execution times vary greatly between computers, so I show comparisons (which was faster) instead of absolute numbers.

    How to run the code

    The repository has a folder for each part and a file for each chapter, named capNN_assunto.py.

    Estrutura do repositório
    curso-numpy/
      README.md
      pyproject.toml
      docs/
        numpy.html
      ambiente/
      básico/
      intermediario/
      avancado/
      ecossistema/
      projetos/
        estudantes/
        imagens/
        rede_neural/
      exemplos/
    

    Each file runs independently, from the root of the repository. With uv, without installing anything beforehand, NumPy comes along:

    Terminal
    uv run básico/cap05_criando_arrays.py
    

    The chapters in the Ecosystem part use more libraries, which the group ecossistema installs all at once:

    Terminal
    uv sync --group ecossistema
    

    Which version does this course assume

    The course assumes Python 3.12 or higher and NumPy 2.x. The examples were executed with NumPy 2.4. Chapter 37 lists what changed from the 1.x series to the 2.x, because much material on the internet still uses old names.

    A rule I repeat in this course

    When you write a for about the elements of an array, stop and ask: is there a vectorized way? There almost always is, and it is shorter and much faster. A good part of what you will learn here are the tools to answer "yes, there is, and it is this."