Created
January 21, 2020 16:31
-
-
Save ortsed/1b086df367a1059beee4ff46310389e6 to your computer and use it in GitHub Desktop.
t-Stochastic Neighbor Embedding Example
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
Python | |
from sklearn import datasets | |
import seaborn as sn | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from sklearn.manifold import TSNE | |
#import the digits dataset | |
digits = datasets.load_digits() | |
X = digits.data | |
y = digits.target | |
tsne = TSNE(n_components=2, random_state=0) | |
tsne_data = tsne.fit_transform(X) | |
#creating a new dataframe for plotting | |
tsne_data = np.vstack((tsne_data.T, y)).T | |
tsne_df = pd.DataFrame(data=tsne_data, columns=("Dim_1", "Dim_2", "label")) | |
#Plotting the result | |
sn.FacetGrid(tsne_df, hue="label", size=6).map(plt.scatter, 'Dim_1', 'Dim_2').add_legend() | |
plt.show() | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
from sklearn import datasets | |
import seaborn as sn | |
import numpy as np | |
import pandas as pd | |
import matplotlib.pyplot as plt | |
from sklearn.manifold import TSNE | |
#import the digits dataset | |
digits = datasets.load_digits() | |
X = digits.data | |
y = digits.target | |
tsne = TSNE(n_components=2, random_state=0) | |
tsne_data = tsne.fit_transform(X) | |
#creating a new dataframe for plotting | |
tsne_data = np.vstack((tsne_data.T, y)).T | |
tsne_df = pd.DataFrame(data=tsne_data, columns=("Dim_1", "Dim_2", "label")) | |
#Plotting the result | |
sn.FacetGrid(tsne_df, hue="label", size=6).map(plt.scatter, 'Dim_1', 'Dim_2').add_legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment