3

I have clustered 3 features Feature1, Feature2 and Feature3 and came up with 2 clusters. I am trying to visualize a 3D cluster using matplotlib.

In the below table, there are three features upon which the clustering is executed. Number of clusters is 2.

    Feature1        Feature2    Feature3    ClusterIndex
  0 1.349656e-09    1.000000    1.090542e-09    0
  1 1.029752e-07    1.000000    6.040669e-08    0
  2 2.311729e-07    1.000000    1.568289e-11    0
  3 1.455860e-08    6.05e-08    1.000000        1
  4 3.095807e-07    2.07e-07    1.000000        1

Tried this code:

   fig = plt.figure()
   ax = fig.add_subplot(111, projection='3d')
   x = np.array(df['Feature1'])
   y = np.array(df['Feature2'])
   z = np.array(df['Feature3'])
   ax.scatter(x,y,z, marker=colormap[kmeans.labels_], s=40)

However, I get the error "ValueError: could not convert string to float: red". The marker part is thus where I get the error.

2D visualization of clusters is pretty simple by plotting the points in a scatter plot and distinguishing it with cluster labels.

Just wondering is there a way to do 3D visualization of clusters.

Any suggestions would be highly appreciated !!

3
  • I get the error "ValueError: could not convert string to float: red". The marker part is where I get the error. It is not able to convert strings to floats. Typecasting does not help. In 2D plotting, it works but not sure why it does not work for 3D plotting. Commented Apr 18, 2017 at 15:39
  • So, what is colormap and what is kmeans.labels_? Commented Apr 18, 2017 at 15:41
  • @ ImportanceOfBeingErnest : kmeans.labels is the cluster indices like 0 and 1 (As i have 2 clusters). Colormap converts labels to colors. Commented Apr 18, 2017 at 15:43

1 Answer 1

9

In principle, the code from the question should work. However it is unclear what marker=colormap[kmeans.labels_] would do and why it is needed.

The 3D scatter plot works exactly as the 2D version of it.

The marker argument would expect a marker string, like "s" or "o" to determine the marker shape.
The color can be set using the c argument. You can provide a single color or an array/a list of colors. In the example below we simply provide the cluster index to c and use a colormap.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import pandas as pd
import numpy as np

v = np.random.rand(10,4)
v[:,3] = np.random.randint(0,2,size=10)
df = pd.DataFrame(v, columns=['Feature1', 'Feature2','Feature3',"Cluster"])
print (df)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x = np.array(df['Feature1'])
y = np.array(df['Feature2'])
z = np.array(df['Feature3'])

ax.scatter(x,y,z, marker="s", c=df["Cluster"], s=40, cmap="RdBu")

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.