Chapter 1, Environment
Why Python became the language of data
Before writing a line of NumPy, it's worth understanding why it exists and why it lives in Python. The answer is less about the language and more about the people who built the libraries around it.
Code for this chapter: ambiente/cap01_ecossistema_python.py
Python is small, and the ecosystem is large
Python itself doesn't know how to train a neural network, draw a graph, or read a spreadsheet. None of this is built-in. What Python has is an easy-to-read syntax and a huge ease of calling code written in other languages. This is what attracted those working with numerical computation: it was possible to write the heavy core in C or Fortran and use it through a simple interface in Python.
NumPy was born in the mid-2000s from the unification of two previous projects (Numeric and Numarray). The rest of the data stack was built on top of it:
| Library | What it offers | Why it depends on arrays |
|---|---|---|
| NumPy | The array ndarray and the math on it | It's the foundation |
| pandas | Tables with labels (DataFrame) | Each column is, underneath, an array |
| Matplotlib | Graphs | Receives arrays and draws |
| scikit-learn | Classic machine learning | Data and models are arrays |
| PyTorch, JAX, TensorFlow | Deep learning | The "tensor" is an array with GPU and derivatives |
None of these libraries are part of Python. Each one was written by people, published for free, and maintained by a community. You can check this in the interpreter itself:
import importlib.util
import sys
import numpy as np
print("numpy faz parte da biblioteca padrão?", "numpy" in sys.stdlib_module_names)
print("numpy está instalado aqui?", importlib.util.find_spec("numpy") is not None)
print(type(np.add).__name__, type(np.ndarray).__name__)
numpy faz parte da biblioteca padrão? False
numpy está instalado aqui? True
ufunc type
The np.add is an object of type ufunc, a compiled function that operates on integer arrays. The heavy lifting happens in this compiled code, and Python just orchestrates. This is the healthy way to think about the whole stack: Python is the glue, and the speed comes from the lower layers.
How much Python do I need to know beforehand?
Less than you think. Variables, lists, loops, functions, and
importare enough to get started, and the rest you learn by using. No one memorizes an entire library. Those who work with data consult the documentation all the time, and what matters is knowing what is possible, to know what to look for.
Exercise 1
Which of these modules come with Python?
Given the list ["math", "numpy", "json", "pandas"], return only those that are part of the standard library.