t-Distributed Stochastic Neighbor Embedding
Last updated
Last updated
The required packages for this section are pandas, seaborn and scikit-learn. These can be installed with the following command in the command window (Windows) / terminal (Mac).
For this section the following dataset will be used:
Load the dataset into a Pandas DataFrame named df as described in the basic plotting section:
Usually before running t-SNE, PCA is performed on the high-dimensional data to reduce the dimensions (e.g. 30, 50) and then t-SNE is applied on the derived Principal Components.
The first step is to normalise the data such that the features (lipids) have zero mean and unit variance, this can easily be done with the StandardScaler from sklearn. By indexing the dataframe with df.iloc[:,1:] we'll select all the data in the dataframe, except for the first column, which contains the labels:
Next, we use the PCA sklearn from sklearn, we'll select the 50 first principal components and we'll apply the PCA algorithm to the normalised data:
Then using t-SNE from sklearn we'll apply the t-SNE algrorithm on the 50 Principal Components.
The TSNE function from sklearn return the results as a numpy ndarray, let's put these results in a Pandas DataFrame:
We can now visualise the projection of the samples to the new feature space with a scatterplot: