Last active
January 13, 2025 17:28
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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