Perform Agglomerative Clustering
Compito
Swipe to start coding
Your task is to use different linkage types and to look at the performance of agglomerative clustering on moons and circles datasets. You have to:
- Import
AgglomerativeClustering
class from sklearn.cluster module. - Add a parameter with the name
linkage
as an input of the function. - Add
.fit()
method of theagglomerative
object to train the model. - Use
'single'
,'complete'
, and'average'
as parameters of the function(parameters in the code have to be used in the same order).
Soluzione
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from sklearn.datasets import make_moons, make_circles
import matplotlib.pyplot as plt
import numpy as np
from sklearn.cluster import AgglomerativeClustering
def check_linkage_parameter(X, y, ds_name, linkage):
agglomerative = AgglomerativeClustering(linkage=linkage,
distance_threshold=0.5, n_clusters=None)
agglomerative.fit(X)
fig, axes = plt.subplots(1, 2)
fig.suptitle(ds_name+' dataset: '+ str(linkage)+' linkage')
axes[0].scatter(X[:, 0], X[:, 1], c=y, cmap='tab20b')
axes[0].set_title('Real clusters')
axes[1].scatter(X[:, 0], X[:, 1], c=agglomerative.labels_, cmap='tab20b')
axes[1].set_title('Clusters with Agglomerative')
X, y = make_moons(n_samples=500)
check_linkage_parameter(X, y, 'Moons', linkage='single')
check_linkage_parameter(X, y, 'Moons', linkage='complete')
check_linkage_parameter(X, y, 'Moons', linkage='average')
X, y = make_circles(n_samples=500)
check_linkage_parameter(X, y, 'Circles', linkage='single')
check_linkage_parameter(X, y, 'Circles', linkage='complete')
check_linkage_parameter(X, y, 'Circles', linkage='average')
Tutto è chiaro?
Grazie per i tuoi commenti!
Sezione 2. Capitolo 4
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from sklearn.datasets import make_moons, make_circles
import matplotlib.pyplot as plt
import numpy as np
from sklearn.___ import ___
# this function will train agglomerative model with different linakges and plot the results
def check_linkage_parameter(X, y, ds_name, ___):
agglomerative = AgglomerativeClustering(linkage=linkage,
distance_threshold=0.5, n_clusters=None)
agglomerative.___(X)
fig, axes = plt.subplots(1, 2)
fig.suptitle(ds_name+' Dataset: '+str(linkage)+' linkage')
axes[0].scatter(X[:, 0], X[:,1 ], c=y, cmap='tab20b')
axes[0].set_title('Real clusters')
axes[1].scatter(X[:, 0], X[:, 1], c=agglomerative.labels_, cmap='tab20b')
axes[1].set_title('Clusters with Agglomerative')
# Check clustering quality on moons dataset
X, y = make_moons(n_samples=500)
check_linkage_parameter(X, y, 'Moons', linkage=___)
check_linkage_parameter(X, y, 'Moons', linkage=___)
check_linkage_parameter(X, y, 'Moons', linkage=___)
# Check clustering quality on circles dataset
X, y = make_circles(n_samples=500)
check_linkage_parameter(X, y, 'Circles', linkage='single')
check_linkage_parameter(X, y, 'Circles', linkage='complete')
check_linkage_parameter(X, y, 'Circles', linkage='average')
Chieda ad AI
Chieda ad AI
Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione