apply three main clustering methods k-means, density-based, and hierarchical methods on different toy datasets
Need help with this assignment?Get an original answer from a qualified tutor — from $10/page.
Get it written →
In this assignment, you will use Python’s sklearn.cluster package and three of its tools called “KMeans”, “DBSCAN”, and “AgglomerativeClustering” to apply three main clustering methods k-means, density-based, and hierarchical methods on different toy datasets of Python SKLearn (see here for more detailLinks to an external site.). You can watch the video illustrating some of the Python tools that you need for this assignment:
Part 1: k-means clustering (25 points):
Load breast cancer dataset (by calling sklearn.datasets.load_breast_cancer)
data = load_breast_cancer().data
Since the dataset has 30 different features, use the following code to reduce the dimensions of the dataset to 2 features (as we will later plot the observations using 2D scatter plots, this is a necessary step!):
from sklearn.decomposition import PCA
pca = PCA(2)
df = pca.fit_transform(data)
Create an object of sklearn.cluster.KMeans class with n_clusters = 10.
- kmeans = KMeans(n_clusters = 10)
Fit and predict the transformed dataset df:
- label = kmeans.fit_predict(df)
Use matplotlib.pyplot to plot all the observations on a 2D coordinate system. Color observations of each cluster with a different color:
import matplotlib.pyplot as plt
for i in range(10):
- plt.scatter(df[label == i , 0] , df[label == i , 1] , label = i)
plt.legend()
- plt.show()
Part 2 (25 points): Repeat Part I for digits dataset (use load_digits() instead). Use density-based clustering (use DBSCAN(min_samples = 10, eps = 1.5) instead of KMeans(n_clusters = 10)). Draw the plot for labels 0, 1, 2, …, 22
Part 3 (25 points): Repeat Part I for iris dataset (use load_iris() instead). Use hierarchical clustering with 5 clusters (use AgglomerativeClustering(n_clusters = 5) instead of KMeans(n_clusters = 10))
- Part 4 (25 points) Repeat Parts 1 and 3 for n_clusters = 20 and digits dataset.
Get a plagiarism-free answer to this question
Send us your instructions and we’ll match you with the best writer in your subject.
- 100% human-written, zero AI
- Turnitin report included
- Confidential — we never share your data
- Free revisions & refunds