Practical Exercises Chapter 15
Exercise 1: K-means Clustering
Task: Cluster the following set of 2D points into 2 clusters using K-means.
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt
# Sample Data
X = np.array([[1, 2],
[5, 8],
[1.5, 1.8],
[8, 8],
[1, 0.6],
[9, 11]])
# Implement K-means
kmeans = KMeans(n_clusters=2)
kmeans.fit(X)
labels = kmeans.labels_
# Visualizing the clusters
for i in range(len(X)):
plt.scatter(X[i][0], X[i][1], c=['r','g'][labels[i]])
plt.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1], marker='x')
plt.show()
Questions:
- What are the coordinates of the cluster centers?
- How does the number of clusters affect the result?
Exercise 2: Principal Component Analysis (PCA)
Task: Apply PCA to reduce the dimensions of the Iris dataset and then plot it.
from sklearn.decomposition import PCA
from sklearn...