Skip to content

About

A Numpy Tutorial for Beginners

Topics

Resources

Stars

30 stars

Watchers

1 watching

Forks

Latest commit

 

History

40 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NumPy Tutorial for Beginners

A hands-on introduction to NumPy, the library that pretty much all numerical and data work in Python is built on. Each topic is a short Jupyter notebook with small examples you can run, change and break.

Everything has been updated for NumPy 2.x.

Contents

# Notebook What's in it
0 Data Types dtypes, type codes, conversion, overflow, promotion, strings
1 Create an Array array, arange, linspace, zeros/ones/full, eye, meshgrid
2 Inspect an Array shape, size, memory use, strides
3 Sampling Methods the Generator API, distributions, choice, shuffling, legacy API
4 Math Functions what axis means, arithmetic, rounding, statistics, comparisons
5 Slicing & Indexing slicing, boolean masks, fancy indexing, where, select
6 Sort an Array sort, argsort, lexsort, partition, searchsorted
7 Manipulate an Array reshape, transpose, flatten, adding/removing axes and elements
8 Combine & Split an Array concatenate, stack, block, split, array_split
9 Set Operations unique, intersection/union/difference, isin
10 Linear Algebra @, norms, solve, lstsq, eigenvalues, QR, SVD
11 Broadcasting the rules, practical uses, and common traps
12 Views and Copies when changing one array changes another
13 Vectorization & Ufuncs why loops are slow and how to get rid of them
14 Missing Values NaN, inf, nan* functions, filling, masked arrays
15 Saving & Loading Arrays .npy/.npz, memory mapping, CSV
16 Exercises 17 practice problems with solutions

If you'd rather read it all in one go, Complete Tutorial.ipynb has chapters 0 to 15 in a single notebook.

Suggested order

If you're completely new to NumPy, go through 0 to 5 in order, then read 11 (Broadcasting) and 12 (Views and Copies) before the rest. Those two explain most of the confusing behaviour you'll run into later. After that, pick whatever you need.

Running the notebooks

git clone https://github.com/eric496/numpy-tutorial.git
cd numpy-tutorial
pip install -r requirements.txt
jupyter lab

You can also open any notebook directly in Google Colab via File → Open notebook → GitHub and pasting the repo URL.

Most examples use a seeded random generator (np.random.default_rng(seed)), so you should see the same numbers as the saved outputs. The exception is the timing examples in notebook 13, which depend on your machine.

Upgrading from older NumPy?

The notebooks used to target NumPy 1.x. If you're following along with older code, these are the changes you're most likely to hit:

  • np.string_ / np.unicode_ are gone, use np.bytes_ / np.str_
  • np.in1d is gone, use np.isin
  • np.random.seed + np.random.rand still work, but np.random.default_rng() is the recommended way
  • Python scalars no longer upcast arrays (float32_array + 1.0 stays float32)

The NumPy 2.0 migration guide has the full list.

Contributing

Found a mistake or something that's unclear? Issues and pull requests are welcome.

If you edit a notebook, please re-run it top to bottom so the saved outputs match, and then regenerate the combined notebook:

python tools/build_complete.py

Further reading

License

MIT

About

A Numpy Tutorial for Beginners

Topics

Resources

Stars

30 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages