{"slug": "mean-vs-mean", "title": "Mean vs mean", "summary": "Dr. Drang, writing on his Leancrew blog on October 9, 2026, reported that a mean-of-columns check in Ronald Kneusel's book Practical Deep Learning produced a nonzero result, which he traced to floating-point rounding in NumPy's standardization of a 15-sample, 5-feature array. The author had dropped Friedman and Mendhekar's The Little Learner about halfway through over its Scheme-and-math notation before switching to Kneusel's Python-based text via his library's O'Reilly access. The episode illustrates why standardized feature columns computed as (x - x.mean(axis=0)) / x.std(axis=0) do not return exactly zero means in 64-bit floating point.", "body_md": "# [Mean vs mean](https://leancrew.com/all-this/2026/10/mean-vs-mean/)\n\nOctober 9, 2026 at 5:55 PM by Dr. Drang\n\nI decided to start learning about machine learning a couple of weeks ago. Partly because it sounds interesting, partly because I think I have enough math to handle it, and partly because I felt it was important to have a deeper knowledge if I want to criticize AI.\n\nI started with a library copy of Friedman and Mendhekar’s [*The Little Learner*](https://lccn.loc.gov/2022045341) but decided to drop it after I was about halfway through. Although I was following along reasonably well, I kept struggling with the book’s notation—after 200 pages, I shouldn’t have to keep returning to the front of the book to see what things like  mean. If the book’s Scheme code were actually written in Scheme, I might have stuck with it, but its mixture of Scheme with math was too frustrating.\n\nYesterday I started Kneusel’s [*Practical Deep Learning*](https://www.oreilly.com/library/view/practical-deep-learning/9798341655119/). My library gives me free access to O’Reilly ebooks, so it’s easy to have the book open on my iPad while I work through example code on my MacBook. And yes, I certainly feel more comfortable working in Python. But I was surprised by a result pretty early on.\n\nIt had to do with calculating the mean values of the columns of a 2D array. The book is explaining a way to normalize (or standardize) a set of data whose features are on vastly different scales. Here’s the code (in an interactive Python session) that does it using the book’s example data:\n\n``` python\npython:\n>>> import numpy as np\n>>> x = np.array([\n...  [6998, 0.1361, 0.3408, 0.00007350, 78596048],\n...  [6580, 0.4908, 3.0150, 0.00004484, 38462706],\n...  [7563, 0.9349, 4.3465, 0.00001003,  6700340],\n...  [8355, 0.6529, 2.1271, 0.00002966, 51430391],\n...  [2393, 0.4605, 2.7561, 0.00003395, 27284192],\n...  [9498, 0.0244, 2.7887, 0.00008880, 78543394],\n...  [4030, 0.6467, 4.8231, 0.00000403, 19101443],\n...  [5275, 0.3560, 0.0705, 0.00000899, 96029352],\n...  [8094, 0.7979, 3.9897, 0.00006691,  7307156],\n...  [ 843, 0.7892, 0.9804, 0.00005798, 10179751],\n...  [1221, 0.9564, 2.3944, 0.00007815, 14241835],\n...  [5879, 0.0329, 2.0085, 0.00009564, 34243070],\n...  [ 923, 0.4159, 1.7821, 0.00002467, 52404615],\n...  [5882, 0.0002, 1.5362, 0.00005066, 18728752],\n...  [1796, 0.7247, 2.3190, 0.00001332, 96703562],\n... ])\n...\n>>> xs = (x - x.mean(axis=0)) / x.std(axis=0)\n>>> xs\narray([[ 0.69308862, -1.12598999, -1.53184967,  0.95258185,  1.18240196],\n       [ 0.54647372, -0.01203876,  0.50510857, -0.01928358, -0.1141859 ],\n       [ 0.89126426,  1.38267719,  1.51932212, -1.19969655, -1.14033263],\n       [ 1.16906091,  0.49704356, -0.17121154, -0.53403994,  0.3047611 ],\n       [-0.92213055, -0.10719726,  0.30790249, -0.38856532, -0.47533014],\n       [ 1.56997199, -1.47678885,  0.33273415,  1.47140742,  1.18070086],\n       [-0.34794732,  0.47757218,  1.88235192, -1.40315756, -0.73969021],\n       [ 0.0887406 , -0.43538419, -1.73773921, -1.23496312,  1.7456197 ],\n       [ 1.07751429,  0.95242255,  1.24754488,  0.72911384, -1.12072823],\n       [-1.46579825,  0.92509981, -1.04466154,  0.42629603, -1.0279233 ],\n       [-1.33321348,  1.4501989 ,  0.03239288,  1.11026413, -0.89668956],\n       [ 0.30059562, -1.45009422, -0.26155005,  1.70335297, -0.25050967],\n       [-1.43773798, -0.24726556, -0.43400063, -0.70325168,  0.33623535],\n       [ 0.30164788, -1.55279003, -0.62130451,  0.1780736 , -0.75173073],\n       [-1.1315303 ,  0.72253467, -0.02503987, -1.08813209,  1.7674014 ]])\n```\n\nThe example data consists of 15 samples, each of which has 5 features. The third command rescales each feature by subtracting its mean and dividing by its standard deviation.\n\nIn the book, Kneusel checks the mean of the fourth column of rescaled data to confirm that it’s basically zero. He gets , which is effectively zero for 64-bit floating point values.\n\nTo make sure I was doing things right, I ran this:\n\n```\npython:\n>>> xs.mean(axis=0)\narray([ 1.48029737e-17, -8.88178420e-17,  6.77698638e-17, -1.62832710e-16,\n        4.44089210e-17])\n```\n\nWhile the fourth mean, , is certainly as good a zero as the book’s answer, it isn’t the same. To double-check my work, I ran this:\n\n```\npython:\n>>> xs[:, 3].mean()\n-1.3322676295501878e-16\n```\n\nThis *is* the book’s value. What’s going on?\n\nYou may think this is perfectly normal. Floating point numbers are inherently inexact, and as long as I get a result that’s effectively zero, there’s nothing to be bothered about. But I was bothered.\n\nI’m using the same function, [`mean`](https://numpy.org/doc/stable/reference/generated/numpy.mean.html), from the same library, NumPy, on the same set of data, and I get different answers depending on how I invoke that function. Or maybe I do get the same answers. Here’s another command and its output:\n\n```\npython:\n>>> for i in range(xs.shape[1]):\n...     print(f'Column {i}: {xs.mean(axis=0)[i] == xs[:, i].mean()}')\n...\nColumn 0: True\nColumn 1: True\nColumn 2: True\nColumn 3: False\nColumn 4: False\n```\n\nFor the first three columns, it doesn’t matter which way I use `mean`; for the last two, it does.\n\nI should mention here that while the different results are obvious when the mean is close to zero, this behavior is present when operating on any 2D array. I’ve tried it out on several 2D arrays made from randomly generated floating point numbers.\n\nDuring these experiments with randomly generated arrays, I got the sense that the likelihood of getting the same mean both ways got smaller as the number of rows in the array got larger. I decided to check that out with this script:\n\n``` python\npython:\n 1:  #!/usr/bin/env python3\n 2:  \n 3:  import numpy as np\n 4:  \n 5:  rng = np.random.default_rng()\n 6:  \n 7:  N = 5000\n 8:  \n 9:  for n in (10, 100, 1000, 10_000, 100_000):\n10:    count = 0\n11:    for i in range(N):\n12:      x0 = rng.uniform(low=0, high=100, size=n)\n13:      x1 = rng.uniform(low=100, high=200, size=n)\n14:      x = np.column_stack((x0, x1))\n15:      if x.mean(axis=0)[0] == x[:, 0].mean():\n16:        count += 1\n17:      if x.mean(axis=0)[1] == x[:, 1].mean():\n18:        count += 1\n19:    print(f'n = {n:,d}')\n20:    print(f'  Same means: {count/(2*N):.2%}')\n```\n\nThe results were\n\n```\nn = 10\n  Same means: 73.97%\nn = 100\n  Same means: 28.47%\nn = 1,000\n  Same means:  7.47%\nn = 10,000\n  Same means:  2.50%\nn = 100,000\n  Same means:  1.03%\n```\n\nSo arrays with more rows *are* less likely to yield the same mean values when the `mean` function is used in these two different ways.\n\nMy working theory is that when I use `mean()` on a 1D array extracted from a 2D array, the computations are done differently than when I use `mean(axis=0)` on the 2D array. Mathematically the same, but computationally different. Maybe it’s as simple as the summation being done in a different order so the partial sums round off in slightly different ways. The more partial sums, the greater the likelihood of a different answer.\n\nUltimately, and are both zero, so this was mainly an exercise in satisfying my curiosity. I worry when computer results that ought to be exactly the same are only nearly the same. It usually means I’ve done something wrong, but apparently not in this case.\n\n**Update 9 Oct 2026 9:41 PM**\n\n[Nathan Grigg](https://nathangrigg.com/), who has set me straight too many times to count, [pointed out](https://mastodon.social/@nathangrigg/117413516387160372) that the [NumPy docs for `sum`](https://numpy.org/doc/stable/reference/generated/numpy.sum.html) explain how that function uses algorithms with different precision depending on\n\n1. whether the `axis` parameter is used or not; and\n2. which axis is chosen if the parameter is used.\n\nThere’s basically a speed/precision tradeoff at work. While it’s true that [the docs for `mean`](https://numpy.org/doc/stable/reference/generated/numpy.mean.html) don’t include this explanation, `sum` and `mean` are so alike that it must be the reason for the different answers.\n\nThanks, Nathan!", "url": "https://wpnews.pro/news/mean-vs-mean", "canonical_source": "https://leancrew.com/all-this/2026/10/mean-vs-mean/", "published_at": "2026-10-09 22:55:47+00:00", "updated_at": "2026-10-23 23:42:10.313965+00:00", "lang": "en", "topics": ["machine-learning", "ai-research"], "entities": ["Dr. Drang", "Leancrew", "Ronald Kneusel", "Practical Deep Learning", "The Little Learner", "Friedman", "Mendhekar", "NumPy"], "also_reported_by": [], "takeaway": "Standardizing columns with (x - x.mean(axis=0)) / x.std(axis=0) in NumPy leaves column means only approximately zero due to 64-bit floating-point rounding, so exact-zero checks on rescaled data will fail.", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/mean-vs-mean", "markdown": "https://wpnews.pro/news/mean-vs-mean.md", "text": "https://wpnews.pro/news/mean-vs-mean.txt", "jsonld": "https://wpnews.pro/news/mean-vs-mean.jsonld"}}