NumPy Arrays from the Ground Up
Think in arrays: shapes, axes, broadcasting and vectorised maths, with embeddings as the running example.
A taste of a lesson
I have embeddings with shape (1000, 384). How do I normalise each row?
Compute each row's length, then divide. norms = np.linalg.norm(E, axis=1, keepdims=True) gives shape (1000, 1): one length per row, kept as a column. Then E_norm = E / norms broadcasts that column across all 384 values in each row. Without keepdims you would get shape (1000,), and the division would fail because shapes are compared from the right. As a check, np.linalg.norm(E_norm, axis=1) should be all ones. What shape do you expect that check to return?
Written by the teacher as an example. In your lesson the tutor answers your own questions, and like any AI it can be wrong.
What you will be able to do
- Create arrays and explain their shape, dtype and axes
- Select data with slices, boolean masks and integer indexing
- Predict when broadcasting works and what shape it produces
- Replace Python loops with vectorised operations
- Compute cosine similarities between embedding vectors
Lesson plan
- 1 Arrays, shapes and dtypes Create arrays and read their shape, number of dimensions and dtype. Start
- 2 Indexing, slicing and masks Select rows, columns and values that meet a condition. Start
- 3 Axes and aggregations Use sum, mean, max and argmax along the right axis. Start
- 4 Broadcasting Combine arrays of different shapes and predict the result shape. Start
- 5 Vectorised thinking Rewrite loops as whole array operations and see the speed difference. Start
- 6 Embeddings and similarity Normalise embedding rows and rank items by cosine similarity to a query. Start
Try asking
About this tutor
For beginners who can write Python loops and now meet NumPy in every AI tutorial. You learn what an array is, how shape, dtype and axis work, how indexing and boolean masks select data, how broadcasting combines arrays of different shapes, and why vectorised code is faster and clearer than loops. The running example is a small matrix of embeddings: you normalise rows, compute similarities with a dot product and find the closest items. Along the way you learn the traps: views versus copies, integer overflow and NaN values that spread through results.
Reviews
4.5
2 ratingsSample
- Elif K.Sample
Clear on views versus copies, which had burned me before. Wanted a bit more on 3D arrays but the basics are very solid.
- Arjun S.Sample
Writing shapes out by hand for broadcasting felt childish for a minute and then fixed every bug I had. The embedding similarity exercise tied it all together.
About the teacher
Numerical Python and code quality for data and AI projects that have outgrown a single notebook
9 tutors 374 lessons taught Sample
I work on the part of AI projects nobody photographs: the arrays, dataframes, tests and packaging that let a prototype survive contact with real data. I came to this through scientific computing and later backend work, so I care about two things at once, getting the numbers right and keeping the code readable for the next person. My lessons are...
See Felix's profile and tutorsMore like this
Other tutors on the same or nearby topics.