Clustering Using K-Means
TensorFlow can also be used to implement iterative clustering algorithms such as k-means. In this recipe, we show an example of using k-means on the iris
dataset.
Getting ready
Almost all of the machine learning models we have explored in this book have been supervised models. TensorFlow is ideal for these types of problems. But we can also implement unsupervised models if we wish. As an example, this recipe will implement k-means clustering.
The dataset we will implement clustering on is the iris
dataset. One of the reasons this is a good dataset is because we already know there are three different targets (three types of iris flowers). This gives us a leg up on knowing that we are looking for three different clusters in the data.
We will cluster the iris
dataset into three groups, and then compare the accuracy of these clusters against the real labels.
How to do it…
To start, we load the necessary libraries. We are also loading some PCA tools from
sklearn
so that we...