Reputation: 23
I have applied Kmeans clustering for my data and trying to map the clusters with folium
The code for the map is:
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
I get below error:
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
The map displays without lines 18 and 20 but without separating cluster colors (since color values missing).
Thanks for any suggestions!
Upvotes: 0
Views: 1989
Reputation: 11
The error says you are using float values where integers or slices are expected. i suggest you have a look the you 'cluster Labels' column of your Toronto_merged dataframe and covert it to int. you may have to check for NaN values as your iteration structure does not provide for NaN, hence you may drop NaN VALUES from Toronto_merge dataframe (use df.isnull() to check for null values)
`Toronto_merged['Cluster Labels] =Toronto_merged['Cluster Labels].astype(int)`
Upvotes: 0