Search code examples
pythoncluster-analysis

Not getting clusters formed in 3D for 38dimensions


Basically I am using K-means for dataset for 527*38(rows x columns). I want to use K-means and I have set 3 clusters but I am not able to visualize it in 3D-dimensions. If anyone can help i have tried it i will show it

I have converted my dataset x4 to list

Following is the code

from sklearn.cluster import KMeans
# Number of clusters
kmeans = KMeans(n_clusters=3)
# Fitting the input data
kmeans = kmeans.fit(x4)
# Centroid values
c = kmeans.cluster_centers_
import numpy as np
x4=np.array(x4)
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(x4[:, 0], x4[:, 1], x4[:, 2])
ax.scatter(c[:, 0], c[:, 1], c[:, 2], marker='*', c='#050505', s=1000)

Trying to plot in 3d i have attached image of output i am getting mycode

Here is the expected output image. Something like this is what i need for 3 clusters Expected output


Solution

  • Looks like you're trying to plot data points with 38 dimensions in a 3D plot using only the first three dimensions, which is incorrect.

    To properly visualize the clusters, you can try the following.

    1. Consider top 3 dimensions with most variance for plotting.
    2. Use dimension reduction methods like Principle Component Analysis to reduce 38 dimensions to 3 dimensions.