{"course":{"slug":"python-for-ai","title":"Python, from Zero, for AI","url":"https://addaly.com/learn/python-for-ai","level":"beginner","track":"build"},"lesson":{"slug":"shapes-and-broadcasting","title":"Shapes, axes and broadcasting: the wrong-shape bug that runs without complaint","url":"https://addaly.com/learn/python-for-ai/shapes-and-broadcasting","position":71,"of":89,"minutes":9,"answer":"Broadcasting stretches a size-1 dimension to match, so (3,) and (3,1) combine into (3,3) silently; axis=0 collapses rows to give one value per column.","keyIdea":"Broadcasting stretches a size-1 dimension to match, so (3,) and (3,1) combine into (3,3) silently; axis=0 collapses rows to give one value per column, and printing .shape before and after an operation is the check that catches the bug that raises nothing."},"body":{"format":"markdown","text":"## Shape is the first thing to check\n\n```python\nm = np.arange(12).reshape(3, 4)\nm.shape       # (3, 4): 3 rows, 4 columns\n```\n\nA shape is a tuple with one number per dimension. `(3, 4)` is three rows of four. `(4,)` is a flat vector of four. `(1, 4)` is a row vector (a matrix with one row) and `(4, 1)` is a column vector. These four shapes hold nearly the same numbers and behave differently in every operation, and a large share of NumPy bugs are one of them standing where another was expected.\n\n`reshape` reinterprets the same data:\n\n```python\nv = np.arange(6)\nv.reshape(2, 3)        # 2 rows of 3\nv.reshape(3, -1)       # -1 means \"work it out\": (3, 2)\nv.reshape(-1, 1)       # column vector, (6, 1)\nv[np.newaxis, :]       # row vector, (1, 6), without reshape\n```\n\n`-1` is a placeholder for whichever size makes the element count work; only one `-1` is allowed. `np.newaxis` (or `None`) in an index inserts a dimension of size 1, which is the usual way to prepare an array for broadcasting.\n\n## Axes\n\nReductions take an `axis` argument, and the rule is easy to state and easy to get backwards: **the axis you name is the one that disappears.**\n\n```python\nm = np.array([[1, 2, 3],\n              [4, 5, 6]])\nm.sum()          # 21             everything\nm.sum(axis=0)    # [5, 7, 9]      collapse rows → one value per column\nm.sum(axis=1)    # [6, 15]        collapse columns → one value per row\n```\n\n`axis=0` runs *down* the rows and leaves the columns; `axis=1` runs *across* the columns and leaves the rows. If you want the mean of each column of a data matrix (the usual thing) that is `axis=0`. Most people reverse this at least once; the fix is to check the output shape rather than the arithmetic. A `(2, 3)` matrix reduced on `axis=0` has shape `(3,)`; on `axis=1`, `(2,)`.\n\n`keepdims=True` keeps the collapsed axis at size 1, so `m.mean(axis=1, keepdims=True)` has shape `(2, 1)`, which is exactly what you want to subtract from `m` in the next section.\n\n## Broadcasting: the rule\n\nWhen two arrays of different shapes meet in an operation, NumPy compares their shapes from the **right**. Two dimensions are compatible if they are equal, or if one of them is 1. A missing dimension on the left counts as 1. Size-1 dimensions are stretched to match. If any pair is incompatible, it raises.\n\n```python\nm = np.ones((3, 4))\nm + 10                 # scalar: broadcast to (3, 4)\nm + np.ones(4)         # (4,) → (1, 4) → (3, 4): adds to each row\nm + np.ones((3, 1))    # (3, 1) → (3, 4): adds to each column\nm + np.ones(3)         # (3,) vs (3, 4): compare 3 with 4 → error\n```\n\nThe last one is the good case: it raises, with a message naming both shapes. The case that does not raise is the dangerous one.\n\n## The bug that runs\n\n```python\nscores = np.random.default_rng(0).random(1000)      # (1000,)\nweights = load_weights()                            # (1000, 1) — someone saved a column\nweighted = scores * weights\nweighted.shape                                      # (1000, 1000)\n```\n\n`(1000,)` against `(1000, 1)`: align from the right, `1000` against `1`: compatible, stretch the 1. Then the missing left dimension of `scores` counts as `1` against `1000`: compatible, stretch. Result: a million-element outer product, computed without a word of complaint, and the mean of it is a plausible-looking number. Code downstream that takes `.mean()` or `.sum()` will run. The wrong answer will be confidently produced.\n\nThe check is one line: `assert weighted.shape == scores.shape`. Cheaper still is the habit of printing shapes as you build a pipeline. Every experienced NumPy user does this; it is not a beginner's crutch.\n\nWhere does a `(1000, 1)` come from? Usually from a `reshape(-1, 1)` that scikit-learn asked for, a CSV read with one column, or a slice that kept a dimension. Flatten it with `.ravel()` or `.squeeze()` before combining it with a flat vector, or be deliberate about which shape is the standard in your code and convert at the boundary.\n\n## Centring a matrix: broadcasting used well\n\nThe same rule that bites also does real work:\n\n```python\nX = np.random.default_rng(0).random((500, 8))     # 500 rows, 8 features\nmu = X.mean(axis=0)                               # (8,): one mean per feature\nsd = X.std(axis=0)                                # (8,)\nZ = (X - mu) / sd                                 # (500, 8) - (8,) → broadcasts row by row\n```\n\nStandardising features is one line, because `(8,)` broadcasts across all 500 rows. To normalise each *row* to unit length (the operation under cosine similarity) reduce on `axis=1` and keep the dimension:\n\n```python\nnorms = np.linalg.norm(X, axis=1, keepdims=True)   # (500, 1)\nX_unit = X / norms                                 # (500, 8) / (500, 1) → row by row\n```\n\nWithout `keepdims`, `norms` is `(500,)`, aligned from the right against `8`, and it raises: the good failure. With `keepdims` it is `(500, 1)` and broadcasts down the rows correctly.\n\n## Reading the error\n\n```\nValueError: operands could not be broadcast together with shapes (500,) (500,8)\n```\n\nThat message is a diagnosis in itself: read the two shapes, align from the right, find the pair that is neither equal nor 1. Here `500` sits against `8`. The fix is almost always a `keepdims=True`, a `[:, None]`, or a `.ravel()`, and knowing which requires knowing which axis you meant.\n\n## Try this now\n\nBuild the `scores` and `weights` example, confirm the million-element result, and fix it two ways: `ravel()` on `weights`, and `[:, None]` on `scores` with a `.squeeze()` after. Then standardise a random `(100, 5)` matrix by column and check `Z.mean(axis=0)` is near zero and `Z.std(axis=0)` near one."},"check":{"question":"`scores` has shape `(1000,)` and `weights` has shape `(1000, 1)`. A developer computes `scores * weights` expecting a thousand weighted scores and gets an array of a million numbers. Why did NumPy not raise an error?","options":[{"text":"Both shapes broadcast: `(1000,)` becomes `(1, 1000)`, the size-1 dimensions stretch to `(1000, 1000)`, and that is a valid elementwise result.","correct":true},{"text":"NumPy treats a trailing dimension of 1 as a request to transpose, so the second operand was flipped before multiplying and the operation became a valid matrix product of a row against a column.","correct":false,"tempting":"No transpose happens; a trailing 1 is a broadcastable dimension, not a transposition flag, and `*` is never a matrix product."},{"text":"Multiplication of arrays of different lengths is undefined, so NumPy filled the result with the product of every pair as a fallback.","correct":false,"tempting":"There is no fallback; the result follows a fixed rule that this case happens to satisfy, which is the point."},{"text":"The arrays had the same number of elements, so NumPy silently reshaped the first to match the second and computed the outer product.","correct":false,"tempting":"Element count is irrelevant to broadcasting; two arrays of a thousand elements with incompatible shapes would raise."}]},"prev":{"slug":"numpy-arrays","title":"NumPy arrays: why one line beats a loop by a hundred times","url":"https://addaly.com/learn/python-for-ai/numpy-arrays"},"next":{"slug":"dtypes-and-precision","title":"dtypes: what float16 costs, why int8 wraps round, and how to compare floats","url":"https://addaly.com/learn/python-for-ai/dtypes-and-precision"},"authorship":"Founder and series editor: Dr Mustafa Mun. Written by AI models to a written standard. Source and assessment review continues. Academic review invited.","review":{"status":"Draft, open for academic review"},"cite":"Addaly (2026) 'Shapes, axes and broadcasting: the wrong-shape bug that runs without complaint', in Python, from Zero, for AI. Edited by M. Mun. Addaly. Available at: https://addaly.com/learn/python-for-ai/shapes-and-broadcasting (Accessed: 21 November 2026).","licence":{"name":"CC BY-NC-SA 4.0","url":"https://creativecommons.org/licenses/by-nc-sa/4.0/"},"reader":{"schema":1,"title":"Shapes, axes and broadcasting: the wrong-shape bug that runs without complaint","courseTitle":"Python, from Zero, for AI","module":{"index":8,"slug":"numbers-in-bulk","title":"Numbers in bulk: NumPy, pandas and the vector under every model"},"answer":null,"keyIdea":"Broadcasting stretches a size-1 dimension to match, so (3,) and (3,1) combine into (3,3) silently; axis=0 collapses rows to give one value per column, and printing .shape before and after an operation is the check that catches the bug that raises nothing.","hero":{"kind":"plate","image":null,"alt":null,"caption":null},"blocks":[{"t":"h2","id":"shape-is-the-first-thing-to-check","runs":[{"text":"Shape is the first thing to check"}]},{"t":"code","lang":"python","label":"Python","text":"m = np.arange(12).reshape(3, 4)\nm.shape       # (3, 4): 3 rows, 4 columns"},{"t":"p","runs":[{"text":"A shape is a tuple with one number per dimension. "},{"text":"(3, 4)","code":true},{"text":" is three rows of four. "},{"text":"(4,)","code":true},{"text":" is a flat vector of four. "},{"text":"(1, 4)","code":true},{"text":" is a row vector (a matrix with one row) and "},{"text":"(4, 1)","code":true},{"text":" is a column vector. These four shapes hold nearly the same numbers and behave differently in every operation, and a large share of NumPy bugs are one of them standing where another was expected."}]},{"t":"p","runs":[{"text":"reshape","code":true},{"text":" reinterprets the same data:"}]},{"t":"code","lang":"python","label":"Python","text":"v = np.arange(6)\nv.reshape(2, 3)        # 2 rows of 3\nv.reshape(3, -1)       # -1 means \"work it out\": (3, 2)\nv.reshape(-1, 1)       # column vector, (6, 1)\nv[np.newaxis, :]       # row vector, (1, 6), without reshape"},{"t":"p","runs":[{"text":"-1","code":true},{"text":" is a placeholder for whichever size makes the element count work; only one "},{"text":"-1","code":true},{"text":" is allowed. "},{"text":"np.newaxis","code":true},{"text":" (or "},{"text":"None","code":true},{"text":") in an index inserts a dimension of size 1, which is the usual way to prepare an array for broadcasting."}]},{"t":"h2","id":"axes","runs":[{"text":"Axes"}]},{"t":"p","runs":[{"text":"Reductions take an "},{"text":"axis","code":true},{"text":" argument, and the rule is easy to state and easy to get backwards: "},{"text":"the axis you name is the one that disappears.","b":true}]},{"t":"code","lang":"python","label":"Python","text":"m = np.array([[1, 2, 3],\n              [4, 5, 6]])\nm.sum()          # 21             everything\nm.sum(axis=0)    # [5, 7, 9]      collapse rows → one value per column\nm.sum(axis=1)    # [6, 15]        collapse columns → one value per row"},{"t":"p","runs":[{"text":"axis=0","code":true},{"text":" runs "},{"text":"down","i":true},{"text":" the rows and leaves the columns; "},{"text":"axis=1","code":true},{"text":" runs "},{"text":"across","i":true},{"text":" the columns and leaves the rows. If you want the mean of each column of a data matrix (the usual thing) that is "},{"text":"axis=0","code":true},{"text":". Most people reverse this at least once; the fix is to check the output shape rather than the arithmetic. A "},{"text":"(2, 3)","code":true},{"text":" matrix reduced on "},{"text":"axis=0","code":true},{"text":" has shape "},{"text":"(3,)","code":true},{"text":"; on "},{"text":"axis=1","code":true},{"text":", "},{"text":"(2,)","code":true},{"text":"."}]},{"t":"p","runs":[{"text":"keepdims=True","code":true},{"text":" keeps the collapsed axis at size 1, so "},{"text":"m.mean(axis=1, keepdims=True)","code":true},{"text":" has shape "},{"text":"(2, 1)","code":true},{"text":", which is exactly what you want to subtract from "},{"text":"m","code":true},{"text":" in the next section."}]},{"t":"h2","id":"broadcasting-the-rule","runs":[{"text":"Broadcasting: the rule"}]},{"t":"p","runs":[{"text":"When two arrays of different shapes meet in an operation, NumPy compares their shapes from the "},{"text":"right","b":true},{"text":". Two dimensions are compatible if they are equal, or if one of them is 1. A missing dimension on the left counts as 1. Size-1 dimensions are stretched to match. If any pair is incompatible, it raises."}]},{"t":"code","lang":"python","label":"Python","text":"m = np.ones((3, 4))\nm + 10                 # scalar: broadcast to (3, 4)\nm + np.ones(4)         # (4,) → (1, 4) → (3, 4): adds to each row\nm + np.ones((3, 1))    # (3, 1) → (3, 4): adds to each column\nm + np.ones(3)         # (3,) vs (3, 4): compare 3 with 4 → error"},{"t":"p","runs":[{"text":"The last one is the good case: it raises, with a message naming both shapes. The case that does not raise is the dangerous one."}]},{"t":"h2","id":"the-bug-that-runs","runs":[{"text":"The bug that runs"}]},{"t":"code","lang":"python","label":"Python","text":"scores = np.random.default_rng(0).random(1000)      # (1000,)\nweights = load_weights()                            # (1000, 1) — someone saved a column\nweighted = scores * weights\nweighted.shape                                      # (1000, 1000)"},{"t":"p","runs":[{"text":"(1000,)","code":true},{"text":" against "},{"text":"(1000, 1)","code":true},{"text":": align from the right, "},{"text":"1000","code":true},{"text":" against "},{"text":"1","code":true},{"text":": compatible, stretch the 1. Then the missing left dimension of "},{"text":"scores","code":true},{"text":" counts as "},{"text":"1","code":true},{"text":" against "},{"text":"1000","code":true},{"text":": compatible, stretch. Result: a million-element outer product, computed without a word of complaint, and the mean of it is a plausible-looking number. Code downstream that takes "},{"text":".mean()","code":true},{"text":" or "},{"text":".sum()","code":true},{"text":" will run. The wrong answer will be confidently produced."}]},{"t":"p","runs":[{"text":"The check is one line: "},{"text":"assert weighted.shape == scores.shape","code":true},{"text":". Cheaper still is the habit of printing shapes as you build a pipeline. Every experienced NumPy user does this; it is not a beginner’s crutch."}]},{"t":"p","runs":[{"text":"Where does a "},{"text":"(1000, 1)","code":true},{"text":" come from? Usually from a "},{"text":"reshape(-1, 1)","code":true},{"text":" that scikit-learn asked for, a CSV read with one column, or a slice that kept a dimension. Flatten it with "},{"text":".ravel()","code":true},{"text":" or "},{"text":".squeeze()","code":true},{"text":" before combining it with a flat vector, or be deliberate about which shape is the standard in your code and convert at the boundary."}]},{"t":"h2","id":"centring-a-matrix-broadcasting-used-well","runs":[{"text":"Centring a matrix: broadcasting used well"}]},{"t":"p","runs":[{"text":"The same rule that bites also does real work:"}]},{"t":"code","lang":"python","label":"Python","text":"X = np.random.default_rng(0).random((500, 8))     # 500 rows, 8 features\nmu = X.mean(axis=0)                               # (8,): one mean per feature\nsd = X.std(axis=0)                                # (8,)\nZ = (X - mu) / sd                                 # (500, 8) - (8,) → broadcasts row by row"},{"t":"p","runs":[{"text":"Standardising features is one line, because "},{"text":"(8,)","code":true},{"text":" broadcasts across all 500 rows. To normalise each "},{"text":"row","i":true},{"text":" to unit length (the operation under cosine similarity) reduce on "},{"text":"axis=1","code":true},{"text":" and keep the dimension:"}]},{"t":"code","lang":"python","label":"Python","text":"norms = np.linalg.norm(X, axis=1, keepdims=True)   # (500, 1)\nX_unit = X / norms                                 # (500, 8) / (500, 1) → row by row"},{"t":"p","runs":[{"text":"Without "},{"text":"keepdims","code":true},{"text":", "},{"text":"norms","code":true},{"text":" is "},{"text":"(500,)","code":true},{"text":", aligned from the right against "},{"text":"8","code":true},{"text":", and it raises: the good failure. With "},{"text":"keepdims","code":true},{"text":" it is "},{"text":"(500, 1)","code":true},{"text":" and broadcasts down the rows correctly."}]},{"t":"h2","id":"reading-the-error","runs":[{"text":"Reading the error"}]},{"t":"code","lang":"","label":"Text","text":"ValueError: operands could not be broadcast together with shapes (500,) (500,8)"},{"t":"p","runs":[{"text":"That message is a diagnosis in itself: read the two shapes, align from the right, find the pair that is neither equal nor 1. Here "},{"text":"500","code":true},{"text":" sits against "},{"text":"8","code":true},{"text":". The fix is almost always a "},{"text":"keepdims=True","code":true},{"text":", a "},{"text":"[:, None]","code":true},{"text":", or a "},{"text":".ravel()","code":true},{"text":", and knowing which requires knowing which axis you meant."}]},{"t":"h2","id":"try-this-now","runs":[{"text":"Try this now"}],"role":"try"},{"t":"p","runs":[{"text":"Build the "},{"text":"scores","code":true},{"text":" and "},{"text":"weights","code":true},{"text":" example, confirm the million-element result, and fix it two ways: "},{"text":"ravel()","code":true},{"text":" on "},{"text":"weights","code":true},{"text":", and "},{"text":"[:, None]","code":true},{"text":" on "},{"text":"scores","code":true},{"text":" with a "},{"text":".squeeze()","code":true},{"text":" after. Then standardise a random "},{"text":"(100, 5)","code":true},{"text":" matrix by column and check "},{"text":"Z.mean(axis=0)","code":true},{"text":" is near zero and "},{"text":"Z.std(axis=0)","code":true},{"text":" near one."}]}],"check":{"question":[{"text":"scores","code":true},{"text":" has shape "},{"text":"(1000,)","code":true},{"text":" and "},{"text":"weights","code":true},{"text":" has shape "},{"text":"(1000, 1)","code":true},{"text":". A developer computes "},{"text":"scores * weights","code":true},{"text":" expecting a thousand weighted scores and gets an array of a million numbers. Why did NumPy not raise an error?"}],"options":[{"text":[{"text":"Both shapes broadcast: "},{"text":"(1000,)","code":true},{"text":" becomes "},{"text":"(1, 1000)","code":true},{"text":", the size-1 dimensions stretch to "},{"text":"(1000, 1000)","code":true},{"text":", and that is a valid elementwise result."}],"correct":true,"tempting":null},{"text":[{"text":"NumPy treats a trailing dimension of 1 as a request to transpose, so the second operand was flipped before multiplying and the operation became a valid matrix product of a row against a column."}],"correct":false,"tempting":[{"text":"No transpose happens; a trailing 1 is a broadcastable dimension, not a transposition flag, and "},{"text":"*","code":true},{"text":" is never a matrix product."}]},{"text":[{"text":"Multiplication of arrays of different lengths is undefined, so NumPy filled the result with the product of every pair as a fallback."}],"correct":false,"tempting":[{"text":"There is no fallback; the result follows a fixed rule that this case happens to satisfy, which is the point."}]},{"text":[{"text":"The arrays had the same number of elements, so NumPy silently reshaped the first to match the second and computed the outer product."}],"correct":false,"tempting":[{"text":"Element count is irrelevant to broadcasting; two arrays of a thousand elements with incompatible shapes would raise."}]}],"order":[2,0,1,3],"why":null},"prev":{"slug":"numpy-arrays","title":"NumPy arrays: why one line beats a loop by a hundred times","minutes":9},"next":{"slug":"dtypes-and-precision","title":"dtypes: what float16 costs, why int8 wraps round, and how to compare floats","minutes":9}}}