Data as Tensors: The Shape Visualizer
The sklearn.fit() Trap
Here's how most beginner tutorials handle data:
This works. It runs. You get numbers back. And it teaches you almost nothing.
df.values silently hands you a 2D NumPy array. fit_transform silently
reshapes, centers, and scales it. If your CSV has 500 rows and 12 columns,
you get a (500, 12) array β but you never had to think about what that
shape means. You never had to ask: which axis is the batch dimension? What
does axis 1 represent? What happens if I reduce along axis 0 vs axis 1?
The abstraction hides the most important question in all of deep learning:
"What does this array's shape actually mean?"
Every neural network bug you will ever debug β wrong matrix multiply,
misaligned batch dimension, exploding gradient from a transposed weight
matrix β traces back to a shape misunderstanding. Pandas and sklearn make
it easy to never develop that intuition. We are going to develop it now,
from first principles.
The Failure Mode
Let's say you load a grayscale image as a flat list and try to reshape it
into a grid for display:
NumPy is being helpful here. This error is loud. But the silent failure
is worse: reshaping (500, 12) to (12, 500) doesn't raise an error β
the element count is conserved β but now every row operation you thought
was iterating over samples is iterating over features. Your model
trains on transposed data. Loss goes down (because neural networks are
disturbingly good at fitting garbage), and you ship a model that fails
in production.
The rule: **reshape only commutes with your intent if you understand what
each axis represents before the reshape.**
This lesson builds the muscle memory to always know your shapes.
The ScratchAI Architecture
We are building a TensorAnalyzer β a pure-function module in model.py
that accepts any NumPy array and returns a complete structural description:
rank, shape, strides, memory footprint, valid reshape candidates, axis
statistics, and indexed slices.
The data flow is deliberately simple because the lesson is about structure,
not computation:
No training loop. No weights. No gradients. Just arrays and their geometry.
This is intentional: before you can train anything, you must be able to
read the shape of data the way a doctor reads an X-ray β immediately,
fluently, without calculation.