NumPy
NumPy gives Python fast, compact, multi-dimensional arrays — the foundation of scientific computing, data analysis and machine learning.
What you will learn
By the end of this tutorial you should be able to:
- explain why NumPy exists and when to reach for it instead of plain Python lists,
- create and inspect arrays (
shape,dtype,ndim,size), - index, slice, reshape and combine arrays,
- run arithmetic, statistical and boolean operations efficiently,
- handle missing data (
NaN), - save and load arrays to disk.
NumPy is the backbone of the Python data stack. Pandas, scikit-learn, PyTorch and TensorFlow all rely on it. Time spent here pays back in every data project that follows.
Why NumPy?
Python is easy to read and write, but plain lists are not designed for numerical work. A list of one million numbers can occupy ten times more memory than the equivalent NumPy array, and a loop over a list is much slower than the same operation in NumPy.
NumPy (short for Numerical Python) solves this with the ndarray — an N-dimensional array stored as a contiguous block of memory of a single data type, with operations implemented in C.
The two key benefits are:
- Less memory. Each element occupies exactly the bytes its type needs (e.g. 2 bytes for
int16), nothing else. - More speed. Operations are vectorised — they run as a single C loop instead of a Python
forloop.
Setup
Create a new project with uv and add NumPy:
Open a Python REPL inside the project:
By convention NumPy is imported as np:
Every example in this tutorial assumes that import is already in place.
Creating arrays
From a Python list
The simplest way is to pass a list to np.array():
=
# [1 2 3 4 5 6]Estàs llegint una vista prèvia.
Inicia sessió per llegir l'article complet. Qualsevol compte obre 4 articles gratuïts al mes; l'alumnat i el professorat llegeixen les pàgines del seu curs sense límit.
Inicia sessió