形成集群时get函数的索引错误

时间:2018-06-06 10:19:36

标签: python-3.x pandas geospatial centroid index-error

我是Python的新手,正在使用Jupyter Notebook

我正在尝试使用DBSCAN方法实现空间聚类。 我能够形成集群,但在尝试找到最接近其质心的每个集群时,我尝试运行以下代码

def get_centermost_point(cluster):
centroid = (MultiPoint(cluster).centroid.x, MultiPoint(cluster).centroid.y)
centermost_point = min(cluster, key=lambda point: great_circle(point, centroid).m)
return tuple(centermost_point)
centermost_points = clusters.map(get_centermost_point)

运行代码后,我收到以下错误,

IndexError                                Traceback (most recent call last)
<ipython-input-43-54be540e9384> in <module>()

 4     centermost_point = min(cluster, key=lambda point: great_circle(point, centroid).m)
  5     return tuple(centermost_point)
----> 6 centermost_points = clusters.map(get_centermost_point)

~\Anaconda3\lib\site-packages\pandas\core\series.py in map(self, arg, na_action)
   2994         """
   2995         new_values = super(Series, self)._map_values(
-> 2996             arg, na_action=na_action)
   2997         return self._constructor(new_values,
   2998                                  index=self.index).__finalize__(self)

~\Anaconda3\lib\site-packages\pandas\core\base.py in _map_values(self, mapper, na_action)
   1002 
   1003         # mapper is a function
-> 1004         new_values = map_f(values, mapper)
   1005 
   1006         return new_values

pandas/_libs/src\inference.pyx in pandas._libs.lib.map_infer()

<ipython-input-43-54be540e9384> in get_centermost_point(cluster)
      1 # given a cluster of points, return the point nearest to the cluster's centroid
      2 def get_centermost_point(cluster):
----> 3     centroid = (MultiPoint(cluster).centroid.x, MultiPoint(cluster).centroid.y)
      4     centermost_point = min(cluster, key=lambda point: great_circle(point, centroid).m)
      5     return tuple(centermost_point)

~\Anaconda3\lib\site-packages\shapely\geometry\point.py in x(self)
     54     def x(self):
     55         """Return x coordinate."""
---> 56         return self.coords[0][0]
     57 
     58     @property

IndexError: list index out of range

有人可以建议一种纠正方法吗?

提前致谢:)

0 个答案:

没有答案