Skip to content

Instantly share code, notes, and snippets.

@christopherlovell
Last active July 27, 2018 11:51
Show Gist options
  • Select an option

  • Save christopherlovell/dd2eee5aabaf6e69bd74addc63f5a13d to your computer and use it in GitHub Desktop.

Select an option

Save christopherlovell/dd2eee5aabaf6e69bd74addc63f5a13d to your computer and use it in GitHub Desktop.
t-SNE example

t-SNE spectra example

Apply t-SNE to galaxy spectra from two different simulations.

First import some modules

import numpy as np

%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib import rc
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=False)

from sklearn.manifold import TSNE

Then load your spectra in to 2D arrays. This assumes that they are sampled on the same resolution grid, so each pixel (or feature) is being compared fairly.

illustris_spec = np.array(...)
eagle_spec = np.array(...)

np.random.shuffle(illustris_spec)
np.random.shuffle(eagle_spec)

combine the spectra from both simulations.

dat = np.vstack([illustris_spec, eagle_spec, vespa_spec])

Run the t-SNE with 2 components!

combined_tsne = TSNE(n_components=2).fit_transform(dat)

Plot the 2D t-SNE distribution, labelled by simulation.

fig = plt.figure()

alpha=0.8
size=4

Ni = len(illustris_spec)
Ne = len(eagle_spec)

plt.scatter(combined_tsne[:Ni,0], combined_tsne[:Ni,1], label='Illustris', alpha=alpha, s=size)
plt.scatter(combined_tsne[Ni:Ni+Ne,0], combined_tsne[Ni:Ni+Ne,1], label='EAGLE', alpha=alpha, s=size)

plt.legend()

plt.xticks([])
plt.yticks([])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment