How to calculate centroid of x,y coordinates in python

梦想的初衷 提交于 2019-12-10 19:52:23

问题


I have got a lot of x,y coordinates which I have clustered based on the distance between them. Now I would like to calculate a centroid measure for each cluster of x,y coordinates. Is there a way to do this?

My coordinates are in the format:

    coordinates_cluster = [[x1,x2,x3,...],[y1,y2,y3,...]]

Each cluster has a minimum length of three points, and all points can have both negative and positive x and y values. I hope that someone can help me.

Best, Martin

(I am using python 2.7 with canopy 1.1.1 (32 bit) on a Windows 7 system.)


回答1:


I realized that it was not that hard, but here is the code for calculating centroids of x,y coordinates:

    >>> c = [[1,4,-5],[3,-2,9]] # of the form [[x1,x2,x3],[y1,y2,y3]]
    >>> centroide = (sum(c[0])/len(c[0]),sum(c[1])/len(c[1]))

    >>> centroide
    (0, 3)



回答2:


If you are interested in calculating centroid as defined in geometry or signal processing [1, 2] :

import numpy as np
# a line from 0,0 to 1,1
x = np.linspace(0, 1, 100)
y = np.linspace(0, 1, 100)
cx = np.dot(x, y) / np.sum(y)

0.67003367003367



来源:https://stackoverflow.com/questions/19684995/how-to-calculate-centroid-of-x-y-coordinates-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!