1
$\begingroup$
kmeans = KMeans(n_clusters=4)

model = kmeans.fit(europe_july)
pred = model.labels_
europe_july['cluster'] = pred

pca = PCA(n_components=2)
pca_model = pca.fit_transform(europe_july)
data_transform = pd.DataFrame(data = pca_model, columns = ['PCA1', 'PCA2'])
data_transform['Cluster'] = pred

plt.figure(figsize=(8,8))
g = sns.scatterplot(data=data_transform, x='PCA1', y='PCA2',\
                    palette=sns.color_palette()[:4], hue='Cluster')
title = plt.title('World countries clusters with PCA')

PCA

But when I run this code it does not seem to take into account this model.

europe_july['country'] = countries
europe_july['iso_alpha'] = iso_alpha


fig = px.choropleth(data_frame = europe_july,
                    locations= "iso_alpha",
                    scope= 'world',
                    title='2020-11-07 (World)',
                    color= "cluster",
                    hover_name= "country",
                    color_continuous_scale= 'earth',
                    )

fig.show()

Since this is the output that I get, as you can see there is clearly a cluster with only three countries, when there is no such cluster predicted by the model.

This is the output of the predictions for clusters and it matches the visualizations by PCA:

array([2, 3, 1, 0, 2, 0, 0, 3, 1, 3, 3, 3, 1, 3, 3, 1, 1, 3, 3, 1, 2, 0,
       1, 1, 0, 1, 2, 3, 0, 2, 3, 2, 2, 1, 2, 3, 2, 0, 2, 2, 3, 3, 1, 2,
       2, 1, 2, 3, 1, 3, 3, 3, 2, 3, 2, 0, 1, 1, 1, 1, 2, 2, 3, 2, 0, 0,
       2, 3, 3, 0, 2, 2, 3, 3, 2, 0, 3, 0, 2, 3, 1, 0, 2, 2, 1, 2, 1, 3,
       3, 3, 1, 1, 3, 1, 3, 0, 3, 3, 1, 3, 0, 2, 1, 2, 0, 3, 1, 2, 3, 3,
       2, 2, 2, 0, 3, 3, 3, 2, 2, 3, 1, 2, 3, 2, 3, 1, 1, 0, 1, 3, 0, 2,
       2, 1, 2, 1, 3, 0, 3, 0, 2, 2, 0, 3, 1, 1, 2, 3, 2, 1, 3, 1, 3, 3,
       3, 3, 3, 3, 2, 0, 1, 0, 0, 2, 3, 2, 1, 3, 2, 3, 0, 3, 3, 2, 1, 3,
       2, 3, 3, 2, 1, 2, 3, 3, 2, 3, 1, 2, 1, 2, 2, 1, 1, 3, 0, 2, 3, 3,
       3, 3, 3, 3, 2, 0, 2, 1, 0, 2, 2, 2, 1, 0], dtype=int32)

vis

Could someone please guide on why my visualisation is wrong?

$\endgroup$
4
  • $\begingroup$ Looking at the plot, there seems to be at least one other country with cluster '0' below Mexico (Nicaragua?). Have you looked at the input data itself and simply calculate the number of records (i.e. countries) for each cluster? $\endgroup$ – Oxbowerce Apr 6 at 13:43
  • $\begingroup$ Thanks, I will edit that. There are 3 in total on the plot. I have an array with all the predictions and there are more than 3, just like visualized with PCA. $\endgroup$ – vojtak Apr 6 at 13:46
  • $\begingroup$ With that continuous scale of colors it is difficult to catch all the elements on cluster 0 (darker brown) why don't you try to filter you dataframe so that you only keep those from cluster 0 and thus you verify if they all are marked in the map? Try changing the data type of cluster id to object so that the colors are discrete as per: plotly.com/python/choropleth-maps $\endgroup$ – Julio Jesus Apr 6 at 18:48
  • $\begingroup$ I tried making them discreet, but it still shows only 3 countries in one cluster and there is no such cluster in the model. :( $\endgroup$ – vojtak Apr 8 at 18:10
0
$\begingroup$

The visualisation is in fact correct, the only issue is that the Plotly map simply does not have those countries marked on its map at all.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.