%matplotlib inline to enable Matplotlib in this notebook.%matplotlib inline
from sklearn.datasets import load_digits
digits = load_digits()
TSNE Estimator for Dimensionality Reduction¶TSNE estimator uses an algorithm called t-distributed Stochastic Neighbor Embedding (t-SNE) to analyze a dataset’s features and reduce them to the specified number of dimensions PCA (principal components analysis) estimator but did not like the results, so we switched to TSNETSNE object that reduces a dataset’s features to two dimensions random_state for reproducibility of the “render sequence” when we display the digit clustersfrom sklearn.manifold import TSNE
tsne = TSNE(n_components=2, random_state=11)
TSNE methods fit and transformfit_transformdigits.data and two columns reduced_data = tsne.fit_transform(digits.data)
reduced_data.shape
scatterplot function, use Matplotlib’s scatter functionimport matplotlib.pyplot as plt
figure = plt.figure(figsize=(5, 5))
dots = plt.scatter(reduced_data[:, 0], reduced_data[:, 1], c='black')
TSNE could be quite different from dataset’s original featurestargets in Digits dataset to color the dots to see whether clusters indeed represent specific digitsc=digits.target — use target values determine dot colorscmap=plt.cm.get_cmap('nipy_spectral_r', 10) — color map to use figure = plt.figure(figsize=(6, 5))
dots = plt.scatter(reduced_data[:, 0], reduced_data[:, 1],
    c=digits.target, cmap=plt.cm.get_cmap('nipy_spectral_r', 10))
 
colorbar = plt.colorbar(dots)  
©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.