Book Image

The Unsupervised Learning Workshop

By : Aaron Jones, Christopher Kruger, Benjamin Johnston
Book Image

The Unsupervised Learning Workshop

By: Aaron Jones, Christopher Kruger, Benjamin Johnston

Overview of this book

Do you find it difficult to understand how popular companies like WhatsApp and Amazon find valuable insights from large amounts of unorganized data? The Unsupervised Learning Workshop will give you the confidence to deal with cluttered and unlabeled datasets, using unsupervised algorithms in an easy and interactive manner. The book starts by introducing the most popular clustering algorithms of unsupervised learning. You'll find out how hierarchical clustering differs from k-means, along with understanding how to apply DBSCAN to highly complex and noisy data. Moving ahead, you'll use autoencoders for efficient data encoding. As you progress, you’ll use t-SNE models to extract high-dimensional information into a lower dimension for better visualization, in addition to working with topic modeling for implementing natural language processing (NLP). In later chapters, you’ll find key relationships between customers and businesses using Market Basket Analysis, before going on to use Hotspot Analysis for estimating the population density of an area. By the end of this book, you’ll be equipped with the skills you need to apply unsupervised algorithms on cluttered datasets to find useful patterns and insights.
Table of Contents (11 chapters)
Preface

6. t-Distributed Stochastic Neighbor Embedding

Activity 6.01: Wine t-SNE

Solution:

  1. Import pandas, numpy, and matplotlib, as well as the t-SNE and PCA models from scikit-learn:
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.decomposition import PCA
    from sklearn.manifold import TSNE
  2. Load the Wine dataset using the wine.data file included in the accompanying source code and display the first five rows of data:
    df = pd.read_csv('wine.data', header=None)
    df.head()

    The output is as follows:

    Figure 6.25: The first five rows of the Wine dataset

  3. The first column contains the labels; extract this column and remove it from the dataset:
    labels = df[0]
    del df[0]
  4. Execute PCA to reduce the dataset to the first six components:
    model_pca = PCA(n_components=6)
    wine_pca = model_pca.fit_transform(df)
  5. Determine the amount of variance within the data described by these six components:
    np.sum(model_pca.explained_variance_ratio_)

    The output...