Book Image

NumPy: Beginner's Guide

By : Ivan Idris
Book Image

NumPy: Beginner's Guide

By: Ivan Idris

Overview of this book

Table of Contents (21 chapters)
NumPy Beginner's Guide Third Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
NumPy Functions' References
Index

Time for Action – clustering points


We will generate some random points and cluster them, which means that the points that are close to each other are put into the same cluster. This is just one of the many techniques that you can apply with scikit-learn. Clustering is a type of machine learning algorithm, which aims to group items based on similarities. Next, we will calculate a square affinity matrix. An affinity matrix is a matrix containing affinity values: for instance, the distances between points. Finally, we will cluster the points with the AffinityPropagation class from scikit-learn.

  1. Generate 30 random point positions within a square of 400 by 400 pixels:

    positions = np.random.randint(0, 400, size=(30, 2))
  2. Calculate the affinity matrix using the Euclidean distance to the origin as the affinity metric:

    positions_norms = np.sum(positions ** 2, axis=1)
    S = - positions_norms[:, np.newaxis] - positions_norms[np.newaxis, :] + 2 * np.dot(positions, positions.T)
  3. Give the AffinityPropagation...