Shape Manipulation & Broadcasting
Reshaping, stacking, and the rule that lets mismatched shapes still add.
reshape relabels the same flat data into a new shape; it does not
reorder data the way a transpose does. If you find yourself needing rows to
become columns, that's .T / np.transpose, not reshape.
Broadcasting is how NumPy applies an operation to two arrays of different shapes without you writing a loop. Compare shapes from the right; two dimensions are compatible if they're equal, or one of them is 1 (it gets "stretched" to match). Play with the examples below.
| Function | Effect | Example |
|---|---|---|
| join along an existing axis |
|
| stack as rows |
|
| stack side by side |
|
| join along a brand-new axis |
|
| drop all size-1 dimensions | shape |
| insert a new size-1 dimension | shape |
Now practice it
Exercises that use what this lesson just covered.