# Using TSNE to Visualize Clusters in Python

I'm using TSNE to visualize my clusters but the output seems a bit strange. There are supposed to be 3 clusters but instead, there are 4 lines. Is there something wrong with how I'm visualizing them or is it the kmeans method itself?

import pandas as pd
import numpy as np
import ast
from sklearn import metrics
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
from sklearn.manifold import TSNE
from sklearn.decomposition import TruncatedSVD

colNames = ['unixTime', 'sampleAmount','Time','samplingRate', 'Data']

data = pd.read_csv("project_fan.csv",  sep = ';', error_bad_lines = False, names = colNames)

# changing data into list
data['Data'] = data.Data.transform(ast.literal_eval)

# Selecting the average value from the list and replacing the list with it
data['Data'] = data.Data.apply(np.mean)

kmeanModel = KMeans(n_clusters = 3)
kmeanModel.fit(data)

y = kmeanModel.labels_

X_train, X_test, y_train, y_test = train_test_split(data, y, test_size = 0.2, random_state = 1)

k = 3
tfs_reduced = TruncatedSVD(n_components=k, random_state=0).fit_transform(data)
tfs_embedded = TSNE(n_components=2, perplexity=40, verbose=2).fit_transform(tfs_reduced)
fig = plt.figure(figsize = (10, 10))
ax = plt.axes()
plt.scatter(tfs_embedded[:, 0], tfs_embedded[:, 1], marker = "x", c = km.labels_)
plt.show()


Sample Dataset:

       unixTime  sampleAmount  Time  samplingRate         Data
0  1.556891e+09         16384   340  48188.235294  1620.242170
1  1.556891e+09         16384   341  48046.920821  1620.237716
2  1.556891e+09         16384   340  48188.235294  1620.236340
3  1.556891e+09         16384   340  48188.235294  1620.229289
4  1.556891e+09         16384   340  48188.235294  1620.227541


Output:

• I am not sure we will be able to help you without the data. How did you find that there are 3 clusters ? Can you plot the tnse results but with the kmeans colors ? Jan 19 '20 at 13:20
• @lcrmorin here's my data: drive.google.com/file/d/1APIG7C5d-zWPfe1bZa2azDmfQIkDOyVu/…
– x89
Jan 19 '20 at 13:55
• @lcrmorin I used the elbow method to find an optimal number of clusters i.e 3
– x89
Jan 19 '20 at 13:55