Time for action – clustering points
We will generate some random points and cluster them, which means that points that are close to each other are put in the same cluster. This is only 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. Second, we will calculate a square affinity matrix. An affinity matrix is a matrix containing affinity values; for instance, distances between points. Finally, we will cluster the points with the AffinityPropagation
class from scikit-learn
. Perform the following steps to cluster points:
We will generate 30 random point positions within a square of 400 x 400 pixels:
positions = np.random.randint(0, 400, size=(30, 2))
We will use the Euclidean distance to the origin as affinity matrix.
positions_norms = np.sum(positions ** 2, axis=1) S = - positions_norms[:, np.newaxis] - positions_norms[np.newaxis, :] + 2 * np.dot(positions, positions.T)
Give the
AffinityPropagation...