<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <!--BOOK_INFORMATION-->
<a href="https
Step1:  Using the familiar statistical modeling API, we import the AgglomerativeClustering
algorithm and specify the desired number of clusters
Step2:  Fitting the model to the data works, as usual, via the fit_predict method
Step3:  We can generate a scatter plot where every data point is colored according to the predicted
label
<ASSISTANT_TASK:>
Python Code:
from sklearn.datasets import make_blobs
X, y = make_blobs(n_samples=10, random_state=100)
Explanation: <!--BOOK_INFORMATION-->
<a href="https://www.packtpub.com/big-data-and-business-intelligence/machine-learning-opencv" target="_blank"><img align="left" src="data/cover.jpg" style="width: 76px; height: 100px; background: white; padding: 1px; border: 1px solid black; margin-right:10px;"></a>
This notebook contains an excerpt from the book Machine Learning for OpenCV by Michael Beyeler.
The code is released under the MIT license,
and is available on GitHub.
Note that this excerpt contains only the raw code - the book is rich with additional explanations and illustrations.
If you find this content useful, please consider supporting the work by
buying the book!
<!--NAVIGATION-->
< Classifying handwritten digits using k-means | Contents | 9. Using Deep Learning to Classify Handwritten Digits >
Implementing Agglomerative Hierarchical Clustering
Although OpenCV does not provide an implementation of agglomerative hierarchical
clustering, it is a popular algorithm that should, by all means, belong to our machine
learning repertoire.
We start out by generating 10 random data points, just like in the previous figure:
End of explanation
from sklearn import cluster
agg = cluster.AgglomerativeClustering(n_clusters=3)
Explanation: Using the familiar statistical modeling API, we import the AgglomerativeClustering
algorithm and specify the desired number of clusters:
End of explanation
labels = agg.fit_predict(X)
Explanation: Fitting the model to the data works, as usual, via the fit_predict method:
End of explanation
import matplotlib.pyplot as plt
%matplotlib inline
plt.style.use('ggplot')
plt.figure(figsize=(10, 6))
plt.scatter(X[:, 0], X[:, 1], c=labels, s=100)
Explanation: We can generate a scatter plot where every data point is colored according to the predicted
label:
End of explanation
<END_TASK>