I applied K_Mean clustering on data and after I applied TSNE to plot the data. I have 4 dimension and 4 groups. The problem is my K_mean is correct but why with tsne, the same group are not all together?
the code :
XX = df [["agent_os_new","agent_category_new","referer_new","agent_name_new"]]
y = df['referer_new']
y
cols = XX.columns
from sklearn.preprocessing import MinMaxScaler
ms = MinMaxScaler()
X = ms.fit_transform(XX)
X = pd.DataFrame(X, columns=[cols])
X[:4]
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=4, random_state=0)
ymeans = kmeans.fit(X)
ymeans
labels = kmeans.labels_
df_new = XX.assign(Cluster =labels)
df_new
from sklearn.manifold import TSNE
import seaborn as sns
X_embedded = TSNE(n_components=2).fit_transform(df_new)
df_subset = pd.DataFrame()
df_subset['tsne1'] = X_embedded[:,0]
df_subset['tsne2'] = X_embedded[:,1]
plt.figure(figsize=(16,10))
sns.scatterplot(
x="tsne1", y="tsne2",
hue=df.label,
palette="Set1",
data=df_subset,
style=df_new["Cluster"],
legend="full",
s=120
)
what I want:
from sklearn.manifold import TSNE
import seaborn as sns
X_embedded = TSNE(n_components=2,random_state=42).fit_transform(X)
centers = np.array(kmeans.cluster_centers_)
model = KMeans(n_clusters = 4, init = "k-means++")
label = model.fit_predict(X_embedded)
plt.figure(figsize=(10,10))
uniq = np.unique(label)
for i in uniq:
plt.scatter(data[label == i , 0] , data[label == i , 1] , label = i)
plt.scatter(centers[:,0], centers[:,1], marker="x", color='k')
#This is done to find the centroid for each clusters.
plt.legend()
plt.show()