Skip to content

Instantly share code, notes, and snippets.

@alik604
Last active October 24, 2023 22:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alik604/f4359c4d397aba213cd6643d65909862 to your computer and use it in GitHub Desktop.
Save alik604/f4359c4d397aba213cd6643d65909862 to your computer and use it in GitHub Desktop.
Quickly visualize your data in 2d and 3d with PCA and TSNE (t-sne)
# imports from matplotlib import pyplot as plt
from matplotlib import pyplot as plt
import pylab
from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d import proj3d
%matplotlib inline
%pylab inline
from sklearn.manifold import TSNE
from sklear.decomposition import PCA
#TSNE(2)
tsne = TSNE(n_components=2, random_state=0).fit_transform(X_train)
x, y = list(zip(*X_2d))
plt.figure(figsize=(9, 6))
plt.scatter(x, y)
plt.legend()
plt.show()
#TSNE(3)
data = TSNE(n_components=3, random_state=0).fit_transform(X_train)
x, y, z = list(zip(*data))
fig = pylab.figure()
ax = fig.add_subplot(111, projection = '3d')
sc = ax.scatter(x,y,z)
# PCA(2)
x, y = list(zip(*MinMaxScaler().fit_transform(PCA(2).fit_transform(X_train[:]))))
plt.figure(figsize=(9, 6))
plt.scatter(x, y)
plt.legend()
plt.show()
# PCA(3)
x, y, z = list(zip(*MinMaxScaler().fit_transform(PCA(3).fit_transform(X_train[:]))))
fig = pylab.figure()
ax = fig.add_subplot(111, projection = '3d')
sc = ax.scatter(x,y,z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment