<p>As part of a project at work I have to calculate the centroid of a set of points in 3D space. Right now I'm doing it in a way that seems simple but naive -- by taking the average of each set of points, as in:</p>

<pre><code>centroid = average(x), average(y), average(z)
</code></pre>

<p>where <code>x</code>, <code>y</code> and <code>z</code> are arrays of floating-point numbers. I seem to recall that there is a way to get a more accurate centroid, but I haven't found a simple algorithm for doing so. Anyone have any ideas or suggestions? I'm using Python for this, but I can adapt examples from other languages.</p>

[{'answer_id': 77978, 'author': 'deemer', 'author_id': 11192, 'author_profile': 'https://Stackoverflow.com/users/11192', 'pm_score': 4, 'selected': False, 'text': '<p>Nope, that is the only formula for the centroid of a collection of points.  See Wikipedia: <a href="http://en.wikipedia.org/wiki/Centroid" rel="noreferrer">http://en.wikipedia.org/wiki/Centroid</a></p>\n'}, {'answer_id': 77985, 'author': 'Dima', 'author_id': 13313, 'author_profile': 'https://Stackoverflow.com/users/13313', 'pm_score': -1, 'selected': False, 'text': '<p>You got it.  What you are calculating is the centroid, or the mean vector.</p>\n'}, {'answer_id': 77997, 'author': 'Sarien', 'author_id': 1994377, 'author_profile': 'https://Stackoverflow.com/users/1994377', 'pm_score': 0, 'selected': False, 'text': '<p>A "more accurate centroid" I believe centroid is defined the way you calculated it hence there can be no "more accurate centroid".</p>\n'}, {'answer_id': 78046, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>you can use increase accuracy summation - Kahan summation - was that what you had in mind? </p>\n'}, {'answer_id': 78058, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Yes that is the correct formula.</p>\n\n<p>If you have a large number of points you can exploit the symmetry of the problem (be it cylindrical, spherical, mirror). Otherwise, you can borrow from statistics and average a random number of the points and just have a bit of error.</p>\n'}, {'answer_id': 85787, 'author': 'Gregg Lind', 'author_id': 15842, 'author_profile': 'https://Stackoverflow.com/users/15842', 'pm_score': 2, 'selected': False, 'text': "<p>Potentially more efficient: if you're calculating this multiple times, you can speed this up quite a bit by keeping two standing variables  </p>\n\n<pre><code>N  # number of points\nsums = dict(x=0,y=0,z=0)  # sums of the locations for each point\n</code></pre>\n\n<p>then changing N and sums whenever points are created or destroyed.  This changes things from O(N) to O(1) for calculations at the cost of more work every time a point is created, moves, or is destroyed.   </p>\n"}, {'answer_id': 88394, 'author': 'AlejoHausner', 'author_id': 14309, 'author_profile': 'https://Stackoverflow.com/users/14309', 'pm_score': 4, 'selected': False, 'text': '<p>You vaguely mention "a way to get a more accurate centroid".  Maybe you\'re talking about a centroid that isn\'t affected by outliers.  For example, the <i>average</i> household income in the USA is probably very high, because a small number of <i>very</i> rich people skew the average; they are the "outliers".  For that reason, statisticians use the <i>median</i> instead.  One way to obtain the median is to sort the values, then pick the value halfway down the list.\n<p>\nMaybe you\'re looking for something like this, but for 2D or 3D points.  The problem is, in 2D and higher, you can\'t sort.  There\'s no natural order.  Nevertheless, there are ways to get rid of outliers.\n<p>\nOne way is to find the <a href="http://en.wikipedia.org/wiki/Convex_hull" rel="noreferrer">convex hull</a> of the points.  The convex hull has all the points on the "outside" of the set of points.  If you do this, and throw out the points that are on the hull, you\'ll be throwing out the outliers, and the points that remain will give a more "representative" centroid.  You can even repeat this process several times, and the result is kind like peeling an onion.  In fact, it\'s called "convex hull peeling".\n<p></p>\n'}, {'answer_id': 37780869, 'author': 'Chris', 'author_id': 454063, 'author_profile': 'https://Stackoverflow.com/users/454063', 'pm_score': 5, 'selected': True, 'text': '<p>Contrary to the common refrain here, there are different ways to define (and calculate) a center of a point cloud. The first and most common solution has been suggested by you already and I will <strong>not</strong> argue that there is anything wrong with this:</p>\n\n<p><code>centroid = average(x), average(y), average(z)</code></p>\n\n<p>The "problem" here is that it will "distort" your center-point depending on the distribution of your points. If, for example, you assume that all your points are within a cubic box or some other geometric shape, but most of them happen to be placed in the upper half, your center-point will also shift in that direction.</p>\n\n<p>As an alternative you could use the mathematical middle (the mean of the extrema) in each dimension to avoid this:</p>\n\n<p><code>middle = middle(x), middle(y), middle(z)</code></p>\n\n<p>You can use this when you don\'t care much about the number of points, but more about the global bounding box, because that\'s all this is - the center of the bounding box around your points.</p>\n\n<p>Lastly, you could also use the <code>median</code> (the element in the middle) in each dimension:</p>\n\n<p><code>median = median(x), median(y), median(z)</code></p>\n\n<p>Now this will sort of do the opposite to the <code>middle</code> and actually help you ignore outliers in your point cloud and find a centerpoint <strong>based on</strong> the distribution of your points.</p>\n\n<p>A more and robust way to find a "good" centerpoint might be to ignore the top and bottom 10% in each dimension and then calculate the <code>average</code> or <code>median</code>. As you can see you can define the centerpoint in different ways. Below I am showing you examples of 2 2D point clouds with these suggestions in mind.</p>\n\n<p>The dark blue dot is the average (mean) centroid.\nThe median is shown in green.\nAnd the middle is shown in red.\nIn the second image you will see exactly what I was talking about earlier: The green dot is "closer" to the densest part of the point cloud, while the red dot is further way from it, taking into account the most extreme boundaries of the point cloud.</p>\n\n<p><a href="https://i.stack.imgur.com/8qSQA.png" rel="noreferrer"><img src="https://i.stack.imgur.com/8qSQA.png" alt="enter image description here"></a>\n<a href="https://i.stack.imgur.com/iZSSi.png" rel="noreferrer"><img src="https://i.stack.imgur.com/iZSSi.png" alt="enter image description here"></a></p>\n'}, {'answer_id': 66577393, 'author': 'Mello', 'author_id': 12392216, 'author_profile': 'https://Stackoverflow.com/users/12392216', 'pm_score': 0, 'selected': False, 'text': '<p>If your <strong>n-dimensional</strong> vector is in a list <strong>[[a0, a1, ..., an],[b0, b1, ..., bn],[c0, c1, ..., cn]]</strong>, just convert the list to array, and than calculate the centroid like this:</p>\n<pre><code>import numpy as np\n\nvectors = np.array(Listv)\ncentroid = np.mean(vectors, axis=0)\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/77936', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/676/']