【问题标题】:Folium map after clustering cannot display colors for clusters聚类后​​的叶图无法显示聚类的颜色
【发布时间】:2019-04-03 09:45:33
【问题描述】:

我已对我的数据应用 Kmeans 聚类并尝试使用 folium 映射聚类

地图的代码是:


    Toronto_map_clusters = folium.Map(location=[latitude, longitude], zoom_start=11)

    x = np.arange(kclusters)
    ys = [i + x + (i*x)**2 for i in range(kclusters)]
    colors_array = cm.rainbow(np.linspace(0, 1, len(ys)))
    rainbow = [colors.rgb2hex(i) for i in colors_array]

    markers_colors = []
    for lat, lon, poi, cluster in zip(Toronto_merged['Latitude'], Toronto_merged['Longitude'], Toronto_merged['Neighborhood'], Toronto_merged['Cluster Labels']):
        label = folium.Popup(str(poi) + ' Cluster ' + str(cluster), parse_html=True)
        folium.CircleMarker(
            [lat, lon],
            radius=5,
            popup=label,
            color=rainbow[cluster-1],
            fill=True,
            fill_color=rainbow[cluster-1],
            fill_opacity=0.7).add_to(Toronto_map_clusters)
    Toronto_map_clusters

我得到以下错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-81-5c051a345a95> in <module>()
     16         radius=5,
     17         popup=label,
***18         color=rainbow[cluster-1]***
     19         fill=True,
    *** 20         fill_color=rainbow[cluster-1]***

TypeError: list indices must be integers or slices, not float

地图显示没有第 18 行和第 20 行,但没有分隔簇颜色(因为缺少颜色值)。

感谢您的任何建议!

【问题讨论】:

    标签: python folium geomap


    【解决方案1】:

    该错误表明您在需要整数或切片的情况下使用浮点值。 我建议您查看 Toronto_merged 数据框的“集群标签”列并将其转换为 int。 您可能需要检查 NaN 值,因为您的迭代结构不提供 NaN,因此您可以从 Toronto_merge 数据帧中删除 NaN VALUES(使用 df.isnull() 检查空值)

    `Toronto_merged['Cluster Labels] =Toronto_merged['Cluster Labels].astype(int)`
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 1970-01-01
      • 2017-03-22
      • 2017-04-10
      相关资源
      最近更新 更多