When I first started coding, one of the concepts that gave me a hard time was understanding axes. Now, we all know in NumPy it is relatively straightforward — axis= 0 means rows, and axis =1 means columns. But the moment you dive into machine learning and start dealing with tensors, suddenly you’re juggling 3 axes, maybe even 4 or more. So what does that mean? Let’s use our imagination to break this down.
What are Axes?
I like to think of axes as directions in space. Each axis tells you how your data is organized. To make this easy (and fun), let’s use the analogy of LEGO blocks. Imagine each number in your data as a flat LEGO piece. By stacking or arranging these pieces, you can build shapes that represent different axes. Now, grab some LEGO blocks, a piece of paper, or just your imagination. Let’s build this together!
One number: a Single LEGO Block
Let’s start with a single LEGO block or a single number. There’s no direction—it’s just one standalone piece. Picture a single LEGO block on its own, and let that represent a number. That’s it! No stacking, no arrangement—just one piece. Simple, right?
Axis 0: A Line of LEGO Blocks
Now, let’s line up several LEGO blocks side by side to make a row. This is axis 0, aka a vector. We’ve introduced one direction horizontally.
The confusing part about NumPy is that when you write axis=0, you are actually specifying the first dimension. (It comes down to how computer geeks count starting from 0). Draw or imagine a line of LEGO blocks. Each block represents a number in an array and as you add more, the row moves in one direction—left to right.
Here is the numpy code:
You can see that we have an array of three numbers, which has an axis of 0 but a dimension of 1.
Axis 1: A Line of LEGO Blocks
How about axis = 1? Let’s take our horizontal lines and stack them on top of each other. Now we have a flat plane or a matrix. This is axis 1, giving us two directions: side to side and top to bottom.
Here is the numpy code:
You can see that we have 9 numbers, arranged in rows of three. Remember again, axis=1 means we have two dimensions to work with! Dimension 0 and dimension 1.
Axis 2: A Cube of LEGO Blocks
Here’s where things get difficult… I mean exciting. Let’s stack multiple 2D planes to form a cube. This is axis 2, introducing a third direction: depth. We are essentially stacking flat LEGO rectangles into a cube that has three directions: width, height, and depth.
Here is the numpy code:
You can see I’ve created 27 numbers in a cube. We have three dimensions (but two axes!).
Axis 3: A Cube of LEGO Blocks with Time
Okay, call me an overthinker, but what happens if we have axis 3 or more? Well, it’s definitely hard to visualize physically, but the concept is the same: every new axis adds another layer of organization. We are now in a hyper-cube. Each higher axis adds another level of stacking. You can think of the passage of time. Time is a copy of the same cube, but adds a dimension. When we think of video, we experience the fourth dimension because the pixels change.
Here is the numpy code to add a 4th dimension (and fourth axis).
So there you have it—axes explained through our childhood favorite, LEGO blocks! Whether you’re working with Pandas or NumPy, remember that axes are just directions in which your data grows. Start with a single block, then line them up, stack them, or even build cubes. The more you play around with these concepts, the more intuitive they’ll feel. Next time you’re stuck, just think: How would I stack my LEGO blocks? And if all else fails, grab a piece of paper, sketch it out, or even build it for real.