Seminar 10
Seminar 10
Seminar 10
Exercise 1 Using your preferred editor (colab is recommended) to fill the snippet gaps.
The following is a simple demonstration of using WSS to decide and plot the clusters
based on k-means clusters algorithm.
%% Implement the WSS method and check through the number of clusters from 1
%% to 12, and plot the figure of WSS vs. number of clusters.
%% Hint: reference the plots in the lecture slides;
%% You may need to use inertia_ from property WCSS, and kmeans function
%
wcss = []
for i in range(1, 12):
kmeans = KMeans(n_clusters=i, init='k-means++', max_iter=300, n_init=10,
random_state=0)
Insert your code block here
Exercise 2 For the following code blocks and plots, run the code first; then provide your
interpretation/explanation for the required parts.
k-means on digits
We will attempt to use k-means to try to identify similar digits without using the original
label information; this might be similar to a first step in extracting meaning from a new
dataset about which you don't have any a priori label information.
We will start by loading the digits and then finding the k-Means clusters. The digits
consist of 1,797 samples with 64 features, where each of the 64 features is the
brightness of one pixel in an 8×8 image.
2
UWL SCE S2
#
from sklearn.metrics import confusion_matrix
mat = confusion_matrix(digits.target, labels)
sns.heatmap(mat.T, square=True, annot=True, fmt='d', cbar=False,
xticklabels=digits.target_names,
yticklabels=digits.target_names)
plt.xlabel('true label')
plt.ylabel('predicted label');