Created
June 3, 2020 13:22
-
-
Save Franklin-Yao/c0282c3ecf82f9a2c2898324120e3e47 to your computer and use it in GitHub Desktop.
tsne visualization
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
from time import time | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from sklearn import datasets | |
from sklearn.manifold import TSNE | |
def get_data(): | |
digits = datasets.load_digits(n_class=10) | |
data = digits.data | |
label = digits.target | |
n_samples, n_features = data.shape | |
return data, label, n_samples, n_features | |
def plot_embedding(data, label, title): | |
x_min, x_max = np.min(data, 0), np.max(data, 0) | |
data = (data - x_min) / (x_max - x_min) | |
fig = plt.figure() | |
label_colors = np.random.random([10 , 3]) | |
colors = label_colors[label] | |
plt.scatter(data[:,0], data[:,1], c=colors) | |
plt.xticks([]) | |
plt.yticks([]) | |
plt.title(title) | |
plt.show() | |
return | |
def main(): | |
data, label, n_samples, n_features = get_data() | |
print('Computing t-SNE embedding') | |
tsne = TSNE(n_components=2, init='pca', random_state=0) | |
t0 = time() | |
result = tsne.fit_transform(data) | |
plot_embedding(result, label, | |
't-SNE embedding of the digits (time %.2fs)' | |
% (time() - t0)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment