Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Applied Unsupervised Learning with Python

You're reading from   Applied Unsupervised Learning with Python Discover hidden patterns and relationships in unstructured data with Python

Arrow left icon
Product type Paperback
Published in May 2019
Publisher
ISBN-13 9781789952292
Length 482 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (3):
Arrow left icon
Aaron Jones Aaron Jones
Author Profile Icon Aaron Jones
Aaron Jones
Benjamin Johnston Benjamin Johnston
Author Profile Icon Benjamin Johnston
Benjamin Johnston
Christopher Kruger Christopher Kruger
Author Profile Icon Christopher Kruger
Christopher Kruger
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Chapter 1
2. Introduction to Clustering FREE CHAPTER 3. Chapter 2
4. Hierarchical Clustering 5. Chapter 3
6. Neighborhood Approaches and DBSCAN 7. Chapter 4
8. Dimension Reduction and PCA 9. Chapter 5
10. Autoencoders 11. Chapter 6
12. t-Distributed Stochastic Neighbor Embedding (t-SNE) 13. Chapter 7
14. Topic Modeling 15. Chapter 8
16. Market Basket Analysis 17. Chapter 9
18. Hotspot Analysis Appendix

Chapter 1: Introduction to Clustering

Activity 1: Implementing k-means Clustering

Solution:

  1. Load the Iris data file using pandas, a package that makes data wrangling much easier through the use of DataFrames:
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    from sklearn.metrics import silhouette_score
    from scipy.spatial.distance import cdist
    iris = pd.read_csv('iris_data.csv', header=None)
    iris.columns = ['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm', 'species']
  2. Separate out the X features and the provided y species labels, since we want to treat this as an unsupervised learning problem:
    X = iris[['SepalLengthCm', 'SepalWidthCm', 'PetalLengthCm', 'PetalWidthCm']]
    y = iris['species']
  3. Get an idea of what our features look like:
    X.head()

    The output is as follows:

    Figure 1.22: First five rows of the data
  4. Bring back the...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Banner background image